Wasm-bindgen: Unable to resolve generated .wasm module

Created on 10 Sep 2018  路  1Comment  路  Source: rustwasm/wasm-bindgen

This might be mostly a webpack issue, rather than a wasm-bindgen issue, but I do have one wasm-bindgen specific question.

Running these commands generates my the .wasm and .js glue file as expected, which is great:

cargo +nightly build --target wasm32-unknown-unknown && wasm-bindgen --out-dir pkg ./target/wasm32-unknown-unknown/release/rust.wasm

The generated .js glue file (rust.js in my case) looks like this:

/* tslint:disable */
import * as wasm from './rust_bg';

const TextDecoder = typeof self === 'object' && self.TextDecoder
    ? self.TextDecoder
    : require('util').TextDecoder;

...more glue...

/**
* @returns {void}
*/
export function greet() {
    return wasm.greet();
}

The thing that is giving me issues is the second line, import * as wasm from './rust_bg';. Importing the greet function elsewhere in my otherwise all JS application and then running webpack (version 4.17.1) results in this error:

 ERROR in ./rust/pkg/rust.js
    Module not found: Error: Can't resolve './rust_bg' in '/Users/twilco/projects/proj-name/frontend/rust/pkg'
     @ ./rust/pkg/rust.js 11:15-35
     @ ./billing/status/status-view.jsx
     @ ./billing/index.js
     @ ./billing/bootstrap.js

When I change import * as wasm from './rust_bg'; to import * as wasm from './rust_bg.wasm';, the problem is fixed, and I'm able to use the imported wasm as expected - however, this manual edit is obviously overwritten every time I redo the bindgen. Is there a reason the .wasm file extension is left off the _bg file? There's probably a way to finagle Webpack into being able to resolve it without the file extension (although I don't know it!), but if there's no downside to adding the .wasm file extension to the end of that import statement then that would be nice to have.

Most helpful comment

Scratch this issue - was as simple as adding this to my webpack.config.js file 馃槼.

resolve: {
      alias: {
        ...stuff...
      },
      extensions: ['.wasm']
}

>All comments

Scratch this issue - was as simple as adding this to my webpack.config.js file 馃槼.

resolve: {
      alias: {
        ...stuff...
      },
      extensions: ['.wasm']
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

fitzgen picture fitzgen  路  3Comments

expobrain picture expobrain  路  4Comments

derekdreery picture derekdreery  路  3Comments

pauldorehill picture pauldorehill  路  3Comments

kurbaniec picture kurbaniec  路  3Comments