Skip to content

Commit d7a0fc3

Browse files
committed
fix bug without async chunks, add more tests
1 parent c328c65 commit d7a0fc3

File tree

4 files changed

+80
-14
lines changed

4 files changed

+80
-14
lines changed

lib/TemplatedPathPlugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ class TemplatedPathPlugin {
156156
hash.update(JSON.stringify(chunk.getChunkMaps(true).hash));
157157
if (REGEXP_CONTENTHASH_FOR_TEST.test(chunkFilename)) {
158158
hash.update(
159-
JSON.stringify(chunk.getChunkMaps(true).contentHash.javascript)
159+
JSON.stringify(
160+
chunk.getChunkMaps(true).contentHash.javascript || {}
161+
)
160162
);
161163
}
162164
if (REGEXP_NAME_FOR_TEST.test(chunkFilename))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
it("should compile and run the test " + NAME, function() {});
2+
3+
if (Math.random() < -1) {
4+
require(["./chunk"], function() {});
5+
}

test/configCases/hash-length/output-filename/test.config.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require("should");
33

44
var findFile = function(files, regex) {
55
return files.find(function(file) {
6-
if(regex.test(file)) {
6+
if (regex.test(file)) {
77
return true;
88
}
99
});
@@ -17,23 +17,30 @@ module.exports = {
1717
findBundle: function(i, options) {
1818
var files = fs.readdirSync(options.output.path);
1919

20-
var bundleDetects = [{
21-
regex: new RegExp("^0.bundle" + i, "i"),
22-
expectedNameLength: options.amd.expectedChunkFilenameLength
23-
}, {
24-
regex: new RegExp("^bundle" + i, "i"),
25-
expectedNameLength: options.amd.expectedFilenameLength
26-
}];
20+
var bundleDetects = [
21+
options.amd.expectedChunkFilenameLength && {
22+
regex: new RegExp("^0.bundle" + i, "i"),
23+
expectedNameLength: options.amd.expectedChunkFilenameLength
24+
},
25+
{
26+
regex: new RegExp("^bundle" + i, "i"),
27+
expectedNameLength: options.amd.expectedFilenameLength
28+
}
29+
].filter(Boolean);
2730

2831
var bundleDetect;
2932
var filename;
3033

31-
for(bundleDetect of bundleDetects) {
34+
for (bundleDetect of bundleDetects) {
3235
filename = findFile(files, bundleDetect.regex);
33-
verifyFilenameLength(
34-
filename,
35-
bundleDetect.expectedNameLength
36-
);
36+
if (!filename) {
37+
throw new Error(
38+
`No file found with correct name (regex: ${
39+
bundleDetect.regex.source
40+
}, files: ${files.join(", ")})`
41+
);
42+
}
43+
verifyFilenameLength(filename, bundleDetect.expectedNameLength);
3744
}
3845

3946
return "./" + filename;

test/configCases/hash-length/output-filename/webpack.config.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,58 @@ module.exports = [
139139
expectedFilenameLength: 9 + 7 + 3,
140140
expectedChunkFilenameLength: 2 + 9 + 7 + 3
141141
}
142+
},
143+
{
144+
name: "contenthash in async-node",
145+
output: {
146+
filename: "bundle12.[contenthash].js",
147+
chunkFilename: "[id].bundle12.[contenthash].js"
148+
},
149+
target: "async-node",
150+
amd: {
151+
expectedFilenameLength: 32,
152+
expectedChunkFilenameLength: 34
153+
}
154+
},
155+
{
156+
name: "contenthash in async-node with length",
157+
output: {
158+
filename: "bundle13.[contenthash:7].js",
159+
chunkFilename: "[id].bundle13.[contenthash:7].js"
160+
},
161+
target: "async-node",
162+
amd: {
163+
expectedFilenameLength: 9 + 7 + 3,
164+
expectedChunkFilenameLength: 2 + 9 + 7 + 3
165+
}
166+
},
167+
{
168+
name: "contenthash in webpack",
169+
entry: "./no-async",
170+
output: {
171+
filename: "bundle14.[contenthash].js",
172+
chunkFilename: "[id].bundle14.[contenthash].js",
173+
globalObject: "this"
174+
},
175+
target: "web",
176+
amd: {
177+
expectedFilenameLength: 32,
178+
expectedChunkFilenameLength: 34
179+
}
180+
},
181+
{
182+
name: "contenthash in async-node with length",
183+
entry: "./no-async",
184+
output: {
185+
filename: "bundle15.[contenthash:7].js",
186+
chunkFilename: "[id].bundle15.[contenthash:7].js",
187+
globalObject: "this"
188+
},
189+
target: "web",
190+
amd: {
191+
expectedFilenameLength: 9 + 7 + 3,
192+
expectedChunkFilenameLength: 2 + 9 + 7 + 3
193+
}
142194
}
143195
];
144196

0 commit comments

Comments
 (0)