diff --git a/.gitignore b/.gitignore index 2e282ec..a64a68f 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,7 @@ node_modules/ .cargo-ok /index.js /build/ +.idea +.gradle +.DS_Store +kotlin-js-store \ No newline at end of file diff --git a/README.md b/README.md index 7cbd11a..d30f247 100644 --- a/README.md +++ b/README.md @@ -2,27 +2,26 @@ 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 -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). +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). -#### Wrangler +## Wrangler -To generate using [wrangler](https://github.com/cloudflare/wrangler) - -``` -wrangler generate projectname https://github.com/cloudflare/kotlin-worker-hello-world -``` +Configure the [wrangler.toml](wrangler.toml) by filling in the `account_id` from the Workers pages of your Cloudflare Dashboard. Further documentation for Wrangler can be found [here](https://developers.cloudflare.com/workers/tooling/wrangler). -#### Gradle +## Gradle After setting up Kotlin per the linked instructions above, ``` -cd projectname -./gradlew buildWorker +./gradlew :compileProductionExecutableKotlinJs ``` -That will compile your code and package it into index.js, after which you can run `wrangler publish` to push it to Cloudflare. +That will compile your code and package it into a JavaScript executable, after which you can run `wrangler publish` to push it to Cloudflare. + +``` +wrangler publish build/js/packages/kotlin-worker-hello-world/kotlin/kotlin-worker-hello-world.js +``` 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) diff --git a/build.gradle.kts b/build.gradle.kts index ec8d98d..e31f80e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - id("org.jetbrains.kotlin.js") version "1.3.72" + kotlin("js") version "1.7.10" } group = "org.example" @@ -9,19 +9,10 @@ repositories { mavenCentral() } -dependencies { - implementation(kotlin("stdlib-js")) - testImplementation(kotlin("test-js")) -} - -kotlin.target.browser { -} - - -tasks.register("buildWorker") { - dependsOn("browserProductionWebpack") - doLast { - /* Kotlin js output assumes window exists, which it won't on Workers. Hack around it */ - file("$projectDir/index.js").writeText("const window=this; " + file("$projectDir/build/distributions/${rootProject.name}.js").readText()) +kotlin { + js(IR) { + nodejs { + } + binaries.executable() } } diff --git a/gradle.properties b/gradle.properties index 29e08e8..ac01301 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1,2 @@ -kotlin.code.style=official \ No newline at end of file +kotlin.code.style=official +kotlin.js.ir.output.granularity=whole-program \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 1b16c34..ae04661 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/package.json b/package.json index bbaea97..82ba47e 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "{{ project-name }}", + "name": "kotlin-worker-hello-world", "version": "1.0.0", "description": "Kotlin hello world for Cloudflare Workers", "main": "index.js", @@ -7,7 +7,5 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "author": "{{ authors }}", - "license": "MIT", - "devDependencies": { - } + "license": "MIT" } diff --git a/settings.gradle b/settings.gradle deleted file mode 100644 index 93cacb8..0000000 --- a/settings.gradle +++ /dev/null @@ -1,2 +0,0 @@ -rootProject.name = '{{ project-name }}' - diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/main.kt index 9a3db25..dd8d0a6 100644 --- a/src/main/kotlin/main.kt +++ b/src/main/kotlin/main.kt @@ -1,21 +1,14 @@ +import org.w3c.fetch.Request import org.w3c.fetch.Response import org.w3c.fetch.ResponseInit -import org.w3c.workers.FetchEvent -import kotlin.js.Promise -external fun addEventListener(type: String, f: (FetchEvent) -> Unit) - -fun main() { - addEventListener("fetch") { event: FetchEvent -> - val headers: dynamic = object {} - headers["content-type"] = "text/plain" - event.respondWith( - Promise.resolve( - Response( - "Kotlin Worker hello world", - ResponseInit(headers = headers) - ) - ) - ) - } +@OptIn(ExperimentalJsExport::class) +@JsExport +fun fetch(request: Request) : Response { + val headers: dynamic = object {} + headers["content-type"] = "text/plain" + return Response( + "Kotlin Worker hello world", + ResponseInit(headers = headers) + ) } diff --git a/src/main/resources/index.html b/src/main/resources/index.html index d3be73f..f6c24ff 100644 --- a/src/main/resources/index.html +++ b/src/main/resources/index.html @@ -2,7 +2,7 @@ - {{ project-name }} + kotlin-worker-hello-world diff --git a/wrangler.toml b/wrangler.toml index 5ea5aef..64f5f08 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -1,6 +1,4 @@ name = "" -type = "javascript" account_id = "" workers_dev = true -route = "" -zone_id = "" \ No newline at end of file +compatibility_date = "2022-08-11" \ No newline at end of file