Skip to content

Modernize example with support for ES Modules #5

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 13 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
/dist
.cargo-ok
.DS_Store
.gradle
.idea
.kotlin
**/*.rs.bk
Cargo.lock
/.wrangler
/build/
/dist
/index.js
bin/
build
Cargo.lock
kotlin-js-store
node_modules/
pkg/
wasm-pack.log
worker/
node_modules/
.cargo-ok
/index.js
/build/
.idea
.gradle
.DS_Store
kotlin-js-store
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
# Kotlin hello world for Cloudflare Workers

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
Your Kotlin code in [Application.kt](worker-app/src/jsMain/kotlin/Application.kt), running on Cloudflare Workers.

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

Configure the [wrangler.toml](wrangler.toml) by filling in the `account_id` from the Workers pages of your Cloudflare Dashboard.
You will need to install wrangler, e.g. `npm install wrangler`

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

## Gradle

After setting up Kotlin per the linked instructions above,

```
./gradlew :compileProductionExecutableKotlinJs
./gradlew assemble
```

That will compile your code and package it into a JavaScript executable, 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 dev` to start a localhost server for testing.

```
wrangler publish build/js/packages/kotlin-worker-hello-world/kotlin/kotlin-worker-hello-world.js
```
To publish to Cloudflare, run `wrangler publish`

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)
25 changes: 15 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension

plugins {
kotlin("js") version "1.7.10"
alias(libs.plugins.kotlin.multiplatform) apply false
}

group = "org.example"
version = "1.0-SNAPSHOT"
rootProject.plugins.withType(org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin::class.java) {
rootProject.the<NodeJsRootExtension>().versions.webpackCli.version = libs.versions.webpack.cli.get()
}

repositories {
mavenCentral()
rootProject.plugins.withType<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin> {
rootProject.the<NodeJsRootExtension>().apply {
download = project.property("CLOUDFLARE_IS_INSTALL_NODE").toString().toBoolean()
nodeVersion = libs.versions.node.get()
}
}

kotlin {
js(IR) {
nodejs {
}
binaries.executable()
rootProject.plugins.withType<org.jetbrains.kotlin.gradle.targets.js.yarn.YarnPlugin> {
rootProject.the<YarnRootExtension>().apply {
download = project.property("CLOUDFLARE_IS_INSTALL_YARN").toString().toBoolean()
}
}
15 changes: 13 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
kotlin.code.style=official
kotlin.js.ir.output.granularity=whole-program
org.gradle.caching=true
org.gradle.configuration-cache=true
org.gradle.configureondemand=true
org.gradle.kotlin.dsl.allWarningsAsErrors=true
org.gradle.parallel=true
org.gradle.vfs.watch=true

kotlin.build.report.output=file

# Configures whether Node and Yarn are installed by the Kotlin build scripts
# Can be disabled on CI or an environment where these are known to be pre-installed
CLOUDFLARE_IS_INSTALL_NODE=true
CLOUDFLARE_IS_INSTALL_YARN=true
15 changes: 15 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[versions]
kotlin = "1.9.22"
kotlinx-coroutines = "1.7.3"
miniflare = "3.20231218.4"
node = "20.11.0"
webpack-cli = "5.1.4"

[libraries]
kotlin-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-debug = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-debug", version.ref = "kotlinx-coroutines" }
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" }

[plugins]
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 4 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
distributionSha256Sum=9d926787066a081739e8200858338b4a69e837c3a821a33aca9db09dd4a41026
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading