angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

WebWorkers problem in Angular related to Vite `optimizeDeps`

Open sedghi opened this issue 1 year ago • 18 comments

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.

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

Image

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)

Image

sedghi avatar Nov 28 '24 20:11 sedghi

@alan-agius4 could you please look into this bug?

daemon29 avatar Dec 10 '24 18:12 daemon29

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.

ssokmen avatar Dec 16 '24 10:12 ssokmen

@alan-agius4 could you please look into this bug?

zhan2016 avatar Dec 20 '24 03:12 zhan2016

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

lambacini avatar Jan 03 '25 07:01 lambacini

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

johnpage-agixis avatar Jan 29 '25 13:01 johnpage-agixis

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

sedghi avatar Jan 29 '25 14:01 sedghi

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

varfoer avatar Feb 04 '25 15:02 varfoer

Any updates?

zhan2016 avatar Feb 28 '25 13:02 zhan2016

Any updates about this ?

bertrand-median avatar Mar 11 '25 13:03 bertrand-median

any updates?

sepideh4330 avatar Mar 17 '25 11:03 sepideh4330

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:

Image

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.

Image

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 avatar Apr 16 '25 16:04 bbearce

@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

sedghi avatar Apr 16 '25 16:04 sedghi

Feel free to delete my comments if they are adding noise. Apologies in advance :)

bbearce avatar Apr 16 '25 16:04 bbearce

My team fixed it by switching the angular compiler to analogjs

johnpage-agixis avatar Apr 17 '25 07:04 johnpage-agixis

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?

lambacini avatar Apr 22 '25 11:04 lambacini

Also having this issue with a different library. Would love to see an update on this from the Angular team.

chopperdaddy avatar May 18 '25 07:05 chopperdaddy

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.

bertrand-median avatar Jun 04 '25 14:06 bertrand-median

Hello everyone! Any news about this issue? Is it really necessary to use a diferente builder?

marcotech2002 avatar Oct 02 '25 16:10 marcotech2002