Skip to content

Adopt Kotlin/JS for 1.4-M2 #2060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions gradle/compile-js-multiplatform.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
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 {
if (it.hasProperty("moduleName")) {
moduleName = project.name
}
if (it.hasProperty("irTarget") && it.irTarget != null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when writing non-trivial construct please drop a comment why it is needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

irTarget.nodejs()
irTarget.compilations['main']?.dependencies {
api "org.jetbrains.kotlinx:atomicfu-js:$atomicfu_version"
}
}
}
Expand All @@ -32,7 +31,15 @@ kotlin {
// When source sets are configured
apply from: rootProject.file('gradle/test-mocha-js.gradle')

compileKotlinJs {
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'
Expand All @@ -44,20 +51,20 @@ compileKotlinJs {
}
}

compileTestKotlinJs {
compileTestJsLegacy.configure {
kotlinOptions.metaInfo = true
kotlinOptions.sourceMap = true
kotlinOptions.moduleKind = 'umd'
}


task populateNodeModules(type: Copy, dependsOn: compileTestKotlinJs) {
task populateNodeModules(type: Copy, dependsOn: compileTestJsLegacy) {
// 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
def configuration = configurations.hasProperty("jsLegacyTestRuntimeClasspath")
? configurations.jsLegacyTestRuntimeClasspath
: configurations.jsTestRuntimeClasspath

from(files {
Expand Down
6 changes: 5 additions & 1 deletion gradle/publish-npm-js.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -30,7 +34,7 @@ task preparePublishNpm(type: Copy) {
}
}
// we must publish output that is transformed by atomicfu
from(kotlin.targets.js.compilations.main.output.allOutputs)
from(jsLegacy.compilations.main.output.allOutputs)
into npmDeployDir
}

Expand Down
30 changes: 19 additions & 11 deletions gradle/test-mocha-js.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ 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: [compileTestKotlinJs, installDependenciesMochaNode]) {
task testMochaNode(type: NodeTask, dependsOn: [compileTestJsLegacy, installDependenciesMochaNode]) {
script = file("$node.nodeModulesDir/node_modules/mocha/bin/mocha")
args = [compileTestKotlinJs.outputFile, '--require', 'source-map-support/register']
args = [compileTestJsLegacy.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 : jsTest

legacyjsTestTask.dependsOn testMochaNode
jsLegacyTestTask.dependsOn testMochaNode

// -- Testing with Mocha under headless Chrome

Expand All @@ -38,7 +46,7 @@ task installDependenciesMochaChrome(type: NpmTask, dependsOn: [npmInstall]) {

def mochaChromeTestPage = file("$buildDir/test-page.html")

task prepareMochaChrome(dependsOn: [compileTestKotlinJs, installDependenciesMochaChrome]) {
task prepareMochaChrome(dependsOn: [compileTestJsLegacy, installDependenciesMochaChrome]) {
outputs.file(mochaChromeTestPage)
}

Expand All @@ -56,8 +64,8 @@ prepareMochaChrome.doLast {
<script>mocha.setup('bdd');</script>
<script src="$node.nodeModulesDir/node_modules/kotlin/kotlin.js"></script>
<script src="$node.nodeModulesDir/node_modules/kotlin-test/kotlin-test.js"></script>
<script src="$compileKotlinJs.outputFile"></script>
<script src="$compileTestKotlinJs.outputFile"></script>
<script src="$compileJsLegacy.outputFile"></script>
<script src="$compileTestJsLegacy.outputFile"></script>
<script>mocha.run();</script>
</body>
</html>
Expand All @@ -66,7 +74,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 = [compileTestJsLegacy.outputFile, '--file', mochaChromeTestPage]
if (project.hasProperty("teamcity")) args += ['--reporter', 'mocha-teamcity-reporter']
}

Expand All @@ -85,11 +93,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: [compileTestJsLegacy, installDependenciesMochaJsdom]) {
script = file("$node.nodeModulesDir/node_modules/mocha/bin/mocha")
args = [compileTestKotlinJs.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']
}

legacyjsTestTask.dependsOn testMochaJsdom
jsLegacyTestTask.dependsOn testMochaJsdom

2 changes: 1 addition & 1 deletion js/example-frontend-js/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down