Description
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 inpackage.json
to point to this file.index.js
for Node.js runtime, set"main"
field inpackage.json
to point to this file.index.mjs
to work withnode --experimental-module
,"module"
field ofpackage.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.