Webpck不会捆绑使用wasm-bindgen生成的npm模块

时间:2019-10-02 03:26:51

标签: webpack wasm-bindgen

我在rust中写了一个小库,用于用复数进行一些计算。我用wasm-bindgen cli生成了wasm bin和js shim,将其发布并尝试在一个React应用程序中使用它。为了我的一生,我无法让Webpack捆绑这东西。

我有一个异步拆分点,并且正在尝试使用wasm-loader,通过这种配置,我从Webpack中获取“未检测到魔术头”。我还尝试过不指定加载程序,在这种情况下,我会出错:“模块解析失败:0x83处的意外部分”,它告诉我为此可能需要合适的加载程序。我不确定应该是什么魔术头或要研究什么其他装载器。

这是我的package.json和webpack配置:

{
  "name": "julia-set",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@babel/core": "^7.6.2",
    "@babel/preset-env": "^7.6.2",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.6",
    "complex-julia-funcs": "^0.1.0",
    "css-loader": "^3.2.0",
    "express": "^4.17.1",
    "nodemon": "^1.19.3",
    "preset-react": "^1.0.0",
    "react": "^16.10.1",
    "react-dom": "^16.10.1",
    "style-loader": "^1.0.0",
    "volleyball": "^1.5.1",
    "wasm-loader": "^1.3.0",
    "webpack": "4.28.4",
    "webpack-cli": "^3.3.9"
  }
}
module.exports = {
  mode: "development",
  entry: __dirname + "/src/index.js",
  output: {
    path: __dirname + "/public",
    filename: "bundle.js"
  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"]
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"]
          }
        }
      }
    ]
  }
};

和调用npm模块的js代码:

const numberReader = async () => {
  const { ComplexSqr } = await import("../../node_modules/complex-julia-funcs");
  const z = new ComplexSqr(2, 3);
  console.log(z);
};

export default numberReader;

npm模块为https://www.npmjs.com/package/complex-julia-funcs

0 个答案:

没有答案