Webpack在构build时使用节点“fs”模块,以便浏览器获取函数的结果
问题 :是否有办法告诉webpack在构build期间告诉内置模块(如fs)执行,以便浏览器获取此函数的结果,而不是函数调用本身?
我的情况 :
目前我正在开发一个使用webpack浏览器的应用程序。 我试图在我的文件中使用节点'fs'模块来从其他目录需要index.js
文件。 例如:
plugins ├──plugin1 │ ├── index.js (simply exports an object) │ ├──plugin2 │ ├── index.js (simply exports an object) | ├──plugin3 │ ├── index.js (simply exports an object) | |──index.js (want to require all index.js from each plugin directory here)
我得到一个webpack的错误说: Can't resolve 'fs' in somepath/node_modules/require-dir
我的文件index.js
位于`plugins / index.js'这只是试图要求我的其他文件。
//module from NPM which uses the 'fs' module ('im not explicity using it) const requireDir = require('require-dir'); const allPlugins = requireDir('./plugins/'); console.log(allPlugins);
无法parsing'/ some_path / node_modules / require-dir'中的'fs'
你有两个select。
- 我没有亲自使用过,但是可以使用这里指定的
node
configuration值。
node: { fs: {true, "mock", "empty", false} }
将fs
设置为上述任何值。
- 不要使用
fs
模块。 它是一个内置/本地模块,可能或不可能依赖本机V8 / C ++函数/库。 请记住,webpack通常捆绑浏览器的资源 。 所以,不要依赖一个插件,你可以手动导入你的插件,如:
plugins/index.js
const plugin1 = require('./plugin1') const plugin2 = require('./plugin2') module.exports = { plugin1, plugin2 }
您也可以使用这个答案来填充require-dir
模块。
感谢Francisco Mateo关于polyfilling require-dir
的额外链接,我了解了webpack添加require
的context
方法 。
这允许我做像我这样的插件/ index.js文件中的dynamic需求:
//require all index.js files inside of /plugins directory let context = require.context('.', true, /\index\.js/); const loadPlugins = function(ctx){ let keys = context.keys(); let values = keys.map(context); return values; } //array of values from each index.js require console.log('loadPlugins', loadPlugins());
- nodejs restful api返回错误状态-1在离子angularjs $ http
- 在Node.js Web应用程序中使用用户名和密码在Azure AD上进行身份validation
- 客户端模块工作stream程:(Browserify + npm + gulp)或(RequireJS + Bower + gulp)?
- 我如何使这个JSfunctionasynchronous?
- 通过存储过程插入logging不工作(使用繁琐的MS SQL驱动程序)
- 通过我的前端页面html中的button启动或编译Js文件
- 如何在节点redis上得到all
- Express.js + body-parser:POST请求中的空req.body
- 部署与webpack反应的应用程序