diff --git a/src/lib/libpthread.js b/src/lib/libpthread.js
index 030fb4f6b98a7..83f0501a52f46 100644
--- a/src/lib/libpthread.js
+++ b/src/lib/libpthread.js
@@ -445,6 +445,16 @@ var LibraryPThread = {
}
worker = new Worker(pthreadMainJs, {{{ pthreadWorkerOptions }}});
} else
+#endif
+#if CROSS_ORIGIN && ENVIRONMENT_MAY_BE_WEB
+ // Support cross-origin loading by creating a new Blob URL to actually
+ // perform the `import`. Without this the `new Worker` would fail
+ // due to CORS restrictions.
+ // https://github.com/emscripten-core/emscripten/issues/21937
+ if (ENVIRONMENT_IS_WEB) {
+ var url = URL.createObjectURL(new Blob([`import '${import.meta.url}'`], { type: 'application/javascript' }));
+ worker = new Worker(url, {{{ pthreadWorkerOptions }}});
+ } else
#endif
// We need to generate the URL with import.meta.url as the base URL of the JS file
// instead of just using new URL(import.meta.url) because bundler's only recognize
diff --git a/test/test_browser.py b/test/test_browser.py
index 16808668b985e..d1a873e0744ca 100644
--- a/test/test_browser.py
+++ b/test/test_browser.py
@@ -5679,14 +5679,18 @@ def test_rollup(self):
shutil.copy('hello.wasm', 'dist/')
self.run_browser('index.html', '/report_result?exit:0')
- def test_cross_origin(self):
+ @parameterized({
+ '': ([],),
+ 'es6': (['-sEXPORT_ES6', '--extern-post-js', test_file('modularize_post_js.js')],),
+ })
+ def test_cross_origin(self, args):
# Verfies that the emscripten-generted JS and Wasm can be hosted on a different origin.
# This test create a second HTTP server running on port 9999 that servers files from `subdir`.
# The main html is the servers from the normal 8888 server while the JS and Wasm are hosted
# on at 9999.
os.mkdir('subdir')
create_file('subdir/foo.txt', 'hello')
- self.compile_btest('hello_world.c', ['-o', 'subdir/hello.js', '-sCROSS_ORIGIN', '-sPROXY_TO_PTHREAD', '-pthread', '-sEXIT_RUNTIME'])
+ self.compile_btest('hello_world.c', ['-o', 'subdir/hello.js', '-sRUNTIME_DEBUG', '-sCROSS_ORIGIN', '-sPROXY_TO_PTHREAD', '-pthread', '-sEXIT_RUNTIME'] + args)
class MyReqestHandler(SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
@@ -5707,9 +5711,10 @@ def end_headers(self):
return SimpleHTTPRequestHandler.end_headers(self)
- create_file('test.html', '''
-
- ''')
+ if '-sEXPORT_ES6' in args:
+ create_file('test.html', '')
+ else:
+ create_file('test.html', '')
server = HttpServerThread(ThreadingHTTPServer(('localhost', 9999), MyReqestHandler))
server.start()