From cc45ea78a5773e45a7e1379813701ab393a55ea6 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Tue, 26 May 2020 13:20:48 +0300 Subject: [PATCH 1/4] Prepare for 1.4-M2 Kotlin/JS --- gradle/compile-js-multiplatform.gradle | 29 +++++++++++--------------- gradle/publish-npm-js.gradle | 2 +- gradle/test-mocha-js.gradle | 22 +++++++++---------- js/example-frontend-js/build.gradle | 2 +- 4 files changed, 25 insertions(+), 30 deletions(-) diff --git a/gradle/compile-js-multiplatform.gradle b/gradle/compile-js-multiplatform.gradle index 2b00d6fc82..cecdedf3cc 100644 --- a/gradle/compile-js-multiplatform.gradle +++ b/gradle/compile-js-multiplatform.gradle @@ -5,16 +5,11 @@ apply from: rootProject.file('gradle/node-js.gradle') kotlin { - targets { - fromPreset(presets.js, 'js') { - // Enable built-in test runner only for IR target. - // These runners don't support changing js module name change. - if (js.hasProperty("irTarget") && irTarget != null) { - irTarget.nodejs() - irTarget.compilations['main']?.dependencies { - api "org.jetbrains.kotlinx:atomicfu-js:$atomicfu_version" - } - } + js { + moduleName = project.name + nodejs() + irTarget.compilations['main']?.dependencies { + api "org.jetbrains.kotlinx:atomicfu-js:$atomicfu_version" } } @@ -32,7 +27,7 @@ kotlin { // When source sets are configured apply from: rootProject.file('gradle/test-mocha-js.gradle') -compileKotlinJs { +compileKotlinJsLegacy { kotlinOptions.metaInfo = true kotlinOptions.sourceMap = true kotlinOptions.moduleKind = 'umd' @@ -44,21 +39,21 @@ compileKotlinJs { } } -compileTestKotlinJs { +compileTestKotlinJsLegacy { kotlinOptions.metaInfo = true kotlinOptions.sourceMap = true kotlinOptions.moduleKind = 'umd' } -task populateNodeModules(type: Copy, dependsOn: compileTestKotlinJs) { +task populateNodeModules(type: Copy, dependsOn: compileTestKotlinJsLegacy) { // we must copy output that is transformed by atomicfu - from(kotlin.targets.js.compilations.main.output.allOutputs) + from(kotlin.js().compilations.main.output.allOutputs) into "$node.nodeModulesDir/node_modules" - def configuration = configurations.hasProperty("legacyjsTestRuntimeClasspath") - ? configurations.legacyjsTestRuntimeClasspath - : configurations.jsTestRuntimeClasspath + def configuration = configurations.hasProperty("jsLegacyTestRuntimeClasspath") + ? configurations.jsLegacyTestRuntimeClasspath + : configurations.jsIrTestRuntimeClasspath from(files { configuration.collect { File file -> diff --git a/gradle/publish-npm-js.gradle b/gradle/publish-npm-js.gradle index f471c48047..d427c3d3ee 100644 --- a/gradle/publish-npm-js.gradle +++ b/gradle/publish-npm-js.gradle @@ -30,7 +30,7 @@ task preparePublishNpm(type: Copy) { } } // we must publish output that is transformed by atomicfu - from(kotlin.targets.js.compilations.main.output.allOutputs) + from(kotlin.js().compilations.main.output.allOutputs) into npmDeployDir } diff --git a/gradle/test-mocha-js.gradle b/gradle/test-mocha-js.gradle index 60969f908d..d630422099 100644 --- a/gradle/test-mocha-js.gradle +++ b/gradle/test-mocha-js.gradle @@ -13,15 +13,15 @@ task installDependenciesMochaNode(type: NpmTask, dependsOn: [npmInstall]) { } // todo: use atomicfu-transformed test files here (not critical) -task testMochaNode(type: NodeTask, dependsOn: [compileTestKotlinJs, installDependenciesMochaNode]) { +task testMochaNode(type: NodeTask, dependsOn: [compileTestKotlinJsLegacy, installDependenciesMochaNode]) { script = file("$node.nodeModulesDir/node_modules/mocha/bin/mocha") - args = [compileTestKotlinJs.outputFile, '--require', 'source-map-support/register'] + args = [compileTestKotlinJsLegacy.outputFile, '--require', 'source-map-support/register'] if (project.hasProperty("teamcity")) args += ['--reporter', 'mocha-teamcity-reporter'] } -def legacyjsTestTask = project.tasks.findByName('legacyjsTest') ? legacyjsTest : jsTest +def jsLegacyTestTask = project.tasks.findByName('jsLegacyTest') ? jsLegacyTest : jsIrTest -legacyjsTestTask.dependsOn testMochaNode +jsLegacyTestTask.dependsOn testMochaNode // -- Testing with Mocha under headless Chrome @@ -38,7 +38,7 @@ task installDependenciesMochaChrome(type: NpmTask, dependsOn: [npmInstall]) { def mochaChromeTestPage = file("$buildDir/test-page.html") -task prepareMochaChrome(dependsOn: [compileTestKotlinJs, installDependenciesMochaChrome]) { +task prepareMochaChrome(dependsOn: [compileTestKotlinJsLegacy, installDependenciesMochaChrome]) { outputs.file(mochaChromeTestPage) } @@ -56,8 +56,8 @@ prepareMochaChrome.doLast { - - + + @@ -66,7 +66,7 @@ prepareMochaChrome.doLast { task testMochaChrome(type: NodeTask, dependsOn: prepareMochaChrome) { script = file("$node.nodeModulesDir/node_modules/mocha-headless-chrome/bin/start") - args = [compileTestKotlinJs.outputFile, '--file', mochaChromeTestPage] + args = [compileTestKotlinJsLegacy.outputFile, '--file', mochaChromeTestPage] if (project.hasProperty("teamcity")) args += ['--reporter', 'mocha-teamcity-reporter'] } @@ -85,11 +85,11 @@ task installDependenciesMochaJsdom(type: NpmTask, dependsOn: [npmInstall]) { if (project.hasProperty("teamcity")) args += ["mocha-teamcity-reporter@$mocha_teamcity_reporter_version"] } -task testMochaJsdom(type: NodeTask, dependsOn: [compileTestKotlinJs, installDependenciesMochaJsdom]) { +task testMochaJsdom(type: NodeTask, dependsOn: [compileTestKotlinJsLegacy, installDependenciesMochaJsdom]) { script = file("$node.nodeModulesDir/node_modules/mocha/bin/mocha") - args = [compileTestKotlinJs.outputFile, '--require', 'source-map-support/register', '--require', 'jsdom-global/register'] + args = [compileTestKotlinJsLegacy.outputFile, '--require', 'source-map-support/register', '--require', 'jsdom-global/register'] if (project.hasProperty("teamcity")) args += ['--reporter', 'mocha-teamcity-reporter'] } -legacyjsTestTask.dependsOn testMochaJsdom +jsLegacyTestTask.dependsOn testMochaJsdom diff --git a/js/example-frontend-js/build.gradle b/js/example-frontend-js/build.gradle index dc4dfd2d9a..735a70d55b 100644 --- a/js/example-frontend-js/build.gradle +++ b/js/example-frontend-js/build.gradle @@ -8,7 +8,7 @@ apply from: rootProject.file('gradle/node-js.gradle') // Workaround resolving new Gradle metadata with kotlin2js // TODO: Remove once KT-37188 is fixed try { - def jsCompilerType = Class.forName("org.jetbrains.kotlin.gradle.targets.js.JsCompilerType") + def jsCompilerType = Class.forName("org.jetbrains.kotlin.gradle.targets.js.KotlinJsCompilerAttribute") def jsCompilerAttr = Attribute.of("org.jetbrains.kotlin.js.compiler", jsCompilerType) project.dependencies.attributesSchema.attribute(jsCompilerAttr) configurations { From e1221079f37cdf1cab617aff8ca05642b793d176 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Tue, 26 May 2020 13:50:15 +0300 Subject: [PATCH 2/4] Compatibility with 1.3.70 --- gradle/compile-js-multiplatform.gradle | 28 ++++++++++++++++++-------- gradle/test-mocha-js.gradle | 26 +++++++++++++++--------- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/gradle/compile-js-multiplatform.gradle b/gradle/compile-js-multiplatform.gradle index cecdedf3cc..e31a11c75a 100644 --- a/gradle/compile-js-multiplatform.gradle +++ b/gradle/compile-js-multiplatform.gradle @@ -6,10 +6,14 @@ apply from: rootProject.file('gradle/node-js.gradle') kotlin { js { - moduleName = project.name - nodejs() - irTarget.compilations['main']?.dependencies { - api "org.jetbrains.kotlinx:atomicfu-js:$atomicfu_version" + if (hasProperty("moduleName")) { + moduleName = project.name + } + if (hasProperty("irTarget") && irTarget != null) { + irTarget.nodejs() + irTarget.compilations['main']?.dependencies { + api "org.jetbrains.kotlinx:atomicfu-js:$atomicfu_version" + } } } @@ -27,7 +31,15 @@ kotlin { // When source sets are configured apply from: rootProject.file('gradle/test-mocha-js.gradle') -compileKotlinJsLegacy { +def compileJsLegacy = tasks.hasProperty("compileKotlinJsLegacy") + ? compileKotlinJsLegacy + : compileKotlinJs + +def compileTestJsLegacy = tasks.hasProperty("compileTestKotlinJsLegacy") + ? compileTestKotlinJsLegacy + : compileTestKotlinJs + +compileJsLegacy.configure { kotlinOptions.metaInfo = true kotlinOptions.sourceMap = true kotlinOptions.moduleKind = 'umd' @@ -39,21 +51,21 @@ compileKotlinJsLegacy { } } -compileTestKotlinJsLegacy { +compileTestJsLegacy.configure { kotlinOptions.metaInfo = true kotlinOptions.sourceMap = true kotlinOptions.moduleKind = 'umd' } -task populateNodeModules(type: Copy, dependsOn: compileTestKotlinJsLegacy) { +task populateNodeModules(type: Copy, dependsOn: compileTestJsLegacy) { // we must copy output that is transformed by atomicfu from(kotlin.js().compilations.main.output.allOutputs) into "$node.nodeModulesDir/node_modules" def configuration = configurations.hasProperty("jsLegacyTestRuntimeClasspath") ? configurations.jsLegacyTestRuntimeClasspath - : configurations.jsIrTestRuntimeClasspath + : configurations.jsTestRuntimeClasspath from(files { configuration.collect { File file -> diff --git a/gradle/test-mocha-js.gradle b/gradle/test-mocha-js.gradle index d630422099..6676dc9268 100644 --- a/gradle/test-mocha-js.gradle +++ b/gradle/test-mocha-js.gradle @@ -12,14 +12,22 @@ task installDependenciesMochaNode(type: NpmTask, dependsOn: [npmInstall]) { if (project.hasProperty("teamcity")) args += ["mocha-teamcity-reporter@$mocha_teamcity_reporter_version"] } +def compileJsLegacy = tasks.hasProperty("compileKotlinJsLegacy") + ? compileKotlinJsLegacy + : compileKotlinJs + +def compileTestJsLegacy = tasks.hasProperty("compileTestKotlinJsLegacy") + ? compileTestKotlinJsLegacy + : compileTestKotlinJs + // todo: use atomicfu-transformed test files here (not critical) -task testMochaNode(type: NodeTask, dependsOn: [compileTestKotlinJsLegacy, installDependenciesMochaNode]) { +task testMochaNode(type: NodeTask, dependsOn: [compileTestJsLegacy, installDependenciesMochaNode]) { script = file("$node.nodeModulesDir/node_modules/mocha/bin/mocha") - args = [compileTestKotlinJsLegacy.outputFile, '--require', 'source-map-support/register'] + args = [compileTestJsLegacy.outputFile, '--require', 'source-map-support/register'] if (project.hasProperty("teamcity")) args += ['--reporter', 'mocha-teamcity-reporter'] } -def jsLegacyTestTask = project.tasks.findByName('jsLegacyTest') ? jsLegacyTest : jsIrTest +def jsLegacyTestTask = project.tasks.findByName('jsLegacyTest') ? jsLegacyTest : jsTest jsLegacyTestTask.dependsOn testMochaNode @@ -38,7 +46,7 @@ task installDependenciesMochaChrome(type: NpmTask, dependsOn: [npmInstall]) { def mochaChromeTestPage = file("$buildDir/test-page.html") -task prepareMochaChrome(dependsOn: [compileTestKotlinJsLegacy, installDependenciesMochaChrome]) { +task prepareMochaChrome(dependsOn: [compileTestJsLegacy, installDependenciesMochaChrome]) { outputs.file(mochaChromeTestPage) } @@ -56,8 +64,8 @@ prepareMochaChrome.doLast { - - + + @@ -66,7 +74,7 @@ prepareMochaChrome.doLast { task testMochaChrome(type: NodeTask, dependsOn: prepareMochaChrome) { script = file("$node.nodeModulesDir/node_modules/mocha-headless-chrome/bin/start") - args = [compileTestKotlinJsLegacy.outputFile, '--file', mochaChromeTestPage] + args = [compileTestJsLegacy.outputFile, '--file', mochaChromeTestPage] if (project.hasProperty("teamcity")) args += ['--reporter', 'mocha-teamcity-reporter'] } @@ -85,9 +93,9 @@ task installDependenciesMochaJsdom(type: NpmTask, dependsOn: [npmInstall]) { if (project.hasProperty("teamcity")) args += ["mocha-teamcity-reporter@$mocha_teamcity_reporter_version"] } -task testMochaJsdom(type: NodeTask, dependsOn: [compileTestKotlinJsLegacy, installDependenciesMochaJsdom]) { +task testMochaJsdom(type: NodeTask, dependsOn: [compileTestJsLegacy, installDependenciesMochaJsdom]) { script = file("$node.nodeModulesDir/node_modules/mocha/bin/mocha") - args = [compileTestKotlinJsLegacy.outputFile, '--require', 'source-map-support/register', '--require', 'jsdom-global/register'] + args = [compileTestJsLegacy.outputFile, '--require', 'source-map-support/register', '--require', 'jsdom-global/register'] if (project.hasProperty("teamcity")) args += ['--reporter', 'mocha-teamcity-reporter'] } From 2f9234923605bcbfc054759881c1c688aeba5736 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Tue, 26 May 2020 15:02:21 +0300 Subject: [PATCH 3/4] Fix check for irTarget --- gradle/compile-js-multiplatform.gradle | 4 ++-- gradle/publish-npm-js.gradle | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/gradle/compile-js-multiplatform.gradle b/gradle/compile-js-multiplatform.gradle index e31a11c75a..ea36783644 100644 --- a/gradle/compile-js-multiplatform.gradle +++ b/gradle/compile-js-multiplatform.gradle @@ -6,10 +6,10 @@ apply from: rootProject.file('gradle/node-js.gradle') kotlin { js { - if (hasProperty("moduleName")) { + if (it.hasProperty("moduleName")) { moduleName = project.name } - if (hasProperty("irTarget") && irTarget != null) { + if (it.hasProperty("irTarget") && it.irTarget != null) { irTarget.nodejs() irTarget.compilations['main']?.dependencies { api "org.jetbrains.kotlinx:atomicfu-js:$atomicfu_version" diff --git a/gradle/publish-npm-js.gradle b/gradle/publish-npm-js.gradle index d427c3d3ee..8e1a7bb5ac 100644 --- a/gradle/publish-npm-js.gradle +++ b/gradle/publish-npm-js.gradle @@ -20,6 +20,10 @@ def npmDeployDir = file("$buildDir/npm") def authToken = prop("kotlin.npmjs.auth.token", "") def dryRun = prop("dryRun", "false") +def jsLegacy = kotlin.targets.hasProperty("jsLegacy") + ? kotlin.targets.jsLegacy + : kotlin.targets.js + // Note: publish transformed files using dependency on sourceSets.main.output task preparePublishNpm(type: Copy) { from(npmTemplateDir) { @@ -30,7 +34,7 @@ task preparePublishNpm(type: Copy) { } } // we must publish output that is transformed by atomicfu - from(kotlin.js().compilations.main.output.allOutputs) + from(jsLegacy.compilations.main.output.allOutputs) into npmDeployDir } From 5a6e0aae1a8642807227eecbaf3569d689615005 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Thu, 28 May 2020 13:13:35 +0300 Subject: [PATCH 4/4] Add explanations of conditional setting properties on JS target --- gradle/compile-js-multiplatform.gradle | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gradle/compile-js-multiplatform.gradle b/gradle/compile-js-multiplatform.gradle index ea36783644..93d371a21f 100644 --- a/gradle/compile-js-multiplatform.gradle +++ b/gradle/compile-js-multiplatform.gradle @@ -6,9 +6,17 @@ apply from: rootProject.file('gradle/node-js.gradle') kotlin { js { + // In 1.3.7x js() has not member `moduleName` + // In 1.4.x it has and allow to safety set compiler output file name and does not break test integration if (it.hasProperty("moduleName")) { moduleName = project.name } + + // In 1.3.7x js() has not member `irTarget` + // In 1.4.x it has in `both` and `legacy` mode and js() is of type `KotlinJsTarget` + // `irTarget` is non-null in `both` mode + // and contains appropriate `irTarget` with type `KotlinJsIrTarget` + // `irTarget` is null in `legacy` mode if (it.hasProperty("irTarget") && it.irTarget != null) { irTarget.nodejs() irTarget.compilations['main']?.dependencies {