Skip to content

Commit fcaa6df

Browse files
committed
Migrate examples to new JS plugin
* General cleanup and TODOs Co-authored with: [email protected]
1 parent 964cd92 commit fcaa6df

File tree

16 files changed

+84
-128
lines changed

16 files changed

+84
-128
lines changed

build.gradle

+8-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,14 @@ configure(subprojects.findAll { it.name != coreModule && it.name != rootModule }
252252
}
253253

254254
// Redefine source sets because we are not using 'kotlin/main/fqn' folder convention
255-
configure(subprojects.findAll { !sourceless.contains(it.name) && it.name != "benchmarks" && it.name != 'example-frontend-js' && it.name != coreModule }) {
255+
configure(subprojects.findAll {
256+
!sourceless.contains(it.name) &&
257+
it.name != "benchmarks" &&
258+
it.name != coreModule &&
259+
it.name != "example-frontend-js"
260+
}) {
261+
// Pure JS and pure MPP doesn't have this notion and are configured separately
262+
// TODO detect it via platformOf and migrate benchmarks to the same scheme
256263
sourceSets {
257264
main.kotlin.srcDirs = ['src']
258265
test.kotlin.srcDirs = ['test']

buildSrc/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ kotlinDslPluginOptions {
1414
experimentalWarning.set(false)
1515
}
1616

17-
val props = Properties().apply {
17+
private val props = Properties().apply {
1818
file("../gradle.properties").inputStream().use { load(it) }
1919
}
2020

buildSrc/src/main/kotlin/Dokka.kt

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import org.jetbrains.dokka.gradle.DokkaTask
1010
import java.io.File
1111
import java.net.URL
1212

13+
/**
14+
* Package-list by external URL for documentation generation.
15+
*/
1316
fun Project.externalDocumentationLink(
1417
url: String,
1518
packageList: File = projectDir.resolve("package.list")

buildSrc/src/main/kotlin/MavenCentral.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.gradle.api.Project
88
import org.gradle.api.publish.maven.MavenPom
99

10-
// --------------- pom configuration ---------------
10+
// Pom configuration
1111

1212
fun MavenPom.configureMavenCentralMetadata(project: Project) {
1313
name by project.name

buildSrc/src/main/kotlin/Platform.kt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import org.gradle.api.Project
22

3+
// Use from Groovy for now
34
fun platformOf(project: Project): String =
45
when (project.name.substringAfterLast("-")) {
56
"js" -> "js"

buildSrc/src/main/kotlin/RunR8.kt

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import org.gradle.kotlin.dsl.get
77
import org.gradle.kotlin.dsl.named
88
import java.io.File
99

10+
/*
11+
* Task used by our ui/android tests to test minification results
12+
* and keep track of size of the binary.
13+
* TODO move back to kotlinx-coroutines-android when it's migrated to the kts
14+
*/
1015
open class RunR8 : JavaExec() {
1116

1217
@OutputDirectory

buildSrc/src/main/kotlin/UnpackAar.kt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.nio.file.Files
99
import java.util.zip.ZipEntry
1010
import java.util.zip.ZipFile
1111

12+
// TODO move back to kotlinx-coroutines-play-services when it's migrated to the kts
1213
@Suppress("UnstableApiUsage")
1314
abstract class UnpackAar : TransformAction<TransformParameters.None> {
1415
@get:InputArtifact

gradle/compile-js.gradle

+14-16
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,22 @@
66

77
apply plugin: 'org.jetbrains.kotlin.js'
88

9-
kotlin.sourceSets {
10-
main.kotlin.srcDirs = ['src']
11-
test.kotlin.srcDirs = ['test']
12-
main.resources.srcDirs = ['resources']
13-
test.resources.srcDirs = ['test-resources']
9+
dependencies {
10+
implementation "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
11+
testImplementation "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
1412
}
1513

16-
dependencies {
17-
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
18-
testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
14+
kotlin {
15+
js(LEGACY) {
16+
moduleName = project.name - "-js"
17+
}
18+
19+
sourceSets {
20+
main.kotlin.srcDirs = ['src']
21+
test.kotlin.srcDirs = ['test']
22+
main.resources.srcDirs = ['resources']
23+
test.resources.srcDirs = ['test-resources']
24+
}
1925
}
2026

2127
tasks.withType(compileKotlinJs.getClass()) {
@@ -25,11 +31,3 @@ tasks.withType(compileKotlinJs.getClass()) {
2531
metaInfo = true
2632
}
2733
}
28-
29-
compileKotlinJs {
30-
kotlinOptions {
31-
// drop -js suffix from outputFile
32-
def baseName = project.name - "-js"
33-
outputFile = new File(outputFile.parent, baseName + ".js")
34-
}
35-
}

js/example-frontend-js/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
Build application with
44

55
```
6-
gradlew :example-frontend-js:bundle
6+
gradlew :example-frontend-js:build
77
```
88

99
The resulting application can be found in `build/dist` subdirectory.
1010

1111
You can start application with webpack-dev-server using:
1212

1313
```
14-
gradlew :example-frontend-js:start
14+
gradlew :example-frontend-js:run
1515
```
1616

1717
Built and deployed application is available at the library documentation site

js/example-frontend-js/build.gradle

+26-22
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,32 @@
22
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
apply plugin: 'kotlin-dce-js'
6-
apply from: rootProject.file('gradle/node-js.gradle')
7-
8-
dependencies {
9-
compile "org.jetbrains.kotlinx:kotlinx-html-js:$html_version"
10-
}
11-
12-
compileKotlinJs {
13-
kotlinOptions {
14-
main = "call"
5+
project.kotlin {
6+
js(LEGACY) {
7+
binaries.executable()
8+
browser {
9+
distribution {
10+
directory = new File(directory.parentFile, "dist")
11+
}
12+
webpackTask {
13+
cssSupport.enabled = true
14+
}
15+
runTask {
16+
cssSupport.enabled = true
17+
}
18+
testTask {
19+
useKarma {
20+
useChromeHeadless()
21+
webpackConfig.cssSupport.enabled = true
22+
}
23+
}
24+
}
1525
}
16-
}
1726

18-
task bundle(type: NpmTask, dependsOn: [npmInstall, build]) {
19-
inputs.files(fileTree("$buildDir/kotlin-js-min/main"))
20-
inputs.files(fileTree("$buildDir/kotlin-js-min/legacy/main"))
21-
inputs.files(fileTree(file("src/main/web")))
22-
inputs.file("npm/webpack.config.js")
23-
outputs.dir("$buildDir/dist")
24-
args = ["run", "bundle"]
25-
}
26-
27-
task start(type: NpmTask, dependsOn: bundle) {
28-
args = ["run", "start"]
27+
sourceSets {
28+
main.dependencies {
29+
implementation "org.jetbrains.kotlinx:kotlinx-html-js:$html_version"
30+
implementation(npm("html-webpack-plugin", "3.2.0"))
31+
}
32+
}
2933
}

js/example-frontend-js/npm/package.json

-22
This file was deleted.

js/example-frontend-js/npm/webpack.config.js

-54
This file was deleted.

js/example-frontend-js/src/ExampleMain.kt

+3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import kotlin.coroutines.*
1313
import kotlin.math.*
1414
import kotlin.random.Random
1515

16+
external fun require(resource: String)
17+
1618
fun main() {
19+
require("style.css")
1720
println("Starting example application...")
1821
document.addEventListener("DOMContentLoaded", {
1922
Application().start()

js/example-frontend-js/src/main/web/main.js

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
;(function (config) {
6+
const HtmlWebpackPlugin = require('html-webpack-plugin');
7+
8+
config.output.filename = "[name].bundle.js"
9+
10+
config.plugins.push(
11+
new HtmlWebpackPlugin({
12+
title: 'Kotlin Coroutines JS Example'
13+
})
14+
)
15+
16+
// path from <root-build>/js/packages/example-frontend-js to src/main/web
17+
config.resolve.modules.push("../../../../js/example-frontend-js/src/main/web");
18+
})(config);

site/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ val copyExampleFrontendJs by tasks.registering(Copy::class) {
2626
from("$srcBuildDir/dist")
2727
into("$buildDocsDir/example-frontend-js")
2828

29-
dependsOn(":example-frontend-js:bundle")
29+
dependsOn(":example-frontend-js:browserDistribution")
3030
}
3131

3232
tasks.register<Exec>("site") {

0 commit comments

Comments
 (0)