From ba48abdd7787f6e5147d29fbbb21301f991029bc Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Tue, 23 Jul 2024 09:41:02 +0200 Subject: [PATCH] Ensure compatibility with Vite This revises commit a7b833c0de5a013844c69bd420ca47be75cd27c8. --- src/library_pthread.js | 20 +++++++++----------- test/test_other.py | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/library_pthread.js b/src/library_pthread.js index 398d80de4f0c2..f4f0822886594 100644 --- a/src/library_pthread.js +++ b/src/library_pthread.js @@ -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') diff --git a/test/test_other.py b/test/test_other.py index fc472a3d4ac5f..ab2904001ab70 100644 --- a/test/test_other.py +++ b/test/test_other.py @@ -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')) @@ -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):