Skip to content

Commit 6456932

Browse files
committed
finished intperpolate & tests
1 parent e8ab398 commit 6456932

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

lib/BannerPlugin.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
"use strict";
77

8-
const path = require("path");
98
const ConcatSource = require("webpack-sources").ConcatSource;
109
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
1110

@@ -25,8 +24,7 @@ function wrapComment(str) {
2524
const REGEXP_HASH = /\[hash:?(\d+)?\]/gi,
2625
REGEXP_CHUNKHASH = /\[chunkhash:?(\d+)?\]/gi,
2726
REGEXP_NAME = /\[name\]/gi,
28-
REGEXP_EXT = /\[ext\]/gi,
29-
REGEXP_PATH = /\[path\]/gi;
27+
REGEXP_EXT = /\[ext\]/gi;
3028

3129
// withHashLength, getReplacer from lib/TemplatedPathPlugin.js
3230
const withHashLength = (replacer, handlerFn) => {
@@ -62,7 +60,7 @@ const interpolate = (banner, file, hash, chunkHash) => {
6260
.replace(REGEXP_HASH, withHashLength(getReplacer(hash)))
6361
.replace(REGEXP_CHUNKHASH, withHashLength(getReplacer(chunkHash)))
6462
.replace(REGEXP_NAME, name)
65-
.replace(REGEXP_EXT, ext)
63+
.replace(REGEXP_EXT, ext);
6664
}
6765

6866
class BannerPlugin {
@@ -88,10 +86,9 @@ class BannerPlugin {
8886
chunk.files
8987
.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options))
9088
.forEach((file) => {
91-
const chunkHash = chunk.renderedHash || chunk.hash
92-
const comment = interpolate(banner, file, compilation.hash, chunkHash)
93-
console.log('comment: ', comment);
94-
return compilation.assets[file] = new ConcatSource(comment, "\n", compilation.assets[file])
89+
const chunkHash = chunk.renderedHash || chunk.hash;
90+
const comment = interpolate(banner, file, compilation.hash, chunkHash);
91+
return compilation.assets[file] = new ConcatSource(comment, "\n", compilation.assets[file]);
9592
});
9693
});
9794
callback();
-12 KB
Binary file not shown.
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const parseBanner = (banner) => {
22
return banner
3-
.slice(4,-3) // remove comment syntax
4-
.split(", ") // into key:val pairs
5-
.map(n => n.split(":")) //[key, val]
3+
.slice(4,-3)
4+
.split(", ")
5+
.map(n => n.split(":"))
66
.reduce((acc, val) => {
77
acc[val[0]] = val[1];
88
return acc;
9-
}, {}); // { key: val }
9+
}, {});
1010
};
1111

1212
var source = require("fs")
@@ -16,12 +16,20 @@ var source = require("fs")
1616

1717
const banner = parseBanner(source)
1818

19-
it("should interpolate file hash in bundle0 chunk", function() {
20-
banner["hash"].should.not.containEql("[hash]");
19+
it("should interpolate file hash in bundle0 chunk", () => {
20+
banner["hash"].should.not.equal("[hash]");
2121
});
2222

23-
it("should interpolate chunkHash in bundle0 chunk", function() {
24-
banner["chunkhash"].should.not.containEql("[chunkhash]");
23+
it("should interpolate chunkHash in bundle0 chunk", () => {
24+
banner["chunkhash"].should.not.equal("[chunkhash]");
25+
});
26+
27+
it("should interpolate name in bundle0 chunk", () => {
28+
banner["name"].should.not.equal("[name]");
29+
});
30+
31+
it("should interpolate extension in bundle0 chunk", () => {
32+
banner["ext"].should.not.equal("[ext]");
2533
});
2634

2735
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], chunkhash:[chunkhash], name:[name], ext:[ext]"
16+
banner: "hash:[hash:10], chunkhash:[chunkhash:10], name:[name], ext:[ext]"
1717
})
1818
]
1919
};

0 commit comments

Comments
 (0)