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

Support both Node.js runtime and bundlers by default, out-of-the-box #469

Closed
KSXGitHub opened this issue Dec 24, 2018 · 7 comments
Closed
Labels
duplicate This issue or pull request already exists enhancement New feature or request feature request

Comments

@KSXGitHub
Copy link

KSXGitHub commented Dec 24, 2018

When I write an NPM package, I rarely think of bundlers as they can comprehend require() syntax, so as long as I only use require() to load module, I don't worry about bundlers. This is not the case with Node.js WASM modules generated by wasm-bindgen as it uses fs.readFileSync (which cannot be polyfilled by bundlers) to load .wasm file.

wasm-pack build currently supports 2 targets:

  • --target browser support bundlers (today's browsers cannot load the output script/module directly) but not Node.js.
  • --target nodejs support Node.js runtime but not bundlers/browsers.

This is inconvenient, if I want to support both targets, I have to write my own wrapper that calls wasm-bindgen twice.

Suggestion

  • Without options, generate 4 files for 4 targets:

    • index.bundler.js for JavaScript bundlers, set "browser" field in package.json to point to this file.
    • index.js for Node.js runtime, set "main" field in package.json to point to this file.
    • index.mjs to work with node --experimental-module, "module" field of package.json may or may not point to this file.
    • index.script.js to work with <script> tag (I want to be able to test this directly in browser console).
  • Deprecate the use of --target <target> in favor of --nodejs index.js, --module index.mjs, --browser index.bundler.js, and --script index.script.js.

Additional Suggestion

Please don't use package name in Cargo.toml for JavaScript entry files, use index.* like most NPM packages.

@KSXGitHub KSXGitHub changed the title Support both Node.js runtime and bundlers Support both Node.js runtime and bundlers by default, out-of-the-box Dec 24, 2018
@ashleygwilliams
Copy link
Member

hi @KSXGitHub ! this is a pretty common request that we get and something we fully intend to implement starting after the most recent release comes out!

@ashleygwilliams ashleygwilliams added duplicate This issue or pull request already exists enhancement New feature or request feature request labels Dec 27, 2018
@KSXGitHub
Copy link
Author

@ashleygwilliams Since you tagged this as "duplicate", can you also give me the link to the original issue?

BTW, this issue isn't a mere request, but also a small suggestion of how this could be implemented.

@ashleygwilliams
Copy link
Member

#157

and i understand that it's a suggestion for implementation- we already have a pretty good idea of how we'd like to do this (very similar to what you have suggested- except we are not interested in supporting .mjs at this time) - just need to do it!

@KSXGitHub
Copy link
Author

@ashleygwilliams Thanks.

except we are not interested in supporting .mjs at this time

What prevents you guy from doing this?

@ashleygwilliams
Copy link
Member

i think at the moment it's still experimental and in the interest of keeping wasm-pack simple, we don't want to support experimental targets. once node stabilizes it we'll add support.

@ashleygwilliams
Copy link
Member

closing in favor of #313 - going to copy your suggestions here into a comment on that issue. looking to do this for the 0.10.0 release

@unicornonea
Copy link

hi, could this work now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request feature request
Projects
None yet
Development

No branches or pull requests

3 participants