在“入口”上的Grunt-webpack通配符

我正在使用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" } } } 

然而,这个失败与“错误在Entry模块找不到:”错误。

我已经试过SRC和DEST选项,但他们似乎甚至没有编译文件:S

提前致谢

entry选项不支持通配符,但咕噜声。 您可以使用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只是返回pages目录中所有匹配文件的数组。 Array.prototype.reduce用于将数组转换为对象。

注意:为了使您的示例完整,您还需要在output.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: { ... } } }, 

唯一的缺点是,如果你想添加特定的文件到这个版本(例如bundle app.js),你将不得不像这样添加到webpackFilesvariables

 //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; });