Javascript 在';条目';
我正在使用Grunt编译页面级JS资产Javascript 在';条目';,javascript,node.js,gruntjs,Javascript,Node.js,Gruntjs,我正在使用Grunt编译页面级JS资产 webpack: { build: { entry: { // Add your page JS here! home: "./assets/scripts/tmp/Pages/home.js", events: "./assets/scripts/tmp/Pages/events.js"
webpack: {
build: {
entry: {
// Add your page JS here!
home: "./assets/scripts/tmp/Pages/home.js",
events: "./assets/scripts/tmp/Pages/events.js"
},
output: {
path: "./public/scripts"
}
}
}
我现在就是这样做的,但我想做一些类似的事情:
webpack: {
build: {
entry: "./assets/scripts/tmp/Pages/*",
output: {
path: "./public/scripts"
}
}
}
但是,此操作失败,出现“未找到输入模块中的错误:”错误
我尝试了SRC和DEST选项,但它们似乎都没有编译文件:S
提前感谢条目选项不支持通配符,但grunt支持。您可以使用grunts通配符支持为
条目
选项构造对象:
var pagesBase = path.resolve("assets/scripts/tmp/Pages");
build: {
entry: grunt.file.expand({ cwd: pagesBase }, "*").reduce(function(map, page) {
map[path.basename(page)] = path.join(pagesBase, page);
return map;
}, {}),
output: {
path: "./public/scripts",
filename: "[name].js" // You also need this to name the output file
}
}
grunt.file.expand
只返回pages目录中所有匹配文件的数组Array.prototype.reduce
用于将数组转换为对象
注意:为了使您的示例完整,您还需要在
输出.filename
选项中包含[name]
。任何其他想要了解简单事实的人。。。这就是我使用的:
webpack: {
build: {
entry: {
"home-page": "./" + path + "scripts/tmp/Pages/home-page.js",
"event-page": "./" + path + "scripts/tmp/Pages/event-page.js",
"performer-page": "./" + path + "scripts/tmp/Pages/performer-page.js",
"order-page": "./" + path + "scripts/tmp/Pages/order-page.js",
"support-page": "./" + path + "scripts/tmp/Pages/support-page.js"
},
output: {
path: "public/scripts",
filename: "[name].js"
}
}
}
与Tobias K.答案类似,但有一个工作示例:
var config = {
...
webpackFiles: {}
};
//Dynamically create list of files in a folder to bundle for webpack
grunt.file.expand({ cwd: 'my/folder/' }, "*").forEach(function(item){
config.webpackFiles[item.substr(0,item.indexOf('.js'))] = './my/folder/' + item;
});
然后在你的咕噜任务中,像这样使用它:
webpack: {
build: {
entry: config.webpackFiles,
output: {
path: "<%= config.jsDest %>/",
filename: "[name].js"
},
module: {
...
}
}
},
看起来你不能这样使用入口点,或者webpack只是将文件移动到公共脚本目录。您可以在Pages目录中包含一个
index.js
,并单独调用每个Pages文件,这将根据文件名将它们打包到输出中的单个文件中。虽然这不是我使用的方法,但我会推荐给其他人。我手动构建了我的列表,因为这个脚本似乎不起作用。从理论上讲,这是最好的方法,我不会因为我不能让它发挥作用而不接受惩罚。谢谢。这是不是缺少一张返回地图代码>在reduce函数中?@graup是的,我刚刚用返回映射尝试了它代码>包含在内,并且运行良好。最好使用_dirname
//Dynamically create list of view to bundle for webpack
config.webpackFiles.App = './' + config.jsSrc + '/App.js';
grunt.file.expand({ cwd: 'Static/js/src/views/' }, "*").forEach(function(item){
config.webpackFiles[item.substr(0,item.indexOf('.js'))] = './Static/js/src/views/' + item;
});