Skip to content

Commit 1ae4eeb

Browse files
committed
Support blob URLs on ES6 with USE_ES6_IMPORT_META enabled
Per the discussion in #23769, implement this policy: - If mainScriptUrlOrBlob is set, create the worker as in the old non-use-import-meta case - If import.meta.url is a blob url, use it for the worker script - Otherwise, use new URL(TARGET_JS_NAME, import.meta.url)
1 parent a9e18d8 commit 1ae4eeb

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/lib/libpthread.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -435,11 +435,29 @@ var LibraryPThread = {
435435
worker = new Worker(p.createScriptURL('ignored'), {{{ pthreadWorkerOptions }}});
436436
} else
437437
#endif
438-
// We need to generate the URL with import.meta.url as the base URL of the JS file
439-
// instead of just using new URL(import.meta.url) because bundler's only recognize
440-
// the first case in their bundling step. The latter ends up producing an invalid
441-
// URL to import from the server (e.g., for webpack the file:// path).
442-
worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), {{{ pthreadWorkerOptions }}});
438+
{
439+
#if expectToReceiveOnModule('mainScriptUrlOrBlob')
440+
if (Module['mainScriptUrlOrBlob']) {
441+
var pthreadMainJs = Module['mainScriptUrlOrBlob'];
442+
if (typeof pthreadMainJs != 'string') {
443+
pthreadMainJs = URL.createObjectURL(pthreadMainJs);
444+
}
445+
worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), {{{ pthreadWorkerOptions }}});
446+
} else
447+
#endif
448+
{
449+
if (import.meta.url.startsWith('blob:')) {
450+
// If our import.meta.url is a blob, we should just use it.
451+
worker = new Worker(import.meta.url, {{{ pthreadWorkerOptions }}});
452+
} else {
453+
// We need to generate the URL with import.meta.url as the base URL of the JS file
454+
// instead of just using new URL(import.meta.url) because bundler's only recognize
455+
// the first case in their bundling step. The latter ends up producing an invalid
456+
// URL to import from the server (e.g., for webpack the file:// path).
457+
worker = new Worker(new URL('{{{ TARGET_JS_NAME }}}', import.meta.url), {{{ pthreadWorkerOptions }}});
458+
}
459+
}
460+
}
443461
#else // EXPORT_ES6
444462
var pthreadMainJs = _scriptName;
445463
#if expectToReceiveOnModule('mainScriptUrlOrBlob')

0 commit comments

Comments
 (0)