diff --git a/src/lib/libpthread.js b/src/lib/libpthread.js index 9baac65e5f3f8..d767e1dc953bb 100644 --- a/src/lib/libpthread.js +++ b/src/lib/libpthread.js @@ -413,49 +413,44 @@ var LibraryPThread = { // Creates a new web Worker and places it in the unused worker pool to wait for its use. allocateUnusedWorker() { - var worker; + var workerUrl, workerSource; #if EXPORT_ES6 - // If we're using module output, use bundler-friendly pattern. -#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) => import.meta.url }); - worker = new Worker(p.createScriptURL('ignored'), {{{ pthreadWorkerOptions }}}); - } else + workerSource = import.meta.url; +#else + workerUrl = workerSource = _scriptName; #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). - // See https://github.com/webpack/webpack/issues/12638 - worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), {{{ pthreadWorkerOptions }}}); -#else // EXPORT_ES6 - var pthreadMainJs = _scriptName; #if expectToReceiveOnModule('mainScriptUrlOrBlob') - // We can't use makeModuleReceiveWithVar here since we want to also - // call URL.createObjectURL on the mainScriptUrlOrBlob. if (Module['mainScriptUrlOrBlob']) { - pthreadMainJs = Module['mainScriptUrlOrBlob']; - if (typeof pthreadMainJs != 'string') { - pthreadMainJs = URL.createObjectURL(pthreadMainJs); + // We can't use makeModuleReceiveWithVar here since we want to also + // call URL.createObjectURL on the mainScriptUrlOrBlob. + workerSource = workerUrl = Module['mainScriptUrlOrBlob']; + if (typeof workerUrl != 'string') { + workerSource = workerUrl = URL.createObjectURL(workerUrl); } } #endif #if PTHREADS_DEBUG - dbg(`Allocating a new web worker from ${pthreadMainJs}`); + dbg(`Allocating a new web worker from ${workerSource}`); #endif #if TRUSTED_TYPES // Use Trusted Types compatible wrappers. if (typeof trustedTypes != 'undefined' && trustedTypes.createPolicy) { - var p = trustedTypes.createPolicy('emscripten#workerPolicy2', { createScriptURL: (ignored) => pthreadMainJs }); - worker = new Worker(p.createScriptURL('ignored'), {{{ pthreadWorkerOptions }}}); + var p = trustedTypes.createPolicy('emscripten#workerPolicy2', { createScriptURL: (ignored) => workerSource }); + workerUrl = p.createScriptURL('ignored'); + } +#endif + var worker; +#if EXPORT_ES6 + if (!workerUrl) { + // 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). + // See https://github.com/webpack/webpack/issues/12638 + worker = new Worker(new URL("{{{ TARGET_JS_NAME }}}", import.meta.url), {{{ pthreadWorkerOptions }}}); } else #endif - worker = new Worker(pthreadMainJs, {{{ pthreadWorkerOptions }}}); -#endif // EXPORT_ES6 + worker = new Worker(workerUrl, {{{ pthreadWorkerOptions }}}); #if ASSERTIONS worker.workerID = PThread.nextWorkerID++; #endif