diff --git a/lib/TemplatedPathPlugin.js b/lib/TemplatedPathPlugin.js index 2438432dc97..27273bd6316 100644 --- a/lib/TemplatedPathPlugin.js +++ b/lib/TemplatedPathPlugin.js @@ -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)) diff --git a/test/configCases/hash-length/output-filename/no-async.js b/test/configCases/hash-length/output-filename/no-async.js new file mode 100644 index 00000000000..e3bce91d575 --- /dev/null +++ b/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() {}); +} diff --git a/test/configCases/hash-length/output-filename/test.config.js b/test/configCases/hash-length/output-filename/test.config.js index 5ed043b07e1..db9bb3bf1ec 100644 --- a/test/configCases/hash-length/output-filename/test.config.js +++ b/test/configCases/hash-length/output-filename/test.config.js @@ -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; } }); @@ -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; diff --git a/test/configCases/hash-length/output-filename/webpack.config.js b/test/configCases/hash-length/output-filename/webpack.config.js index 5b480e34b82..971f6ee6ebb 100644 --- a/test/configCases/hash-length/output-filename/webpack.config.js +++ b/test/configCases/hash-length/output-filename/webpack.config.js @@ -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 + } } ];