Skip to content

Commit 687c038

Browse files
authored
Merge pull request webpack#7270 from webpack/feature/banner_function
Allow banner to be specified as a function
2 parents 51fa786 + 561e95e commit 687c038

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

lib/BannerPlugin.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,33 @@ const wrapComment = str => {
2222

2323
class BannerPlugin {
2424
constructor(options) {
25-
if (arguments.length > 1)
25+
if (arguments.length > 1) {
2626
throw new Error(
2727
"BannerPlugin only takes one argument (pass an options object)"
2828
);
29+
}
2930

3031
validateOptions(schema, options, "Banner Plugin");
3132

32-
if (typeof options === "string")
33+
if (typeof options === "string" || typeof options === "function") {
3334
options = {
3435
banner: options
3536
};
37+
}
38+
3639
this.options = options || {};
37-
this.banner = this.options.raw
38-
? options.banner
39-
: wrapComment(options.banner);
40+
41+
if (typeof options.banner === "function") {
42+
const getBanner = this.options.banner;
43+
this.banner = this.options.raw
44+
? getBanner
45+
: data => wrapComment(getBanner(data));
46+
} else {
47+
const banner = this.options.raw
48+
? this.options.banner
49+
: wrapComment(this.options.banner);
50+
this.banner = () => banner;
51+
}
4052
}
4153

4254
apply(compiler) {
@@ -78,13 +90,15 @@ class BannerPlugin {
7890
basename = filename.substr(lastSlashIndex + 1);
7991
}
8092

81-
const comment = compilation.getPath(banner, {
93+
const data = {
8294
hash,
8395
chunk,
8496
filename,
8597
basename,
8698
query
87-
});
99+
};
100+
101+
const comment = compilation.getPath(banner(data), data);
88102

89103
compilation.assets[file] = new ConcatSource(
90104
comment,

schemas/plugins/BannerPlugin.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,15 @@
3939
],
4040
"properties": {
4141
"banner": {
42-
"description": "The banner as string, it will be wrapped in a comment",
43-
"type": "string"
42+
"description": "Specifies the banner",
43+
"anyOf": [
44+
{
45+
"instanceof": "Function"
46+
},
47+
{
48+
"type": "string"
49+
}
50+
]
4451
},
4552
"raw": {
4653
"description": "If true, banner will not be wrapped in a comment",
@@ -76,6 +83,10 @@
7683
}
7784
}
7885
},
86+
{
87+
"description": "The banner as function, it will be wrapped in a comment",
88+
"instanceof": "Function"
89+
},
7990
{
8091
"description": "The banner as string, it will be wrapped in a comment",
8192
"minLength": 1,

test/configCases/plugins/banner-plugin/index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
it("should contain banner in bundle0 chunk", function() {
2-
var fs = require("fs");
3-
var source = fs.readFileSync(__filename, "utf-8");
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
it("should contain banner in bundle0 chunk", () => {
5+
const source = fs.readFileSync(__filename, "utf-8");
46
expect(source).toMatch("A test value");
7+
expect(source).toMatch("banner is a string");
8+
expect(source).toMatch("banner is a function");
9+
expect(source).toMatch("/*!\n * multiline\n * banner\n * 1\n */");
510
});
611

7-
it("should not contain banner in vendors chunk", function() {
8-
var fs = require("fs"),
9-
path = require("path");
10-
var source = fs.readFileSync(path.join(__dirname, "vendors.js"), "utf-8");
12+
it("should not contain banner in vendors chunk", () => {
13+
const source = fs.readFileSync(path.join(__dirname, "vendors.js"), "utf-8");
1114
expect(source).not.toMatch("A test value");
1215
});
1316

test/configCases/plugins/banner-plugin/webpack.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ module.exports = {
1212
filename: "[name].js"
1313
},
1414
plugins: [
15+
new webpack.BannerPlugin("banner is a string"),
16+
new webpack.BannerPlugin(() => "banner is a function"),
1517
new webpack.BannerPlugin({
1618
banner: "A test value",
1719
exclude: ["vendors.js"]
20+
}),
21+
new webpack.BannerPlugin({
22+
banner: ({ chunk }) => `multiline\nbanner\n${chunk.id}`
1823
})
1924
]
2025
};

0 commit comments

Comments
 (0)