This a 馃悰 bug report.
/index.jsimport { add } from './math.rs'
console.log(add(1, 2))
/math.rs#[no_mangle]
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
$ parcel build -t node -o dist/bundle.js index.js
$ node dist/bundle.js
3
$ parcel build -t node -o dist/bundle.js index.js
$ node dist/bundle.js
/Users/mizdra/src/gomi/parcel-wasm-for-node/dist/bundle.js:8
module.exports=function(t){return fetch(t).then(function(t){return WebAssembly.instantiateStreaming?WebAssembly.instantiateStreaming(t):t.arrayBuffer().then(function(t){return WebAssembly.instantiate(t)})}).then(function(t){return t.instance.exports})};
^
ReferenceError: fetch is not defined
at module.exports (/Users/mizdra/src/gomi/parcel-wasm-for-node/dist/bundle.js:8:28)
at u (/Users/mizdra/src/gomi/parcel-wasm-for-node/dist/bundle.js:6:610)
at Array.map (<anonymous>)
at Function.t [as load] (/Users/mizdra/src/gomi/parcel-wasm-for-node/dist/bundle.js:6:303)
at Object.require.0 (/Users/mizdra/src/gomi/parcel-wasm-for-node/dist/bundle.js:10:50)
at t (/Users/mizdra/src/gomi/parcel-wasm-for-node/dist/bundle.js:1:408)
at /Users/mizdra/src/gomi/parcel-wasm-for-node/dist/bundle.js:1:646
at Object.<anonymous> (/Users/mizdra/src/gomi/parcel-wasm-for-node/dist/bundle.js:1:663)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
fs.readFile instead of fetchnode-fetchI want to test .wasm with unit testing frameworks on Node.js and run on browsers.
https://gist.github.com/mizdra/e71b62f0ee839c8ad58ef679f2db747b
| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.6.2 |
| Node | 9.5.0 |
| npm/Yarn | 5.6.0/1.3.2 |
| Operating System | macOS Sierra 10.12.6 |
We would have to use a different wasm bundle loader with node:
https://github.com/parcel-bundler/parcel/blob/0dc062f77e36a19297dac542be33cded04580e89/src/builtins/loaders/wasm-loader.js#L1-L4
Something like this? (from https://gist.github.com/kanaka/3c9caf38bc4da2ecec38f41ba24b77df)
const buffer = toUint8Array(fs.readFileSync(filename))
return WebAssembly.compile(buffer)
.then(module => {
//...
});
}
Otherwise, it's the users responsibility to include some fetch polyfill.
Something like this?
Yes. Thank you for providing examples!
In other words, I think that parcel should output codes which Node.js can execute when modules include .wasm and bundled with parcel [build] --target node ... .