Skip to content

Commit cf56736

Browse files
committed
Load Workers with {type:'module'} in ESM mode
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.
1 parent 5b24a09 commit cf56736

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/library_pthread.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,10 @@ var LibraryPThread = {
452452
createScriptURL: (ignored) => new URL('{{{ PTHREAD_WORKER_FILE }}}', import.meta.url);
453453
}
454454
);
455-
worker = new Worker(p.createScriptURL('ignored'));
455+
worker = new Worker(p.createScriptURL('ignored'), {type: 'module'});
456456
} else
457457
#endif
458-
worker = new Worker(new URL('{{{ PTHREAD_WORKER_FILE }}}', import.meta.url));
458+
worker = new Worker(new URL('{{{ PTHREAD_WORKER_FILE }}}', import.meta.url), {type: 'module'});
459459
} else {
460460
#endif
461461
// Allow HTML module to configure the location where the 'worker.js' file will be loaded from,
@@ -568,7 +568,7 @@ var LibraryPThread = {
568568
// Called when a thread needs to be strongly referenced.
569569
// Currently only used for:
570570
// - keeping the "main" thread alive in PROXY_TO_PTHREAD mode;
571-
// - crashed threads that needs to propagate the uncaught exception
571+
// - crashed threads that needs to propagate the uncaught exception
572572
// back to the main thread.
573573
#if ENVIRONMENT_MAY_BE_NODE
574574
if (ENVIRONMENT_IS_NODE) {

test/test_other.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def test_emcc_output_worker_mjs(self, args):
303303
test_file('hello_world.c')] + args)
304304
src = read_file('subdir/hello_world.mjs')
305305
self.assertContained("new URL('hello_world.wasm', import.meta.url)", src)
306-
self.assertContained("new Worker(new URL('hello_world.worker.js', import.meta.url))", src)
306+
self.assertContained("new Worker(new URL('hello_world.worker.js', import.meta.url), {type: 'module'})", src)
307307
self.assertContained('export default Module;', src)
308308
src = read_file('subdir/hello_world.worker.js')
309309
self.assertContained("import('./hello_world.mjs')", src)
@@ -317,7 +317,7 @@ def test_emcc_output_worker_mjs_single_file(self):
317317
test_file('hello_world.c'), '-sSINGLE_FILE'])
318318
src = read_file('hello_world.mjs')
319319
self.assertNotContained("new URL('data:", src)
320-
self.assertContained("new Worker(new URL('hello_world.worker.js', import.meta.url))", src)
320+
self.assertContained("new Worker(new URL('hello_world.worker.js', import.meta.url), {type: 'module'})", src)
321321
self.assertContained('hello, world!', self.run_js('hello_world.mjs'))
322322

323323
def test_emcc_output_mjs_closure(self):

0 commit comments

Comments
 (0)