我正在使用Grunt编译页面级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
提前感谢
发布于 2014-03-28 14:43:30
entry选项不支持通配符,但grunt支持。可以使用grunts通配符支持为entry选项构造对象:
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只返回页面目录中所有匹配文件的数组。Array.prototype.reduce用于将数组转换为对象。
注意:要使示例完整,还需要在[name]选项中包括output.filename。
发布于 2014-03-31 15:55:05
为了寻找一个简单的事实..。这就是我所用的:
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"
}
}
}发布于 2014-05-06 13:46:53
与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: {
...
}
}
},唯一的缺点是,如果要向此构建添加特定文件(例如,包app.js),则必须将其添加到webpackFiles变量中,如下所示
//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;
});https://stackoverflow.com/questions/22078420
复制相似问题