Hi all Did anybody succeed in creating a webWorker...
# javascript
p
Hi all Did anybody succeed in creating a webWorker in wasm? The worker code is defined in a sub-project. When compiled to javascript, everything works fine. When compiled to wasmJs, the worker does not start because the wrapper javascript code generated try to reference apis which are not available in the browser. Using kotlin 2.2.21 Any help would be appreciated Pierre
t
code generated try to reference apis which are not available in the browser
Do you have error stacktrace?
The worker code is defined in a sub-project
You should connect Gradle compilation and sync tasks in that case
p
I am not in front of my computer right now but as far as I remember I get a TypeError because the code is trying to access document. And if I manually edit the javascript code to bypass this error, there is a later error when trying to build the url of the wasm code to be loaded with an error 'Invalid base URL'
As for connecting the compilatons we already do that so that the worker js and wasm files are copied to the main UI distribution folder
t
I get a TypeError because the code is trying to access document.
Do you have this error in production mode?
there is a later error when trying to build the url of the wasm code to be loaded with an error 'Invalid base URL'
Is there code example how you create
Worker
?
p
Do you have this error in production mode?
I have not tried this in production yet, will try on monday.
Is there code example how you create Worker ?
We simply do: val worker = Worker('worker-project.js')
t
val worker = Worker('worker-project.js')
For bundler it should be like this:
Copy code
val worker = Worker(URL("worker-project.js", import.meta.url))
p
For bundler it should be like this:
Copy code
val worker = Worker(URL("worker-project.js", import.meta.url))
Thanks, will try that on monday and report the result.
For bundler it should be like this:
Copy code
val worker = Worker(URL("worker-project.js", import.meta.url))
Tried that today but that requires the worker sub-project's output to be embedded in the main UI subproject's produced bundle. Our main UI bundle is already big enough and we would rather not do thst. Will try some alternatives. Thanks anyway for your time.
t
but that requires the worker sub-project's output to be embedded in the main UI subproject's produced bundle
Separate chunk for the worker expected
With name like
345235347458.js
p
After much googling and reading the webpack docs I found the solution. In the worker subproject's webpack.config.d subdirectory I added a config.js file with the following content:
Copy code
config.target = 'webworker';
t
Was it problem in file name?
If yes - you can configure it in Gradle
p
No, it's the configuration of the webpack task: webpack must be instructed to create a js file for use in a WebWorker where document and such apis are not available.
When not providing this option Webpack generates a js file which contains:
Copy code
__webpack_require__.b = document.baseUri || self.location.href;
Whereas, when defining the option, it generates:
Copy code
__webpack_require__.b = self.location + "";
🎉 1
👍 1