javascript - Webpack 使用 Emscripten 引发错误:无法解析 'fs'
问题描述
我在将 javascript 文件导入 vue.js 组件时遇到错误:
这是 /components 子文件夹的内容:
/startingV/src/components$ ls -lah
total 132K
drwxr-xr-x 2 marco marco 4,0K dic 26 11:22 .
drwxr-xr-x 5 marco marco 4,0K dic 26 09:32 ..
-rw-r--r-- 1 marco marco 441 nov 2 2016 Counter.vue
-rw-r--r-- 1 marco marco 441 dic 21 15:13 FormValidation.vue
-rw-r--r-- 1 marco marco 100K dic 26 10:38 js_plumbing.js
-rw-r--r-- 1 marco marco 9,3K dic 26 10:38 js_plumbing.wasm
-rw-r--r-- 1 marco marco 473 dic 26 11:14 Result.vue
编译时:
Failed to compile.
./src/components/js_plumbing.js
Module not found: Error: Can't resolve 'fs' in '/home/marco/cpp/WebAssemblyinAction
/Appendix_B/B.1_ccall/startingV/src/components'
这是 Result.vue 文件:
template>
<p button @click="callAdd">Add!</p>
</template>
<script>
import * as js_plumbing from './js_plumbing'
export default {
data () {
return {
result: null
}
},
methods: {
callAdd() {
const result = Module.ccall('js_plumbing.Add',
'number',
['number', 'number'],
[1, 2]);
console.log('Result: ${result}');
}
}
}
</script>
js_plumbing.js 和 js_plumbing.wasm 文件是通过这个命令生成的:
emcc add.c -o js_plumbing.js -s EXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap']
其中 add.c 是:
#include <stdlib.h>
#include <emscripten.h>
// If this is an Emscripten (WebAssembly) build then...
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#ifdef __cplusplus
extern "C" { // So that the C++ compiler does not rename our function names
#endif
EMSCRIPTEN_KEEPALIVE
int Add(int value1, int value2)
{
return (value1 + value2);
}
#ifdef __cplusplus
}
#endif
这是我获取有关如何生成 .js 和 .wasm 文件的一些信息的链接: https ://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-ccall-包裹
执行 npn run dev 时命令行中的完整错误消息:
(base) marco@marco-U36SG:~/cpp/WebAssemblyinAction/Appendix_B
/B.1_ccall/startingV$ npm run dev
> myproject@1.0.0 dev /home/marco/cpp/WebAssemblyinAction/Appendix_B
/B.1_ccall/startingV
> cross-env NODE_ENV=development webpack-dev-server --open --hot
✖ 「wdm」: Hash: 12bee4de7abfde1cbc5d
Version: webpack 4.41.3
Time: 6707ms
Built at: 12/26/2019 12:37:07 PM
Asset Size Chunks Chunk Names
build.js 2.18 MiB main [emitted] [big] main
Entrypoint main [big] = build.js
[0] multi (webpack)-dev-server/client?http://localhost:8080
(webpack)/hot/dev-server.js ./src/main.js 52 bytes {main} [built]
[./node_modules/strip-ansi/index.js] 161 bytes {main} [built]
[./node_modules/vue/dist/vue.esm.js] 319 KiB {main} [built]
[./node_modules/webpack-dev-server/client/index.js?http:
//localhost:8080] (webpack)-dev-server/client?http://localhost:8080
4.29 KiB {main} [built]
[./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-
server/client/overlay.js 3.51 KiB {main} [built]
[./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-
server/client/socket.js 1.53 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/createSocketUrl.js]
(webpack)-dev-server/client/utils/createSocketUrl.js 2.89 KiB {main}
[built]
[./node_modules/webpack-dev-server/client/utils/log.js] (webpack)-dev-
server/client/utils/log.js 964 bytes {main} [built]
[./node_modules/webpack-dev-server/client/utils/reloadApp.js]
(webpack)-dev-server/client/utils/reloadApp.js 1.59 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/sendMessage.js]
(webpack)-dev-server/client/utils/sendMessage.js 402 bytes {main}
[built]
[./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync
nonrecursive ^\.\/log$ 170 bytes {main} [built]
[./node_modules/webpack/hot/dev-server.js] (webpack)/hot/dev-server.js
1.59 KiB {main} [built]
[./node_modules/webpack/hot/emitter.js] (webpack)/hot/emitter.js 75
bytes {main} [built]
[./node_modules/webpack/hot/log-apply-result.js] (webpack)/hot/log-
apply-result.js 1.27 KiB {main} [built]
[./src/main.js] 134 bytes {main} [built]
+ 34 hidden modules
ERROR in ./src/components/js_plumbing.js
Module not found: Error: Can't resolve 'fs' in '/home/marco
/cpp/WebAssemblyinAction/Appendix_B/B.1_ccall/startingV
/src/components'
@ ./src/components/js_plumbing.js 88:26-39
@ ./node_modules/babel-loader/lib!./node_modules/vue-loader
/lib/selector.js?type=script&index=0!./src/components/Result.vue
@ ./src/components/Result.vue
@ ./node_modules/babel-loader/lib!./node_modules/vue-loader
/lib/selector.js?type=script&index=0!./src/App.vue
@ ./src/App.vue
@ ./src/main.js
如何解决?马可
解决方案
TLDR;
有两种解决方案:
- 添加
-s ENVIRONMENT='web'
你的 emcc 命令,所以试试emcc add.c -o js_plumbing.js -s EXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap'] -s ENVIRONMENT='web'
或者
"node": { "fs": "empty" }
在您的webpack.config.js
. 我不知道如何以零配置方式做到这一点。
为什么
Emscripten 的 emcc 命令同时生成.js
和.wasm
文件。该.js
文件不仅是 的包装器,.wasm
而且还为 Web 提供了 C/C++ 运行时,更重要的是,在问题中,它是.wasm
文件的通用加载器。
该.js
文件的加载器非常全面,默认涵盖了 node、web、web worker 和D8 JS shell。这通常会给 Webpack 等捆绑器带来问题,因为捆绑器会尝试解析源代码中的所有模块,包括require('fs')
用于.wasm
在 nodejs 环境中加载文件的模块,并且如果任何模块不适用于 Web 环境,则捆绑器会产生错误。
避免该问题的一种方法是在命令中使用ENVIRONMENT
标志。emcc
这将删除一些有问题的代码,例如require('fs')
.
fs
另一种方法是通过添加"node": { "fs": "empty" }
webpack 配置让 Webpack 忽略模块。
推荐阅读
- javascript - _.debounce 检查去抖是否挂起
- git - 无法调试项目的 Git 版本(Xamarin)
- laravel-5.4 - laravel 5.4 中的关系显示数据
- powershell - Foreach(Powershell)似乎无法正常工作
- python - 获取“NameError:名称'room_path'未定义”
- javascript - 在我的 notes.js 中找不到记事本应用程序的错误
- c++ - 是否将构造函数的参数作为类的实例传递?
- javascript - Angular 订阅对象属性更改
- javascript - Redux - 添加到嵌套状态
- ios - Swift:XPC 连接中断并且应用程序冻结?