Hi! I am trying to implement web workers in my KMP...
# javascript
c
Hi! I am trying to implement web workers in my KMP project and publish it to npm, but I am not sure how to do it.
Copy code
js(IR) {
        browser {
            webpackTask(Action {
                mainOutputFileName.set("js-worker.js")
            })

            @OptIn(org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalDistributionDsl::class)
            distribution(Action {
                distributionName.set("jsWorker")
            })
        }

        binaries.library()
        binaries.executable()
    }
I created another target for worker, so now I have 2.
Copy code
val jsMain by getting {
   resources.srcDirs("./build/jsWorker")
}
I also added the dependency for the jsWorker folder. After build i can see that the js-worker.js file is generated in the
projectRoot/common/build
folder. But I want to publish it to npm, so i need to use the
projectRoot/build
folder. Is it possible somehow to generate the js-worker.js file to the
projectRoot/build
folder? Because if my guess is correct, the worker file wont be included, when I publish the project to npm.
a
@Ilya Goncharov [JB] do you know how to do it?
For the Kotlin team, note how the article's code uses two JS targets. This won't be available anymore if https://youtrack.jetbrains.com/issue/KT-59316/ is considered valid also for the JS target.
plus1 1
c
@Edoardo Luppi Yes I saw this, but it is not exported to npm, right? So I think the bundle generated by ./gradlew jsBrowserProductionWebpack, won't contain the js worker file. Or am I missing something?
Screenshot 2023-11-21 at 13.15.26.png
e
The article's author wires the web worker build to the front-end development run task, which produces the output under
build/frontend
. You can obviously wire up tasks that are needed in your specific case, or simply copy distribution files to a common directory
Your use case seems slightly different so your best bet is to experiment with the Gradle DSL configuration.
I think it's totally possible. Need to find out which tasks should depend on which others, and how to organize built artifacts.