标题重写为:The module appears to be a WebAssembly module, but it is not flagged as such for webpack.
P粉176980522
P粉176980522 2023-08-25 23:56:27
[Vue.js讨论组]

我正在尝试在我的 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 runserve 之后我收到此错误:

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"
},
...


P粉176980522
P粉176980522

全部回复(1)
P粉990008428

回答有点晚了,但也许对某人有帮助。

module.exports = {
  runtimeCompiler: true,
  configureWebpack: {
    externals: {
      experiments: {
        asyncWebAssembly: true,
      },
    },
  },
};
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号