Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EXPORT_ES6 should add leading ./ to WebAssembly path #19066

Closed
dhdaines opened this issue Mar 24, 2023 · 10 comments
Closed

EXPORT_ES6 should add leading ./ to WebAssembly path #19066

dhdaines opened this issue Mar 24, 2023 · 10 comments

Comments

@dhdaines
Copy link

esbuild is finally getting around to supporting import.meta.url in the same way that other bundlers do, which will allow modules compiled with EXPORT_ES6 to find their corresponding WebAssembly files properly when bundling...

... except that it doesn't quite work, because the currently proposed implementation expects the path of a resource referenced with new URL(..., import.meta.url) to begin with ./ or ../ to avoid ambiguity.

For compatibility with this, it may be useful (as long as it doesn't hurt anyone else) to add this leading ./ when outputting that path with EXPORT_ES6, otherwise it will continue to be difficult to use Emscripten-compiled modules with esbuild.

@sbc100
Copy link
Collaborator

sbc100 commented Mar 24, 2023

Sounds like a good idea. I'm also looked at adding tests here in the emscripten repo for various bundlers. I did the first one in #18969 but I plan to do more.

@dhdaines
Copy link
Author

Excellent! There is also the problem where esbuild chokes on this:

    scriptDirectory = require('url').fileURLToPath(new URL('./', import.meta.url)); // includes trailing slash

but I'm not really sure if there's any way around that (and in any case you can avoid it by adding -sENVIRONMENT=web)

@dhdaines
Copy link
Author

dhdaines commented Mar 28, 2023

Actually Webpack doesn't seem to like that line above (new URL('./', import.meta.url)) either. I'll file a separate bug with a test case for it.

@dhdaines
Copy link
Author

Right, so the issue with new URL('./', import.meta.url) is really a feature/bug of Webpack, which tries to resolve it as a module. Most of the time, it succeeds in doing this, even though it resolves to something useless, because there's a package.json in the same directory as the Emscripten-generated JS. This isn't actually what we want it to do, because:

  1. The resulting URL is never actually used for anything since it is wrapped in ENVIRONMENT_IS_NODE.
  2. It results in Webpack bundling whatever it ends up resolving that to, meaning that the output bundle for an ES6 module can be somewhat larger than it would be for a CJS one.

The obvious solution is to build a web-only module (e.g. -sENVIRONMENT=web) when using EXPORT_ES6, in which case everything Just Works with webpack (but not with esbuild, for the moment), and also you don't have to stub out module.

Failing that, maybe there is another way to get the script directory in Node without triggering Webpack's bogus module resolution, but I'm not totally sure what it is...

@dhdaines
Copy link
Author

This is what Webpack will do to new URL('./', import.meta.url) - pretty sure it is their bug, but perhaps there is a workaround: webpack/webpack#16878

@dhdaines
Copy link
Author

Also note that the proposed implementation for import.meta.url handling in esbuild doesn't actually work to load WebAssembly, but this is also their problem since It Works In Webpack: evanw/esbuild#2508 (comment)

@RReverser
Copy link
Collaborator

to begin with ./ or ../ to avoid ambiguity.

That sounds like a weird choice to me TBH because relative URLs are unambiguous and are specified to behave in the same way whether ./ is included or not.

@dhdaines
Copy link
Author

to begin with ./ or ../ to avoid ambiguity.

That sounds like a weird choice to me TBH because relative URLs are unambiguous and are specified to behave in the same way whether ./ is included or not.

Indeed! I am not really sure what the esbuild maintainer means by "unambiguous" in this case, but perhaps it has something to do with thinking that a bare relative path is a "module"...

@RReverser
Copy link
Collaborator

To be fair, I've been watching that PR & the original issue for a long while (you might see my comments there) and there hasn't been any change in many months now, so I'd rather wait for any updates from the maintainer before committing to any changes on the Emscripten side.

I suspect they currently just don't have time to work on this feature, so applying workarounds that might or might not be necessary when the PR is finished seems a bit premature.

@dhdaines
Copy link
Author

Sounds good - I will close this issue, because for the moment, you can either copy the .wasm manually or also use https://www.npmjs.com/package/@chialab/esbuild-plugin-meta-url which works in most cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants