Merge pull request #1052 from larrifax/master

Include/exclude filtering in BannerPlugin
This commit is contained in:
Tobias Koppers 2015-05-10 21:10:39 +02:00
commit 7a05452001
5 changed files with 47 additions and 6 deletions

View File

@ -3,6 +3,7 @@
Author Tobias Koppers @sokra
*/
var ConcatSource = require("webpack-core/lib/ConcatSource");
var ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
function wrapComment(str) {
if(str.indexOf("\n") < 0) return "/*! " + str + " */";
@ -10,19 +11,20 @@ function wrapComment(str) {
}
function BannerPlugin(banner, options) {
if(!options) options = {};
this.banner = options.raw ? banner : wrapComment(banner);
this.entryOnly = options.entryOnly;
this.options = options || {};
this.banner = this.options.raw ? banner : wrapComment(banner);
}
module.exports = BannerPlugin;
BannerPlugin.prototype.apply = function(compiler) {
var options = this.options;
var banner = this.banner;
var entryOnly = this.entryOnly;
compiler.plugin("compilation", function(compilation) {
compilation.plugin("optimize-chunk-assets", function(chunks, callback) {
chunks.forEach(function(chunk) {
if(entryOnly && !chunk.initial) return;
chunk.files.forEach(function(file) {
if(options.entryOnly && !chunk.initial) return;
chunk.files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options)).forEach(function(file) {
compilation.assets[file] = new ConcatSource(banner, "\n", compilation.assets[file]);
});
});

View File

@ -0,0 +1,14 @@
it("should contain banner in bundle0 chunk", function() {
var fs = require("fs");
var source = fs.readFileSync(__filename, "utf-8");
source.should.containEql("A test value");
});
it("should not contain banner in vendors chunk", function() {
var fs = require("fs"),
path = require("path");
var source = fs.readFileSync(path.join(__dirname, "vendors.js"), "utf-8");
source.should.not.containEql("A test value");
});
require.include("./test.js");

View File

@ -0,0 +1,3 @@
var foo = {};
module.exports = foo;

View File

@ -0,0 +1,3 @@
var bar = {};
module.exports = bar;

View File

@ -0,0 +1,19 @@
var webpack = require("../../../../");
module.exports = {
node: {
__dirname: false,
__filename: false
},
entry: {
bundle0: ["./index.js"],
vendors: ["./vendors.js"]
},
output: {
filename: "[name].js"
},
plugins: [
new webpack.BannerPlugin("A test value", {
exclude: ["vendors.js"]
})
]
};