Skip to content

Commit 132a612

Browse files
committed
swapped interpolate for compilation.getPath, removed superfluous test
1 parent 8664f54 commit 132a612

3 files changed

Lines changed: 20 additions & 55 deletions

File tree

‎lib/BannerPlugin.js‎

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,6 @@ const wrapComment = (str) => {
1313
return `/*!\n * ${str.split("\n").join("\n * ")}\n */`;
1414
};
1515

16-
const REGEXP_HASH = /\[hash:?(\d+)?\]/gi,
17-
REGEXP_CHUNKHASH = /\[chunkhash:?(\d+)?\]/gi,
18-
REGEXP_NAME = /\[name\]/gi,
19-
REGEXP_EXT = /\[ext\]/gi;
20-
21-
// withHashLength, getReplacer from lib/TemplatedPathPlugin.js
22-
const withHashLength = (replacer, handlerFn) => {
23-
return function(_, hashLength) {
24-
const length = hashLength && parseInt(hashLength, 10);
25-
if(length && handlerFn) {
26-
return handlerFn(length);
27-
}
28-
29-
const hash = replacer.apply(this, arguments);
30-
return length ? hash.slice(0, length) : hash;
31-
};
32-
};
33-
34-
const getReplacer = (value) => {
35-
return function(match) {
36-
return `${value}`;
37-
};
38-
};
39-
40-
const interpolate = (banner, file, hash, chunkHash) => {
41-
let name, ext;
42-
const splitPoint = file.lastIndexOf(".");
43-
44-
if(splitPoint > -1) {
45-
name = file.slice(0, splitPoint);
46-
ext = file.slice(splitPoint, -1);
47-
} else {
48-
name = file;
49-
ext = "";
50-
}
51-
52-
return banner
53-
.replace(REGEXP_HASH, withHashLength(getReplacer(hash)))
54-
.replace(REGEXP_CHUNKHASH, withHashLength(getReplacer(chunkHash)))
55-
.replace(REGEXP_NAME, name)
56-
.replace(REGEXP_EXT, ext);
57-
};
58-
5916
class BannerPlugin {
6017
constructor(options) {
6118
if(arguments.length > 1)
@@ -79,8 +36,24 @@ class BannerPlugin {
7936
chunk.files
8037
.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options))
8138
.forEach((file) => {
82-
const chunkHash = chunk.renderedHash || chunk.hash;
83-
const comment = interpolate(banner, file, compilation.hash, chunkHash);
39+
let filename, basename;
40+
const splitPoint = file.lastIndexOf(".");
41+
42+
if(splitPoint > 0) {
43+
filename = file.slice(0, splitPoint);
44+
basename = file.slice(splitPoint, -1);
45+
} else {
46+
filename = file;
47+
basename = "";
48+
}
49+
50+
const comment = compilation.getPath(banner, {
51+
hash: compilation.hash,
52+
chunk,
53+
filename,
54+
basename,
55+
});
56+
8457
return compilation.assets[file] = new ConcatSource(comment, "\n", compilation.assets[file]);
8558
});
8659
});

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,16 @@ it("should interpolate file hash in bundle0 chunk", () => {
2020
banner["hash"].should.not.equal("[hash]");
2121
});
2222

23-
it("should interpolate file hash in bundle0 chunk and respect size limit", () => {
24-
banner["hash"].length.should.equal(10);
25-
});
26-
2723
it("should interpolate chunkHash in bundle0 chunk", () => {
2824
banner["chunkhash"].should.not.equal("[chunkhash]");
2925
});
3026

31-
it("should interpolate chunkHash in bundle0 chunk and respect size limit", () => {
32-
banner["chunkhash"].length.should.equal(10);
33-
});
34-
3527
it("should interpolate name in bundle0 chunk", () => {
3628
banner["name"].should.not.equal("[name]");
3729
});
3830

3931
it("should interpolate extension in bundle0 chunk", () => {
40-
banner["ext"].should.not.equal("[ext]");
32+
banner["ext"].should.not.equal("[filebase]");
4133
});
4234

4335
require.include("./test.js");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
},
1414
plugins: [
1515
new webpack.BannerPlugin({
16-
banner: "hash:[hash:10], chunkhash:[chunkhash:10], name:[name], ext:[ext]"
16+
banner: "hash:[hash], chunkhash:[chunkhash], name:[name], ext:[filebase]"
1717
})
1818
]
1919
};

0 commit comments

Comments
 (0)