Skip to content

Commit 6a26e9b

Browse files
authored
Merge pull request webpack#4693 from Travmatth/fix-4252-BannerPlugin-placeholder
Fix 4252 banner plugin placeholder
2 parents 53bb15b + 08eca2f commit 6a26e9b

File tree

6 files changed

+117
-8
lines changed

6 files changed

+117
-8
lines changed

lib/BannerPlugin.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
const ConcatSource = require("webpack-sources").ConcatSource;
99
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
1010

11-
function wrapComment(str) {
11+
const wrapComment = (str) => {
1212
if(!str.includes("\n")) return `/*! ${str} */`;
1313
return `/*!\n * ${str.split("\n").join("\n * ")}\n */`;
14-
}
14+
};
1515

1616
class BannerPlugin {
1717
constructor(options) {
@@ -33,14 +33,36 @@ class BannerPlugin {
3333
compilation.plugin("optimize-chunk-assets", (chunks, callback) => {
3434
chunks.forEach((chunk) => {
3535
if(options.entryOnly && !chunk.isInitial()) return;
36-
3736
chunk.files
3837
.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options))
39-
.forEach((file) =>
40-
compilation.assets[file] = new ConcatSource(
41-
banner, "\n", compilation.assets[file]
42-
)
43-
);
38+
.forEach((file) => {
39+
let basename;
40+
let query = "";
41+
let filename = file;
42+
const hash = compilation.hash;
43+
const querySplit = filename.indexOf("?");
44+
45+
if(querySplit >= 0) {
46+
query = filename.substr(querySplit);
47+
filename = filename.substr(0, querySplit);
48+
}
49+
50+
if(filename.indexOf("/") < 0) {
51+
basename = filename;
52+
} else {
53+
basename = filename.substr(filename.lastIndexOf("/") + 1);
54+
}
55+
56+
const comment = compilation.getPath(banner, {
57+
hash,
58+
chunk,
59+
filename,
60+
basename,
61+
query,
62+
});
63+
64+
return compilation.assets[file] = new ConcatSource(comment, "\n", compilation.assets[file]);
65+
});
4466
});
4567
callback();
4668
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
const parseBanner = (banner) => {
2+
return banner
3+
.slice(4,-3)
4+
.split(", ")
5+
.map(n => n.split(":"))
6+
.reduce((acc, val) => {
7+
acc[val[0]] = val[1];
8+
return acc;
9+
}, {});
10+
};
11+
12+
var source = require("fs")
13+
.readFileSync(__filename, "utf-8")
14+
.split("\n")
15+
.slice(0,1)[0];
16+
17+
const banner = parseBanner(source)
18+
const REGEXP_HASH = /^[A-Za-z0-9]{20}$/
19+
20+
it("should interpolate file hash in chunk banner", () => {
21+
REGEXP_HASH.test(banner["hash"]).should.be.true;
22+
});
23+
24+
it("should interpolate chunkHash in chunk banner", () => {
25+
REGEXP_HASH.test(banner["chunkhash"]).should.be.true;
26+
});
27+
28+
it("should interpolate file into chunk banner", () => {
29+
banner["file"].should.equal("dist/banner.js");
30+
});
31+
32+
it("should interpolate name in chunk banner", () => {
33+
banner["name"].should.equal("dist/banner");
34+
});
35+
36+
it("should interpolate basename in chunk banner", () => {
37+
banner["filebase"].should.equal("banner.js");
38+
});
39+
40+
it("should interpolate query in chunk banner", () => {
41+
banner["query"].should.equal("?value");
42+
});
43+
44+
it("should parse entry into file in chunk banner", () => {
45+
banner["file"].should.not.equal(banner["filebase"]);
46+
});
47+
48+
it("should parse entry into name in chunk banner", () => {
49+
banner["filebase"].should.not.equal(banner["name"]);
50+
});
51+
52+
require.include("./test.js");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var fs = require("fs");
2+
3+
module.exports = {
4+
findBundle: function(i, options) {
5+
return "./dist/banner.js";
6+
}
7+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var foo = {};
2+
3+
module.exports = foo;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var bar = {};
2+
3+
module.exports = bar;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
const webpack = require("../../../../");
4+
5+
module.exports = {
6+
node: {
7+
__dirname: false,
8+
__filename: false
9+
},
10+
entry: {
11+
"dist/banner": ["./index.js"],
12+
vendors: ["./vendors.js"]
13+
},
14+
output: {
15+
filename: "[name].js?value"
16+
},
17+
plugins: [
18+
new webpack.BannerPlugin({
19+
banner: "hash:[hash], chunkhash:[chunkhash], name:[name], filebase:[filebase], query:[query], file:[file]"
20+
})
21+
]
22+
};

0 commit comments

Comments
 (0)