Skip to content

Commit

Permalink
fix bug without async chunks, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Mar 24, 2018
1 parent c328c65 commit d7a0fc3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
4 changes: 3 additions & 1 deletion lib/TemplatedPathPlugin.js
Expand Up @@ -156,7 +156,9 @@ class TemplatedPathPlugin {
hash.update(JSON.stringify(chunk.getChunkMaps(true).hash));
if (REGEXP_CONTENTHASH_FOR_TEST.test(chunkFilename)) {
hash.update(
JSON.stringify(chunk.getChunkMaps(true).contentHash.javascript)
JSON.stringify(
chunk.getChunkMaps(true).contentHash.javascript || {}
)
);
}
if (REGEXP_NAME_FOR_TEST.test(chunkFilename))
Expand Down
5 changes: 5 additions & 0 deletions test/configCases/hash-length/output-filename/no-async.js
@@ -0,0 +1,5 @@
it("should compile and run the test " + NAME, function() {});

if (Math.random() < -1) {
require(["./chunk"], function() {});
}
33 changes: 20 additions & 13 deletions test/configCases/hash-length/output-filename/test.config.js
Expand Up @@ -3,7 +3,7 @@ require("should");

var findFile = function(files, regex) {
return files.find(function(file) {
if(regex.test(file)) {
if (regex.test(file)) {
return true;
}
});
Expand All @@ -17,23 +17,30 @@ module.exports = {
findBundle: function(i, options) {
var files = fs.readdirSync(options.output.path);

var bundleDetects = [{
regex: new RegExp("^0.bundle" + i, "i"),
expectedNameLength: options.amd.expectedChunkFilenameLength
}, {
regex: new RegExp("^bundle" + i, "i"),
expectedNameLength: options.amd.expectedFilenameLength
}];
var bundleDetects = [
options.amd.expectedChunkFilenameLength && {
regex: new RegExp("^0.bundle" + i, "i"),
expectedNameLength: options.amd.expectedChunkFilenameLength
},
{
regex: new RegExp("^bundle" + i, "i"),
expectedNameLength: options.amd.expectedFilenameLength
}
].filter(Boolean);

var bundleDetect;
var filename;

for(bundleDetect of bundleDetects) {
for (bundleDetect of bundleDetects) {
filename = findFile(files, bundleDetect.regex);
verifyFilenameLength(
filename,
bundleDetect.expectedNameLength
);
if (!filename) {
throw new Error(
`No file found with correct name (regex: ${
bundleDetect.regex.source
}, files: ${files.join(", ")})`
);
}
verifyFilenameLength(filename, bundleDetect.expectedNameLength);
}

return "./" + filename;
Expand Down
52 changes: 52 additions & 0 deletions test/configCases/hash-length/output-filename/webpack.config.js
Expand Up @@ -139,6 +139,58 @@ module.exports = [
expectedFilenameLength: 9 + 7 + 3,
expectedChunkFilenameLength: 2 + 9 + 7 + 3
}
},
{
name: "contenthash in async-node",
output: {
filename: "bundle12.[contenthash].js",
chunkFilename: "[id].bundle12.[contenthash].js"
},
target: "async-node",
amd: {
expectedFilenameLength: 32,
expectedChunkFilenameLength: 34
}
},
{
name: "contenthash in async-node with length",
output: {
filename: "bundle13.[contenthash:7].js",
chunkFilename: "[id].bundle13.[contenthash:7].js"
},
target: "async-node",
amd: {
expectedFilenameLength: 9 + 7 + 3,
expectedChunkFilenameLength: 2 + 9 + 7 + 3
}
},
{
name: "contenthash in webpack",
entry: "./no-async",
output: {
filename: "bundle14.[contenthash].js",
chunkFilename: "[id].bundle14.[contenthash].js",
globalObject: "this"
},
target: "web",
amd: {
expectedFilenameLength: 32,
expectedChunkFilenameLength: 34
}
},
{
name: "contenthash in async-node with length",
entry: "./no-async",
output: {
filename: "bundle15.[contenthash:7].js",
chunkFilename: "[id].bundle15.[contenthash:7].js",
globalObject: "this"
},
target: "web",
amd: {
expectedFilenameLength: 9 + 7 + 3,
expectedChunkFilenameLength: 2 + 9 + 7 + 3
}
}
];

Expand Down

0 comments on commit d7a0fc3

Please sign in to comment.