emscripten
emscripten copied to clipboard
Use simple import.meta.url for worker creation
This needs to be tested under various bundlers. Spun out from #23804 . Allows for scripts to be renamed after compilation.
Vite and (unreleased) Webpack and (I believe) Parcel allow for the form new Worker(import.meta.url), which simplifies the new worker creation in libpthread and avoids the need for using a macro to expand the current script name (which makes the generated code fragile).
I’ve taken the opportunity to expand the test coverage for permutations of bundlers and mainScriptUrlOrBlob with and without threads (qua #23890 ), but if this feels redundant then the change in libpthread is enough.
Looking at the test failures in test-other, I think it needs to be new Worker(new URL(import.meta.url)), at least on Node.js.
TypeError [ERR_WORKER_PATH]: The worker script or module filename must be an absolute path or a relative path starting with './' or '../'. Wrap file:// URLs with `new URL`. Received "file:///tmp/emtest_zpd2a426/emscripten_test_other_ybmo_i00/subdir/module.mjs"
We could perhaps use new Worker(import.meta.url) when ENVIRONMENT_MAY_BE_WEB && !ENVIRONMENT_MAY_BE_NODE, but this code section already has quite a few conditional expressions. :sweat_smile:
Looking at the test failures in
test-other, I think it needs to benew Worker(new URL(import.meta.url)), at least on Node.js.TypeError [ERR_WORKER_PATH]: The worker script or module filename must be an absolute path or a relative path starting with './' or '../'. Wrap file:// URLs with `new URL`. Received "file:///tmp/emtest_zpd2a426/emscripten_test_other_ybmo_i00/subdir/module.mjs"We could perhaps use
new Worker(import.meta.url)whenENVIRONMENT_MAY_BE_WEB && !ENVIRONMENT_MAY_BE_NODE, but this code section already has quite a few conditional expressions. 😅
Or we could improve our new Worker polyfill on node so that it support string as a well as URLs.