diff --git a/lib/BannerPlugin.js b/lib/BannerPlugin.js index f8e1f757c..a3c1143aa 100644 --- a/lib/BannerPlugin.js +++ b/lib/BannerPlugin.js @@ -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]); }); }); diff --git a/test/configCases/plugins/banner-plugin/index.js b/test/configCases/plugins/banner-plugin/index.js new file mode 100644 index 000000000..50db60024 --- /dev/null +++ b/test/configCases/plugins/banner-plugin/index.js @@ -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"); diff --git a/test/configCases/plugins/banner-plugin/test.js b/test/configCases/plugins/banner-plugin/test.js new file mode 100644 index 000000000..c9d886584 --- /dev/null +++ b/test/configCases/plugins/banner-plugin/test.js @@ -0,0 +1,3 @@ +var foo = {}; + +module.exports = foo; diff --git a/test/configCases/plugins/banner-plugin/vendors.js b/test/configCases/plugins/banner-plugin/vendors.js new file mode 100644 index 000000000..39ad0d4e1 --- /dev/null +++ b/test/configCases/plugins/banner-plugin/vendors.js @@ -0,0 +1,3 @@ +var bar = {}; + +module.exports = bar; diff --git a/test/configCases/plugins/banner-plugin/webpack.config.js b/test/configCases/plugins/banner-plugin/webpack.config.js new file mode 100644 index 000000000..e400d9098 --- /dev/null +++ b/test/configCases/plugins/banner-plugin/webpack.config.js @@ -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"] + }) + ] +};