angular / angular/angular-cli

WebWorkers problem in Angular related to Vite `optimizeDeps`

Aperta
#28,994 23 commenti 24 reazioni 0 assegnatari Vedi su GitHub
angular/build:application area: @angular/build freq1: low severity3: broken type: bug/fix
Lingua principale
TypeScript
Stelle
27k
Fork
11.8k
Merge medio
14h 23m
PR unite (30g)
162

Descrizione

### 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](https://github.com/cornerstonejs/cornerstone3D/) that utilizes web workers. It employs [the new Worker interface](https://github.com/cornerstonejs/cornerstone3D/blob/main/packages/dicomImageLoader/src/init.ts#L7-L11):

```js
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](https://github.com/vitejs/vite/issues/8427), 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](https://github.com/cornerstonejs/angular-cornerstone3d) 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](https://github.com/angular/angular-cli/issues/26859).

![Image](https://github.com/user-attachments/assets/b101e1de-a645-4248-9e89-196cd3a04aa7)
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](https://github.com/user-attachments/assets/22786cdf-ae02-4c19-9a6a-c0fed89da210)

I also have tried the following

```js
"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

```text
With the cache enabled, the Vite warning appears, and disabling the cache loads the worker from the wrong path.
```

### Your Environment

```text
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](https://github.com/cornerstonejs/vue-cornerstone3d), [React](https://github.com/cornerstonejs/vite-react-cornerstone3d), and [Quasar](https://github.com/sedghi/quasar-cornerstone3D) 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](https://github.com/user-attachments/assets/2bd562aa-a0bf-46cf-ac49-0146c67c769d)

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Clone the linked cornerstonejs/angular-cornerstone3d reproduction and run npm i followed by npm run start; compare its Angular setup with the working Vite examples. Investigate Angular CLI handling of Vite optimizeDeps, caching, and module workers, then verify that the warning is resolved, the worker uses the correct path, and npm run build succeeds.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript, vite
Ambito
build-system, tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.