Skip to content

Commit 8c574d8

Browse files
committed
Update for Wrangler 2 and Kotlin 1.7.10
This uses the newer Worker syntax and removes some of the bespoke browser work now that Kotlin better supports JavaScript.
1 parent 195e97c commit 8c574d8

File tree

10 files changed

+37
-55
lines changed

10 files changed

+37
-55
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ node_modules/
99
.cargo-ok
1010
/index.js
1111
/build/
12+
.idea
13+
.gradle
14+
.DS_Store
15+
kotlin-js-store

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@
22

33
Your Kotlin code in [main.kt](https://github.com/cloudflare/kotlin-worker-hello-world/blob/master/src/main/kotlin/main.kt), running on Cloudflare Workers
44

5-
In addition to [Wrangler](https://github.com/cloudflare/wrangler) you will need to install Kotlin, including a JDK and support for Gradle projects. The easiest way to do this is using the free Community Edition of [IntelliJ IDEA](https://kotlinlang.org/docs/tutorials/jvm-get-started.html).
5+
In addition to [Wrangler v2.x](https://github.com/cloudflare/wrangler2) you will need to install Kotlin, including a JDK and support for Gradle projects. The easiest way to do this is using the free Community Edition of [IntelliJ IDEA](https://kotlinlang.org/docs/tutorials/jvm-get-started.html).
66

7-
#### Wrangler
7+
## Wrangler
88

9-
To generate using [wrangler](https://github.com/cloudflare/wrangler)
10-
11-
```
12-
wrangler generate projectname https://github.com/cloudflare/kotlin-worker-hello-world
13-
```
9+
Configure the [wrangler.toml](wrangler.toml) by filling in the `account_id` from the Workers pages of your Cloudflare Dashboard.
1410

1511
Further documentation for Wrangler can be found [here](https://developers.cloudflare.com/workers/tooling/wrangler).
1612

17-
#### Gradle
13+
## Gradle
1814

1915
After setting up Kotlin per the linked instructions above,
2016

2117
```
22-
cd projectname
23-
./gradlew buildWorker
18+
./gradlew :compileProductionExecutableKotlinJs
2419
```
2520

26-
That will compile your code and package it into index.js, after which you can run `wrangler publish` to push it to Cloudflare.
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.
22+
23+
```
24+
wrangler publish build/js/packages/kotlin-worker-hello-world/kotlin/kotlin-worker-hello-world.js
25+
```
2726

2827
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)

build.gradle.kts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id("org.jetbrains.kotlin.js") version "1.3.72"
2+
kotlin("js") version "1.7.10"
33
}
44

55
group = "org.example"
@@ -9,19 +9,10 @@ repositories {
99
mavenCentral()
1010
}
1111

12-
dependencies {
13-
implementation(kotlin("stdlib-js"))
14-
testImplementation(kotlin("test-js"))
15-
}
16-
17-
kotlin.target.browser {
18-
}
19-
20-
21-
tasks.register("buildWorker") {
22-
dependsOn("browserProductionWebpack")
23-
doLast {
24-
/* Kotlin js output assumes window exists, which it won't on Workers. Hack around it */
25-
file("$projectDir/index.js").writeText("const window=this; " + file("$projectDir/build/distributions/${rootProject.name}.js").readText())
12+
kotlin {
13+
js(IR) {
14+
nodejs {
15+
}
16+
binaries.executable()
2617
}
2718
}

gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
kotlin.code.style=official
1+
kotlin.code.style=official
2+
kotlin.js.ir.output.granularity=whole-program
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
{
2-
"name": "{{ project-name }}",
2+
"name": "kotlin-worker-hello-world",
33
"version": "1.0.0",
44
"description": "Kotlin hello world for Cloudflare Workers",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
99
"author": "{{ authors }}",
10-
"license": "MIT",
11-
"devDependencies": {
12-
}
10+
"license": "MIT"
1311
}

settings.gradle

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/main/kotlin/main.kt

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1+
import org.w3c.fetch.Request
12
import org.w3c.fetch.Response
23
import org.w3c.fetch.ResponseInit
3-
import org.w3c.workers.FetchEvent
4-
import kotlin.js.Promise
54

6-
external fun addEventListener(type: String, f: (FetchEvent) -> Unit)
7-
8-
fun main() {
9-
addEventListener("fetch") { event: FetchEvent ->
10-
val headers: dynamic = object {}
11-
headers["content-type"] = "text/plain"
12-
event.respondWith(
13-
Promise.resolve(
14-
Response(
15-
"Kotlin Worker hello world",
16-
ResponseInit(headers = headers)
17-
)
18-
)
19-
)
20-
}
5+
@OptIn(ExperimentalJsExport::class)
6+
@JsExport
7+
fun fetch(request: Request) : Response {
8+
val headers: dynamic = object {}
9+
headers["content-type"] = "text/plain"
10+
return Response(
11+
"Kotlin Worker hello world",
12+
ResponseInit(headers = headers)
13+
)
2114
}

src/main/resources/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>{{ project-name }}</title>
5+
<title>kotlin-worker-hello-world</title>
66
<script src="{{ project-name}}.js"></script>
77
</head>
88
<body>

wrangler.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
name = ""
2-
type = "javascript"
32
account_id = ""
43
workers_dev = true
5-
route = ""
6-
zone_id = ""
4+
compatibility_date = "2022-08-11"

0 commit comments

Comments
 (0)