问题描述
我的应用中有大量 javascript 文件,它们被分成 4 个子目录.在 grunt 中,我抓取所有这些并将它们编译成一个文件.这些文件没有 module.exports 功能.
I have a large amount of javascript files split into 4 subdirectory in my app. In grunt I grab all of them and compile them into one file. These files do not have a module.exports function.
我想使用 webpack 并将其拆分为 4 个部分.我不想手动进入并要求我的所有文件.
I want to use webpack and split it into 4 parts. I don't want to manually go in and require all my files.
我想创建一个插件,在编译时遍历目录树,然后获取所有 .js 文件名和路径,然后需要子目录中的所有文件并将其添加到输出中.
I would like to create a plugin that on compilation walks the directory trees, then grabs all the .js file names and paths, then requires all files in the sub directories and adds it to the output.
我希望将每个目录中的所有文件编译成一个模块,然后我可以从我的入口点文件中要求该模块,或者包含在 http://webpack.github.io/docs/plugins.html 提及.
I want all the files in each directories compiled into a module that I could then require from my entry point file, or include in the assets that http://webpack.github.io/docs/plugins.html mentions.
当添加一个新文件时,我只想把它放到正确的目录中并知道它会被包含.
When adding a new file I just want to drop it in the correct directory and know it will be included.
有没有办法使用 webpack 或某人编写的插件来做到这一点?
Is there a way to do this with webpack or a plugin that someone has written to do this?
推荐答案
我是这样做的:
function requireAll(r) { r.keys().forEach(r); }
requireAll(require.context('./modules/', true, /.js$/));
这篇关于如何在没有require语句的情况下使用webpack加载目录中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!