If I have a wasmJs app where I want to write a JS ...
# multiplatform
w
If I have a wasmJs app where I want to write a JS service worker, but in Kotlin, does anyone know how I'd set that up? From what I can tell, there's no implicit dependency from wasmJs to the js source set, and I'm not sure how I could tell the wasmJs target to compile a specific .kt file into JS. I guess I could make the wasmJs build task also build the js target and have a build step copy the output into the wasm distribution?
I'm still very new to webdev, so I have NOT fully validated this, but the solution from https://avwie.github.io/multithreaded-web-applications-in-kotlin-with-web-workers seems to work. Basically, split
commonMain
into
nonJsCommonMain
for the actual app, then create a
js("worker")
target, and attach
wasmJsProcessResources
dependsOn
workerBrowserDistribution
along with
wasmJsMain.dependencies.resources.srcDirs(layout.buildDirectory.dir("dist/worker/productionExecutable")
. Then in an init/app .js file, you can register the service worker pointing to the JS file
navigator.serviceWorker.register("worker.js")
. For actually handling the service events, creating a
fun main()
inside the
Worker.kt
file seems to work and gets executed when the worker is created.