Skip to content

Commit

Permalink
webpack/bundler Fix: Update worker JS file import URL to be compatibl…
Browse files Browse the repository at this point in the history
…e with bundlers (#22165)

This PR closes #22140 by updating the URL used to import the worker JS
in `library_pthread.js::allocateUnusedWorker`. Previously the URL would
be:
```js
worker = new Worker(new URL(import.meta.url))
```
which webpack translates to the`file:///` path, causing the import to
fail b/c the `file://` path is not valid to access ([webpack docs import
meta](https://webpack.js.org/blog/2020-10-10-webpack-5-release/#importmeta)).
  • Loading branch information
Twinklebear committed Jul 1, 2024
1 parent 35be995 commit a7b833c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion test/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))

Expand All @@ -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):
Expand Down

0 comments on commit a7b833c

Please sign in to comment.