Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load Workers with {type:'module'} in ESM mode (#20452)
Even in ESM mode, we use dynamic import to load main code from the Web Worker, so, technically, our Workers dont *have* to be ESM as well - they're compatible with regular script mode too. Unfortunately, unlike browsers, some bundlers care about the distinction and don't bundle Workers correctly when `new Worker(...)` is used without `{type: 'module'}`. Some recent examples where I've seen this: - issue with parcel bundler: parcel-bundler/parcel#8727 (comment) - issue with esbuild bundler used by popular vite toolchain: GoogleChromeLabs/web-gphoto2#12 In both cases adding `{type: 'module'}` fixes the build. One difference this change will make is that `{type: 'module'}` forces Worker to be loaded as a module which always runs in strict JS mode. Our main code should already be compatible with `use strict`, or it wouldn't work as ESM at all, and our `src/worker.js` code has `use strict` at the top, so it's compatible too, so I don't expect any issues, but it's something worth keeping in mind if we get breakage reports. Also note that older browsers without Module Workers support will ignore `{type: 'module'}` and run Workers in script mode, so as long as we don't use static imports or `import.meta.url` in `src/worker.js`, this is a backward-compatible change.
- Loading branch information