Javascript Webpack使用Emscripten抛出错误:Can';t解决';财政司司长';
将javascript文件导入vue.js组件时遇到错误: 这是/components子文件夹的内容:Javascript Webpack使用Emscripten抛出错误:Can';t解决';财政司司长';,javascript,vue.js,module,webassembly,emscripten,Javascript,Vue.js,Module,Webassembly,Emscripten,将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-
/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>
其中,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
如何解决?
马可TLDR;
有两种解决方案:
-s ENVIRONMENT='web'
,因此请尝试emcc Add.c-o js_peoping.js-s EXTRA_EXPORTED_RUNTIME_方法=['ccall','cwrap']-s ENVIRONMENT='web'
webpack.config.js
中添加“节点”:{“fs”:“empty”}
字段。但我不知道如何以零配置的方式完成.js
和.wasm
文件。.js
文件不仅是.wasm
的包装器,而且还为web提供了C/C++运行时,更重要的是,它是.wasm
文件的通用加载程序
.js
文件的加载器非常全面,默认情况下包括节点、web、web工作者和。这通常会给Webpack之类的绑定器带来问题,因为绑定器尝试解析源代码中的所有模块,包括用于在nodejs环境中加载.wasm
文件的require('fs')
,如果任何模块不适用于web环境,则绑定器会产生错误
避免该问题的一种方法是在emcc
命令中使用标志。这将删除一些有问题的代码,例如require('fs')
另一种方法是让网页包忽略
fs
模块,方法是在网页包配置中添加“节点”:{“fs”:“empty”}
。js\u piping.js文件中有什么?Hi@Piyush!我用js_pipeoping.js文件的内容更新了我的问题,你用什么来编译返回错误?@bumsikim我用这个命令编译了add.c文件:emcc add.c-o js_pipeoping.js-s EXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap']哦,我问的第一个编译命令抱怨没有'fs'模块,不是emcc命令。我猜emcc命令不会出错,对吧?我尝试了两种方法:在webpack.config.js中使用ENVIRONMENT='web'和module.exports={node:{'fs':'empty'},效果很好。非常感谢您对webpack 5的出色解释resolve.fallback.fs:false
#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
(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