我在 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;