模块似乎是WebAssembly模块,但模块未标记为Webpack的WebAssembly模块

svujldwt  于 2022-12-19  发布在  Webpack
关注(0)|答案(1)|浏览(420)

我尝试在我的Vue项目中导入一个webassembly模块(用Rust编写并用wasm-pack编译)。我所做的是创建一个项目:

vue-cli create my-vue-webasm-proj

我选择了Vue 2,之后我修改了我的main.js,如下所示(添加了async beforeCreate()):

/* main.js */

import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
  async beforeCreate() {
    const wlib= await import('my-webasm-lib')
    console.log(wlib)
  },
}).$mount('#app')

npm run serve之后,我得到这个错误:

Module parse failed: Unexpected character '' (1:0)
The module seem to be a WebAssembly module, but module is not flagged as WebAssembly module for webpack.
BREAKING CHANGE: Since webpack 5 WebAssembly is not enabled by default and flagged as experimental feature.
You need to enable one of the WebAssembly experiments via 'experiments.asyncWebAssembly: true' (based on async modules) or 'experiments.syncWebAssembly: true' (like webpack 4, deprecated).
For files that transpile to WebAssembly, make sure to set the module type in the 'module.rules' section of the config (e. g. 'type: "webassembly/async"').
(Source code omitted for this binary file)

我该如何补救

我尝试将此配置添加到webpack.config.js中,但没有成功:

module.exports = {
  experiments: {
    asyncWebAssembly: true,
    importAsync: true
  }
}

我的package.json,如果如下:

...
"dependencies": {
  "core-js": "^3.8.3",
  "my-webasm-lib": "file:../my-webasm-lib/my-webasm-lib-pkg",
  "vue": "^2.6.14"
},
"devDependencies": {
  "@babel/core": "^7.12.16",
  "@babel/eslint-parser": "^7.12.16",
  "@vue/cli-plugin-babel": "~5.0.0",
  "@vue/cli-plugin-eslint": "~5.0.0",
  "@vue/cli-service": "~5.0.0",
  "eslint": "^7.32.0",
  "eslint-plugin-vue": "^8.0.3",
  "vue-template-compiler": "^2.6.14"
},
...
6l7fqoea

6l7fqoea1#

这是一个有点晚的答案,但也许可以帮助一些人。

module.exports = {
  runtimeCompiler: true,
  configureWebpack: {
    externals: {
      experiments: {
        asyncWebAssembly: true,
      },
    },
  },
};

相关问题