Skip to content

Commit

Permalink
Ensure compatibility with Vite
Browse files Browse the repository at this point in the history
This revises commit a7b833c.
  • Loading branch information
kleisauke committed Sep 11, 2024
1 parent 1c7ced2 commit ba48abd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
20 changes: 9 additions & 11 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,26 +425,24 @@ var LibraryPThread = {
};
#if EXPORT_ES6 && USE_ES6_IMPORT_META
// If we're using module output, use bundler-friendly pattern.
// 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).
var workerUrl = new URL("{{{ TARGET_JS_NAME }}}", import.meta.url);
#if PTHREADS_DEBUG
dbg('Allocating a new web worker from ' + import.meta.url);
#endif
#if TRUSTED_TYPES
// Use Trusted Types compatible wrappers.
if (typeof trustedTypes != 'undefined' && trustedTypes.createPolicy) {
var p = trustedTypes.createPolicy(
'emscripten#workerPolicy1',
{
createScriptURL: (ignored) => new URL("{{{ TARGET_JS_NAME }}}", import.meta.url)
}
);
var p = trustedTypes.createPolicy('emscripten#workerPolicy1', {
createScriptURL: (ignored) => workerUrl
});
worker = new Worker(p.createScriptURL('ignored'), workerOptions);
} 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
// 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);
worker = new Worker(workerUrl, workerOptions);
#else
var pthreadMainJs = currentScript;
#if expectToReceiveOnModule('mainScriptUrlOrBlob')
Expand Down
4 changes: 2 additions & 2 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,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('hello_world.mjs', import.meta.url), workerOptions)", src)
self.assertContained("new URL('hello_world.mjs', import.meta.url)", src)
self.assertContained('export default Module;', src)
self.assertContained('hello, world!', self.run_js('subdir/hello_world.mjs'))

Expand All @@ -383,7 +383,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('hello_world.mjs', import.meta.url), workerOptions)", src)
self.assertContained("new URL('hello_world.mjs', import.meta.url)", src)
self.assertContained('hello, world!', self.run_js('hello_world.mjs'))

def test_emcc_output_mjs_closure(self):
Expand Down

0 comments on commit ba48abd

Please sign in to comment.