diff --git a/src/library_pthread.js b/src/library_pthread.js index 414be3e9eea27..79ec001921a8a 100644 --- a/src/library_pthread.js +++ b/src/library_pthread.js @@ -447,13 +447,17 @@ var LibraryPThread = { var p = trustedTypes.createPolicy( 'emscripten#workerPolicy1', { - createScriptURL: (ignored) => new URL(import.meta.url) + createScriptURL: (ignored) => new URL("{{{ TARGET_JS_NAME }}}", import.meta.url) } ); worker = new Worker(p.createScriptURL('ignored'), workerOptions); } else #endif - worker = new Worker(new URL(import.meta.url), workerOptions); + // 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 + // the first case in their bundling step. The latter ends up producing an invalid + // URL to import from the server (e.g., for webpack the file:// path). + worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), workerOptions); #else var pthreadMainJs = _scriptName; #if expectToReceiveOnModule('mainScriptUrlOrBlob') diff --git a/test/common.py b/test/common.py index 0f4e64aa975da..0c3514241efa2 100644 --- a/test/common.py +++ b/test/common.py @@ -1150,7 +1150,7 @@ def add_on_exit(self, code): # libraries, for example def get_emcc_args(self, main_file=False, compile_only=False, asm_only=False): def is_ldflag(f): - return any(f.startswith(s) for s in ['-sEXPORT_ES6', '-sPROXY_TO_PTHREAD', '-sENVIRONMENT=', '--pre-js=', '--post-js=']) + return any(f.startswith(s) for s in ['-sEXPORT_ES6', '-sPROXY_TO_PTHREAD', '-sENVIRONMENT=', '--pre-js=', '--post-js=', '-sPTHREAD_POOL_SIZE=']) args = self.serialize_settings(compile_only or asm_only) + self.emcc_args if asm_only: diff --git a/test/test_browser.py b/test/test_browser.py index e668740a9dafb..273eea9a83f67 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -5548,13 +5548,13 @@ def test_error_reporting(self): def test_webpack(self, es6): if es6: shutil.copytree(test_file('webpack_es6'), 'webpack') - self.emcc_args += ['-sEXPORT_ES6'] + self.emcc_args += ['-sEXPORT_ES6', '-pthread', '-sPTHREAD_POOL_SIZE=1'] outfile = 'src/hello.mjs' else: shutil.copytree(test_file('webpack'), 'webpack') outfile = 'src/hello.js' with utils.chdir('webpack'): - self.compile_btest('hello_world.c', ['-sEXIT_RUNTIME', '-sMODULARIZE', '-sENVIRONMENT=web', '-o', outfile]) + self.compile_btest('hello_world.c', ['-sEXIT_RUNTIME', '-sMODULARIZE', '-sENVIRONMENT=web,worker', '-o', outfile]) self.run_process(shared.get_npm_cmd('webpack') + ['--mode=development', '--no-devtool']) shutil.copyfile('webpack/src/hello.wasm', 'webpack/dist/hello.wasm') self.run_browser('webpack/dist/index.html', '/report_result?exit:0') diff --git a/test/test_other.py b/test/test_other.py index 0a1ee11eff637..4f5433b9eafc3 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -374,7 +374,7 @@ def test_emcc_output_worker_mjs(self, args): test_file('hello_world.c')] + args) src = read_file('subdir/hello_world.mjs') self.assertContained("new URL('hello_world.wasm', import.meta.url)", src) - self.assertContained("new Worker(new URL(import.meta.url), workerOptions)", src) + self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), workerOptions)", src) self.assertContained('export default Module;', src) self.assertContained('hello, world!', self.run_js('subdir/hello_world.mjs')) @@ -386,7 +386,7 @@ def test_emcc_output_worker_mjs_single_file(self): test_file('hello_world.c'), '-sSINGLE_FILE']) src = read_file('hello_world.mjs') self.assertNotContained("new URL('data:", src) - self.assertContained("new Worker(new URL(import.meta.url), workerOptions)", src) + self.assertContained("new Worker(new URL('hello_world.mjs', import.meta.url), workerOptions)", src) self.assertContained('hello, world!', self.run_js('hello_world.mjs')) def test_emcc_output_mjs_closure(self):