要求vm2使用电子和webpack
当我尝试使用Webpack导入Electron渲染器进程中的vm2
模块时遇到错误。
import fs from 'fs' console.log('ok') const { NodeVM } = require('vm2') console.log('ko')
这是Webpack显示的错误:
WARNING in ./~/vm2/lib/main.js Critical dependencies: 180:26-33 require function is used in a way in which dependencies cannot be statically extracted 335:15-22 require function is used in a way in which dependencies cannot be statically extracted 367:26-33 require function is used in a way in which dependencies cannot be statically extracted @ ./~/vm2/lib/main.js 180:26-33 335:15-22 367:26-33 WARNING in ./~/vm2/lib/sandbox.js Module parse failed: /Users/guillaume/Voyager/Voyager/node_modules/vm2/lib/sandbox.js 'return' outside of function (20:0) You may need an appropriate loader to handle this file type. SyntaxError: 'return' outside of function (20:0) at Parser.pp.raise (/Users/guillaume/Voyager/Voyager/node_modules/acorn/dist/acorn.js:923:13) at Parser.pp.parseReturnStatement (/Users/guillaume/Voyager/Voyager/node_modules/acorn/dist/acorn.js:1864:74)
我尝试从我的webpack config中排除node_modules文件夹:
module: { loaders: [{ test: /\.js$/, include: PATHS.app, loader: 'babel', exclude: /node_modules/, query: { presets: [ 'es2015', 'react', 'stage-1' ], plugins: [ 'add-module-exports', 'transform-decorators-legacy' ], }, }, { test: /\.scss$/, loaders: [ 'style', 'css', 'postcss-loader?parser=postcss-scss' ], include: PATHS.app, }], }
但似乎没有工作。 出现第一个console.log
,而不是第二个。 我也使用webpack-target-electron-renderer
添加了目标。 相同..
最后,当我添加externals: [/vm2/]
,Webpack不会引发任何错误,但渲染控制台说Uncaught ReferenceError: vm2 is not defined
我想这是有事做, vm2
使用dynamicrequire
和webpack不喜欢(因为导入fs
工作正常)
这个问题从哪里来,我该如何解决?