Skip to content

Commit c5559f1

Browse files
committed
Update Kotlin 1 -> 2 and support ES Modules
1 parent 0ea3915 commit c5559f1

File tree

11 files changed

+274
-189
lines changed

11 files changed

+274
-189
lines changed

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,18 @@ In addition to [Wrangler v2.x](https://github.com/cloudflare/wrangler2) you will
66

77
## Wrangler
88

9-
Configure the [wrangler.toml](wrangler.toml) by filling in the `account_id` from the Workers pages of your Cloudflare Dashboard.
9+
Documentation for Wrangler can be found [here](https://developers.cloudflare.com/workers/tooling/wrangler).
1010

11-
Further documentation for Wrangler can be found [here](https://developers.cloudflare.com/workers/tooling/wrangler).
12-
13-
## Gradle
14-
15-
After setting up Kotlin per the linked instructions above,
11+
To test the build locally, run it in dev.
1612

17-
```
18-
./gradlew :compileProductionExecutableKotlinJs
13+
```shell
14+
wrangler dev
1915
```
2016

21-
That will compile your code and package it into a JavaScript executable, after which you can run `wrangler publish` to push it to Cloudflare.
17+
To deploy the example, use the following command.
2218

23-
```
24-
wrangler publish build/js/packages/kotlin-worker-hello-world/kotlin/kotlin-worker-hello-world.js
19+
```shell
20+
wrangler deploy
2521
```
2622

27-
For more information on interop between Kotlin and Javascript, see the [Kotlin docs](https://kotlinlang.org/docs/reference/js-interop.html). Regarding coroutines, see [this issue and workaround](https://github.com/cloudflare/kotlin-worker-hello-world/issues/2)
23+
For more information on interop between Kotlin and Javascript, see the [Kotlin docs](https://kotlinlang.org/docs/js-interop.html). Regarding coroutines, see [this issue and workaround](https://github.com/cloudflare/kotlin-worker-hello-world/issues/2)

build.gradle.kts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
2+
13
plugins {
2-
kotlin("js") version "1.7.10"
4+
kotlin("multiplatform") version "2.2.10"
35
}
46

57
group = "org.example"
@@ -10,9 +12,33 @@ repositories {
1012
}
1113

1214
kotlin {
13-
js(IR) {
14-
nodejs {
15-
}
15+
js {
16+
nodejs()
1617
binaries.executable()
1718
}
1819
}
20+
21+
// https://kotlinlang.org/docs/js-project-setup.html#support-for-es2015-features
22+
tasks.withType<KotlinJsCompile>().configureEach {
23+
compilerOptions {
24+
target.set("es2015")
25+
}
26+
}
27+
28+
tasks.named("jsProductionExecutableCompileSync") {
29+
val entrypointFile = "${layout.buildDirectory.asFile.get()}/js/packages/kotlin-worker-hello-world/kotlin/kotlin-worker-hello-world.mjs"
30+
outputs.file(entrypointFile)
31+
32+
val jsEntrypoint = """
33+
// The entrypoint expected by Cloudflare
34+
export default {
35+
async fetch(request, env, ctx) {
36+
return HelloWorker.fetch(request, env, ctx);
37+
},
38+
};
39+
""".trimIndent()
40+
41+
doLast {
42+
File(entrypointFile).appendText(jsEntrypoint)
43+
}
44+
}

gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
kotlin.code.style=official
2-
kotlin.js.ir.output.granularity=whole-program
2+
kotlin.js.ir.output.granularity=whole-program
3+
org.gradle.configuration-cache=true

gradle/wrapper/gradle-wrapper.jar

-12.9 KB
Binary file not shown.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.0.0-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
46
zipStoreBase=GRADLE_USER_HOME
57
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)