Parcel: 馃悰 Fetch API is included in the codes generated by `parcel build -t node`

Created on 9 Mar 2018  路  2Comments  路  Source: parcel-bundler/parcel

This a 馃悰 bug report.

馃帥 Configuration (.babelrc, package.json, cli command)

/index.js

import { add } from './math.rs'

console.log(add(1, 2))

/math.rs

#[no_mangle]
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

馃 Expected Behavior

$ parcel build -t node -o dist/bundle.js index.js
$ node dist/bundle.js
3

馃槸 Current Behavior

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

馃拋 Possible Solution

  • Use fs.readFile instead of fetch
  • Use node-fetch

馃敠 Context

I want to test .wasm with unit testing frameworks on Node.js and run on browsers.

馃捇 Code Sample

https://gist.github.com/mizdra/e71b62f0ee839c8ad58ef679f2db747b

馃實 Your Environment

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

Bug WASM

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

philipodev picture philipodev  路  3Comments

adamreisnz picture adamreisnz  路  3Comments

davidnagli picture davidnagli  路  3Comments

dsky1990 picture dsky1990  路  3Comments

will-stone picture will-stone  路  3Comments