webpackrustwebpack-dev-serverrust-wasm

Rust Wasm and webpack-dev-server 5: Module not found


I'm trying to write hello, world with rust and wasm. Dev server works with

"webpack": "^4.29.3", 
"webpack-dev-server": "^3.1.5"

but if update to

"webpack": "^5.90.3", 
"webpack-dev-server": "^5.0.2"

Module not found error occurs after rebuilding wasm.

Steps to reproduce:

  1. git clone https://github.com/levap5/wasm-game-of-life.git
  2. cd wasm-game-of-life
  3. git checkout 90666d3f
  4. wasm-pack build
  5. cd www
  6. npm install
  7. npm run start | webpack-dev-server will start. There are no errors now.
  8. open http://localhost:8080/ It works as expected
  9. change alert message in src/lib.rs
  10. wasm-pack build in wasm-game-of-life dir

The error occurs after step 10 and http://localhost:8080/ runs old (cached) rust (wasm) code. How to fix this?


Solution

  • I found a solution using wasm-pack-plugin plugin.

    package.json:

    "devDependencies": {
        "@wasm-tool/wasm-pack-plugin": "^1.7.0",
        // ...
    }
    

    webpack.config.js:

    const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
    // ...
    plugins: [
        new WasmPackPlugin({
          crateDirectory: path.resolve(__dirname, "..") // ".." in my case
        }),
       // ...
    ]
    // ...
    

    However, the plugin's maintenance is questionable. I immediately came across this issue. After looking at the plugin code, the issue seems easy to fix.