rustwasm / wasm-pack

📦✨ your favorite rust -> wasm workflow tool!
https://rustwasm.github.io/wasm-pack/
Apache License 2.0
6.28k stars 409 forks source link

wasm-pack and webpack 5 #835

Open BenoitDel opened 4 years ago

BenoitDel commented 4 years ago

I am trying to make wasm-pack and webpack 5 works together. They work well all together (out of the box) only for the most simple use case. If memory or imports are necessary, modification has to be made to the wasm-pack build to adjust with:

I have several questions:

BenoitDel commented 4 years ago

wasm-bindgen is taking care of it. wasm bindgen-2110

samvv commented 3 years ago

A short update on Webpack 5 support. If you're using the most recent version of Webpack you need to add the following to webpack.config.js:

{
  module: {
    rules: [
      {
        test: /\.wasm$/,
        type: 'webassembly/sync',
      }
    ]
  },
  experiments: {
    syncWebAssembly: true
  }
}

Note that this requires the now deprecated syncWebAssembly backend. If you're getting the following error:

Uncaught (in promise) LinkError: WebAssembly.instantiate(): Import #0 module="./edmund_bg.js" function="__wbg_new_59cb74e423758ede" error: function import requires a callable

You're using the new experimental asyncWebAssembly backend that is currently not working too well with wasm-pack.

Given that the working configuration is now deprecated, I'm curious to how asyncWebAssembly will evolve and how it will integrate with wasm-pack. No idea if the error is a bug in Webpack or in this project.

superjose commented 3 years ago

I'm currently diving into Web Assembly, and I was wondering what is this "new Web Assembly spec", and whether it's something that wasm-pack will handle, or is there a consideration that we need to take into working with Rust itself.

tcper commented 2 years ago

Finally, I found this! CRA not work freaks me out, recommend add this content to README

HarikrishnanBalagopal commented 1 year ago

Any update on this? Still facing issues

klu235 commented 1 year ago

I am using webpack v5.88.2 and webpack-dev-server v4.15.1. I tried samvv's suggested changes in webpack.config.js, and still got the following error when trying to run webpack-dev-server:

export 'memory' (imported as 'memory') was not found in 'wasm-for-my-site/my_website_bg'

The export of the memory variable is supposed to be done by my_website_bg.wasm.d.ts, and this export works perfectly in webpack4, but not in webpack5.

webpack's frequent breaking of backwards compatibility is disappointing, to say the least. Its documentation also contains many grammar mistakes that some of the maintainers refuse to correct, even when given pull requests with corrections of those errors and no other changes.

I hope the Rust and WASM communities move on to better-documented and more consistent bundlers in the future.