WebWorkers problem in Angular related to Vite `optimizeDeps`
Command
serve
Is this a regression?
- [ ] Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was
No response
Description
We have a library that utilizes web workers. It employs the new Worker interface:
const instance = new Worker(
new URL('./decodeImageFrameWorker.js', import.meta.url),
{ type: 'module' }
);
We have successfully consumed the lib in various scenarios, whether based on Webpack or Vite. See the examples below:
- https://github.com/cornerstonejs/vite-react-cornerstone3d
- https://github.com/cornerstonejs/vue-cornerstone3d
- https://github.com/cornerstonejs/nextjs-cornerstone3d
When integrating into a Vite-based framework, a warning appears regarding one of the dependencies. This is a known issue in Vite, specifically with esbuild. To resolve this, we should add the library to optimizeDeps in the Vite config - recommended by vite maintainers, which works well.
I attempted to integrate a poc of the same library in Angular and encountered a similar warning. I tried customizing Vite but faced a limitation, as there is no way to override optimizeDeps in Angular, as noted here.
In some threads, I see that setting
"cli": {
"cache": {
"enabled": false
}
}
removes the warning above, but then the worker is requested from an incorrect path.
I also have tried the following
"development": {
"buildTarget": "angular-vite-6:build:development",
"prebundle": false
}
still no success
Minimal Reproduction
This is basically minimal repo for reproduction
- https://github.com/cornerstonejs/angular-cornerstone3d
commands to run
npm i
npm run start
Exception or Error
With the cache enabled, the Vite warning appears, and disabling the cache loads the worker from the wrong path.
Your Environment
Angular CLI: 19.0.2
Node: 18.19.1
Package Manager: npm 10.2.4
OS: darwin arm64
Angular: 19.0.1
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1900.2
@angular-devkit/build-angular 19.0.2
@angular-devkit/core 19.0.2
@angular-devkit/schematics 19.0.2
@angular/cli 19.0.2
@schematics/angular 19.0.2
rxjs 7.8.1
typescript 5.6.3
zone.js 0.15.0
Anything else relevant?
I want to emphasize that other Vite-based implementations for Vue, React, and Quasar work, so it's only Angular that's not working.
In addition, the npm run build also does not work and tries to load the worker from a wrong path (Again the build with vite, and webpack works)
@alan-agius4 could you please look into this bug?
Hey @alan-agius4, have you had a chance to take a look? Many Angular users who utilize Cornerstone3D, an open-source medical imaging library, have encountered this issue. It’s a known challenge within the community, affecting developers working with Angular who aim to integrate Cornerstone3D for medical image viewing and manipulation. Because of that, this issue is blocking our progress.
@alan-agius4 could you please look into this bug?
Hello @alan-agius4 I would be glad if you can give information about this issue. Will a work be done in the short term or can we produce an alternative solution ? We are waiting for this problem to be solved to publish our own application. Thank you for your attention
The really annoying thing is that much of cornerstone works without workers, so for three months everything was going fine. But now we need to show a 3D segmentation surface and the converter (polyseg) uses a worker.
We should have built it with Vue
I see there's a PR in our recipe that seems to fix the issue with another builder, analogjs, but I haven't had a chance to review it yet
A customer reported a similar problem for a (non-public) library of ours. The issue of the missing worker script could be solved by semi-manually adding the scripts via a glob statement in the angular.json. In your project it seems to work with:
diff --git a/angular.json b/angular.json
index 8f1ac30..86f2240 100644
--- a/angular.json
+++ b/angular.json
@@ -24,6 +24,10 @@
{
"glob": "**/*",
"input": "public"
+ },
+ {
+ "glob": "*Worker*",
+ "input": "node_modules/@cornerstonejs/dicom-image-loader/dist/esm"
}
],
"styles": [
@@ -57,6 +61,9 @@
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
+ "options": {
+ "prebundle": false
+ },
"configurations": {
"production": {
"buildTarget": "angular-vite-6:build:production"
In our case this leads to a new problem with the comlink import, which you have too. The worker just silently fails. An error event is sent though when a callback for Worker.onerror is defined. This only happens when external modules are imported in the worker script but not with importing local scripts. Would be interesting if something similar happens in your case.
I am currently working on a minimal reproduction example. This is what I currently have: https://github.com/varfoer/comlink_worker_issues. The worker packages are already build and you should be able to just go ahead and npm i && npm start in the angular folder. The error appears in the console for the second worker that is only having an unused comlink import. I am still figuring out if I am doing something wrong there...
Any updates?
Any updates about this ?
any updates?
Over here at Colorado University also trying to use this with VueJS. The typescript example works but rebuilding without type script is giving me the error others are mentioning here:
FWIW, this repo link has a working typescript vuejs deploy but I'm rebuilding from scratch without type script, and with npm versus yarn to make sure I understand everything (adding lines\components 1 by 1):
https://github.com/cornerstonejs/vue-cornerstone3d
I'm close but just can't quite get the sample dicom to render.
I'm not sure this worker thing is the source of my issue but it's hard to get off the ground with cornerstonejs.
@bbearce we already have these documented https://www.cornerstonejs.org/docs/getting-started/vue-angular-react-etc the issue with angular is that it does not let you edit vite config
Feel free to delete my comments if they are adding noise. Apologies in advance :)
My team fixed it by switching the angular compiler to analogjs
There are projects waiting for this change and deciding whether to continue using angular or not. Why haven't we had a single answer for months! I'm sorry about that but it's a really interesting process considering the size of the angular community. Any updates?
Also having this issue with a different library. Would love to see an update on this from the Angular team.
Is there any news ? I would like to stop using analogJS to fix this issue. Maybe with angular 20 ? Didn't find any change for the configuration of vite.
Hello everyone! Any news about this issue? Is it really necessary to use a diferente builder?