| plugins { |
| id "com.github.node-gradle.node" version "2.2.0" |
| } |
| |
| description = 'Kotlin Standard Library for JS' |
| |
| apply plugin: 'kotlin-platform-js' |
| apply plugin: 'idea' |
| |
| configurePublishing(project) |
| |
| configurations { |
| sources |
| commonSources |
| distSources |
| distJs |
| distLibrary |
| publishedArtifacts |
| } |
| |
| def builtinsSrcDir = "${buildDir}/builtin-sources" |
| def builtinsSrcDir2 = "${buildDir}/builtin-sources-for-builtins" |
| def commonSrcDir = "${projectDir}/../src/kotlin" |
| def commonSrcDir2 = "${projectDir}/../common/src" |
| def jsCommonDir = "${projectDir}/../js" |
| |
| def builtinsDir = "${rootDir}/core/builtins" |
| def unsignedCommonSrcDir = "${rootDir}/libraries/stdlib/unsigned/src" |
| |
| def jsSrcDir = "src" |
| def jsCommonSrcDir = "${jsCommonDir}/src" |
| def jsTestSrcDir = "test" |
| def jsCommonTestSrcDir = "${jsCommonDir}/test" |
| |
| def jsSrcJsDir = "${jsSrcDir}/js" |
| def jsOutputFile = "${buildDir}/classes/kotlin.js" |
| def jsOutputMetaFile = "${buildDir}/classes/kotlin.meta.js" |
| |
| def kotlinTestJsOutputFile = "${project(':kotlin-test:kotlin-test-js').buildDir}/classes/main/kotlin-test.js" |
| |
| // TODO: take from sourcesets' outputs |
| def jsTestOutputFile = "${buildDir}/classes/kotlin/test/kotlin-stdlib-js_test.js" |
| def kotlinTestJsTestOutputFile = "${project(':kotlin-test:kotlin-test-js').buildDir}/classes/kotlin/test/kotlin-test-js_test.js" |
| |
| sourceSets { |
| builtins { |
| kotlin { |
| srcDir builtinsSrcDir2 |
| srcDir "${jsCommonDir}/runtime" |
| srcDir 'runtime' |
| } |
| } |
| |
| main { |
| kotlin { |
| srcDir builtinsSrcDir |
| srcDir jsCommonSrcDir |
| srcDir jsSrcDir |
| } |
| } |
| |
| test { |
| kotlin { |
| srcDir jsTestSrcDir |
| srcDir jsCommonTestSrcDir |
| } |
| } |
| } |
| |
| dependencies { |
| expectedBy project(":kotlin-stdlib-common") |
| commonSources project(path: ":kotlin-stdlib-common", configuration: "sources") |
| testCompile project(':kotlin-test:kotlin-test-js') |
| } |
| |
| task prepareComparableSource(type: Copy) { |
| doFirst { |
| delete builtinsSrcDir2 |
| } |
| from("${builtinsDir}/native/kotlin") { |
| include "Comparable.kt" |
| } |
| into builtinsSrcDir2 |
| } |
| |
| task prepareBuiltinsSources(type: Copy) { |
| doFirst { |
| delete builtinsSrcDir |
| } |
| from("${builtinsDir}/native/kotlin") { |
| include "Iterator.kt" |
| include "Collections.kt" |
| include "CharSequence.kt" |
| include "Annotation.kt" |
| } |
| from("${builtinsDir}/src/kotlin/") { |
| include "annotation/Annotations.kt" |
| include "Function.kt" |
| include "Iterators.kt" |
| include "Range.kt" |
| include "Progressions.kt" |
| include "ProgressionIterators.kt" |
| include "Ranges.kt" |
| include "internal/InternalAnnotations.kt" |
| include "internal/progressionUtil.kt" |
| include "Unit.kt" |
| } |
| into builtinsSrcDir |
| } |
| |
| tasks.withType(org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile) { |
| kotlinOptions { |
| main = "noCall" |
| moduleKind = "commonjs" |
| freeCompilerArgs = [ |
| "-version", |
| "-Xallow-kotlin-package", |
| "-Xallow-result-return-type" |
| ] |
| } |
| } |
| |
| compileBuiltinsKotlin2Js { |
| dependsOn prepareComparableSource |
| kotlinOptions { |
| metaInfo = false |
| outputFile = "${buildDir}/classes/builtins/kotlin.js" |
| sourceMap = true |
| sourceMapPrefix = "./" |
| } |
| } |
| |
| compileKotlin2Js { |
| dependsOn prepareBuiltinsSources |
| kotlinOptions { |
| outputFile = "${buildDir}/classes/main/kotlin.js" |
| sourceMap = true |
| sourceMapPrefix = "./" |
| sourceMapBaseDirs = files( |
| [builtinsSrcDir, jsSrcDir, jsCommonSrcDir, commonSrcDir, commonSrcDir2, unsignedCommonSrcDir] |
| .collect { file(it).absoluteFile } |
| ) |
| freeCompilerArgs += [ |
| "-Xopt-in=kotlin.RequiresOptIn", |
| "-Xopt-in=kotlin.ExperimentalMultiplatform", |
| "-Xopt-in=kotlin.contracts.ExperimentalContracts", |
| "-Xinline-classes" |
| ] |
| } |
| } |
| |
| compileTestKotlin2Js { |
| kotlinOptions { |
| moduleKind = "umd" |
| freeCompilerArgs += [ |
| "-Xopt-in=kotlin.RequiresOptIn", |
| "-Xopt-in=kotlin.ExperimentalUnsignedTypes", |
| "-Xopt-in=kotlin.ExperimentalStdlibApi", |
| ] |
| } |
| } |
| |
| task compileJs(type: NoDebugJavaExec) { |
| dependsOn compileBuiltinsKotlin2Js, compileKotlin2Js |
| inputs.files(compileBuiltinsKotlin2Js.outputs.files).withPathSensitivity(PathSensitivity.RELATIVE) |
| inputs.files(compileKotlin2Js.outputs.files).withPathSensitivity(PathSensitivity.RELATIVE) |
| inputs.dir(jsSrcDir).withPathSensitivity(PathSensitivity.RELATIVE) |
| inputs.dir(jsCommonSrcDir).withPathSensitivity(PathSensitivity.RELATIVE) |
| |
| outputs.file(jsOutputFile) |
| outputs.file("${jsOutputFile}.map") |
| outputs.file(jsOutputMetaFile) |
| outputs.cacheIf { true } |
| |
| def inputFiles = fileTree(jsSrcJsDir) { |
| include '**/*.js' |
| } |
| |
| main = "org.jetbrains.kotlin.cli.js.internal.JSStdlibLinker" |
| doFirst { |
| args = [jsOutputFile, rootDir, "$jsSrcDir/wrapper.js"] + inputFiles.collect { it.path }.sort() + |
| (compileBuiltinsKotlin2Js.outputs.files.collect { it.path }.sort() + |
| compileKotlin2Js.outputs.files.collect { it.path }.sort()).findAll { |
| it.endsWith(".js") && !it.endsWith(".meta.js") |
| } |
| } |
| classpath = configurations.kotlinCompilerClasspath |
| |
| doLast { |
| ant.replaceregexp( |
| file: jsOutputFile, |
| match: "module.exports,\\s*require\\([^)]+\\)", |
| replace: "", |
| byline: "true", encoding: "UTF-8") |
| ant.replaceregexp( |
| file: jsOutputFile, |
| match: "function\\s*\\(_,\\s*Kotlin\\)", |
| replace: "function()", |
| byline: "true", encoding: "UTF-8") |
| ant.replaceregexp( |
| file: jsOutputFile, |
| match: "return\\s+_;", |
| replace: "", |
| byline: "true", encoding: "UTF-8") |
| |
| def sourceMapFile = file("${jsOutputFile}.map") |
| def sourceMap = new groovy.json.JsonSlurper().parseText(sourceMapFile.text) |
| |
| def sourceMapBasePaths = [ |
| "./", |
| "libraries/stdlib/js-v1/src/js/", |
| "libraries/stdlib/js-v1/src/", |
| ] |
| sourceMap.sources = sourceMap.sources.collect { sourcePath -> |
| def prefixToRemove = sourceMapBasePaths.find { basePath -> sourcePath.startsWith(basePath) } |
| if (prefixToRemove != null) sourcePath.substring(prefixToRemove.length()) else sourcePath |
| } |
| |
| def sourceMapSourcesBaseDirs = [jsSrcDir, jsCommonSrcDir, jsSrcJsDir, builtinsSrcDir, commonSrcDir, commonSrcDir2, projectDir, unsignedCommonSrcDir] |
| |
| sourceMap.sourcesContent = sourceMap.sources.collect { sourceName -> |
| def text = sourceMapSourcesBaseDirs.collect { file("$it/$sourceName") }.find { it.exists() }?.text |
| if (text == null) logger.warn("Sources missing for file $sourceName") |
| text |
| } |
| |
| sourceMapFile.text = groovy.json.JsonOutput.toJson(sourceMap) |
| |
| file(jsOutputMetaFile).text = file(compileKotlin2Js.outputFile.path.replaceAll('\\.js$', '.meta.js')).text |
| } |
| } |
| |
| classes.dependsOn compileJs |
| |
| |
| jar { |
| enabled false |
| } |
| |
| task libraryJarWithoutIr(type: Jar, dependsOn: compileJs) { |
| classifier = null |
| manifestAttributes(manifest, project, 'Main') |
| destinationDirectory = file("$buildDir/lib/dist") |
| |
| // TODO: Use standard implementation title after js stdlib detector becomes more flexible (KT-17655) |
| Properties properties = new Properties() |
| new File("${rootDir}/resources/kotlinManifest.properties").withInputStream { |
| properties.load(it) |
| } |
| manifest.attributes 'Implementation-Title': properties."manifest.impl.title.kotlin.javascript.stdlib" |
| |
| includeEmptyDirs false |
| duplicatesStrategy DuplicatesStrategy.EXCLUDE |
| from jsOutputFile |
| from jsOutputMetaFile |
| from "${jsOutputFile}.map" |
| from sourceSets.main.output |
| } |
| |
| task libraryJarWithIr(type: Zip, dependsOn: libraryJarWithoutIr) { |
| archiveExtension = "jar" |
| destinationDirectory = file("$buildDir/libs") |
| |
| duplicatesStrategy DuplicatesStrategy.FAIL |
| |
| from zipTree(libraryJarWithoutIr.archiveFile) |
| dependsOn(":kotlin-stdlib-js-ir:compileKotlinJs") |
| from { |
| def irKlib = tasks.getByPath(":kotlin-stdlib-js-ir:compileKotlinJs") |
| fileTree(irKlib.outputs.files.first().path) |
| } |
| } |
| |
| jar.dependsOn(libraryJarWithIr) |
| |
| task sourcesJar(type: Jar, dependsOn: compileJs) { |
| classifier = 'sources' |
| includeEmptyDirs false |
| duplicatesStrategy = DuplicatesStrategy.FAIL |
| from(sourceSets.builtins.allSource) { |
| into 'kotlin' |
| } |
| from(sourceSets.main.allSource) { |
| into 'kotlin' |
| exclude '**/*.java' |
| exclude 'org.w3c/**' |
| exclude 'js/**' |
| } |
| from(sourceSets.main.allSource) { |
| include 'org.w3c/**' |
| } |
| } |
| |
| task distSourcesJar(type: Jar) { |
| dependsOn(sourcesJar, configurations.commonSources) |
| destinationDirectory = file("$buildDir/lib/dist") |
| archiveClassifier = 'sources' |
| duplicatesStrategy = DuplicatesStrategy.FAIL |
| from zipTree(sourcesJar.outputs.files.singleFile) |
| |
| from(zipTree(configurations.commonSources.singleFile)) { |
| it.includeEmptyDirs = false |
| exclude 'META-INF/*' |
| into 'common' |
| } |
| } |
| |
| def javadocJar = javadocJar() |
| |
| artifacts { |
| runtime libraryJarWithIr |
| publishedArtifacts libraryJarWithIr |
| publishedArtifacts sourcesJar |
| publishedArtifacts javadocJar |
| sources sourcesJar |
| distSources distSourcesJar |
| distLibrary libraryJarWithoutIr |
| |
| compileJs.outputs.files.forEach { artifact -> |
| distJs(artifact) { builtBy(compileJs) } |
| } |
| } |
| |
| uploadArchives { |
| configuration = configurations.publishedArtifacts |
| } |
| |
| install { |
| configuration = configurations.publishedArtifacts |
| } |
| |
| node { |
| download = true |
| version = '12.18.0' |
| nodeModulesDir = buildDir |
| } |
| |
| // Otherwise Node ignores nodeModulesDir |
| task deleteLegacyNodeModules(type: Delete) { |
| delete "$projectDir/node_modules" |
| } |
| |
| task installMocha(type: NpmTask, dependsOn: [deleteLegacyNodeModules]) { |
| args = ['install', '[email protected]'] |
| } |
| |
| task installTeamcityReporter(type: NpmTask, dependsOn: [deleteLegacyNodeModules]) { |
| args = ['install', '[email protected]'] |
| } |
| |
| task runMocha(type: NodeTask, dependsOn: [testClasses, installMocha, ':kotlin-test:kotlin-test-js:testClasses']) { |
| script = file("${buildDir}/node_modules/mocha/bin/mocha") |
| |
| if (project.hasProperty("teamcity")) { |
| dependsOn installTeamcityReporter |
| args = ['--reporter', 'mocha-teamcity-reporter'] |
| } |
| else { |
| args = ['--reporter', 'min'] |
| } |
| |
| args += [jsTestOutputFile, kotlinTestJsTestOutputFile] |
| |
| execOverrides { |
| it.ignoreExitValue = rootProject.ignoreTestFailures |
| it.environment('NODE_PATH', [file(jsOutputFile).parent, file(kotlinTestJsOutputFile).parent].join(File.pathSeparator)) |
| it.workingDir = buildDir |
| } |
| } |
| |
| test.dependsOn runMocha |