emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Use simple import.meta.url for worker creation

Open JoeOsborn opened this issue 8 months ago • 2 comments
trafficstars

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.

JoeOsborn avatar Mar 11 '25 17:03 JoeOsborn

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:

kleisauke avatar Mar 15 '25 13:03 kleisauke

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. 😅

Or we could improve our new Worker polyfill on node so that it support string as a well as URLs.

sbc100 avatar Mar 15 '25 19:03 sbc100