diff --git a/lib/webworker/WebWorkerMainTemplatePlugin.js b/lib/webworker/WebWorkerMainTemplatePlugin.js index e685470960f..02d3d5d4265 100644 --- a/lib/webworker/WebWorkerMainTemplatePlugin.js +++ b/lib/webworker/WebWorkerMainTemplatePlugin.js @@ -54,6 +54,20 @@ class WebWorkerMainTemplatePlugin { )} + "`, chunk: { id: '" + chunkId + "', + hash: `" + ${JSON.stringify(chunkMaps.hash)}[chunkId] + "`, + hashWithLength(length) { + const shortChunkHashMap = Object.create(null); + for (const chunkId of Object.keys(chunkMaps.hash)) { + if (typeof chunkMaps.hash[chunkId] === "string") { + shortChunkHashMap[chunkId] = chunkMaps.hash[ + chunkId + ].substr(0, length); + } + } + return `" + ${JSON.stringify( + shortChunkHashMap + )}[chunkId] + "`; + }, contentHash: { javascript: `" + ${JSON.stringify( chunkMaps.contentHash.javascript @@ -74,7 +88,10 @@ class WebWorkerMainTemplatePlugin { shortContentHashMap )}[chunkId] + "`; } - } + }, + name: `" + (${JSON.stringify( + chunkMaps.name + )}[chunkId]||chunkId) + "` }, contentHashType: "javascript" }) + diff --git a/test/configCases/issues/issue-7563/index.js b/test/configCases/issues/issue-7563/index.js new file mode 100644 index 00000000000..9e0bf87e832 --- /dev/null +++ b/test/configCases/issues/issue-7563/index.js @@ -0,0 +1,3 @@ +it("should compile without error", function() { + return import(/* webpackChunkName: "one" */ "./one"); +}); diff --git a/test/configCases/issues/issue-7563/one.js b/test/configCases/issues/issue-7563/one.js new file mode 100644 index 00000000000..bd816eaba4c --- /dev/null +++ b/test/configCases/issues/issue-7563/one.js @@ -0,0 +1 @@ +module.exports = 1; diff --git a/test/configCases/issues/issue-7563/test.config.js b/test/configCases/issues/issue-7563/test.config.js new file mode 100644 index 00000000000..dee26555271 --- /dev/null +++ b/test/configCases/issues/issue-7563/test.config.js @@ -0,0 +1,22 @@ +var fs = require('fs'); + +module.exports = { + noTests: true, + findBundle: function(i, options) { + var regex = new RegExp("^bundle\." + options.name, "i"); + var files = fs.readdirSync(options.output.path); + var bundle = files.find(function (file) { + return regex.test(file); + }); + + if (!bundle) { + throw new Error( + `No file found with correct name (regex: ${ + regex.source + }, files: ${files.join(", ")})` + ); + } + + return "./" + bundle; + } +}; diff --git a/test/configCases/issues/issue-7563/webpack.config.js b/test/configCases/issues/issue-7563/webpack.config.js new file mode 100644 index 00000000000..09164011712 --- /dev/null +++ b/test/configCases/issues/issue-7563/webpack.config.js @@ -0,0 +1,65 @@ +"use strict"; + +// Have to test [hash] and [chunkhash] separately to avoid +// "Cannot use [chunkhash] or [contenthash] for chunk in 'bundle1.[hash].[hash:16].[chunkhash].[chunkhash:16].[name].[id].[query].js' (use [hash] instead)" +var testAllButHash = "[chunkhash].[chunkhash:16].[name].[id].[query]"; +var testHash = "[hash].[hash:16]"; + +module.exports = [ + { + name: "webworker-all", + target: "webworker", + output: { + filename: "bundle.webworker-all." + testAllButHash + ".js" + } + }, + { + name: "webworker-hash", + target: "webworker", + output: { + filename: "bundle.webworker-hash." + testHash + ".js" + } + }, + { + name: "node-all", + target: "node", + output: { + filename: "bundle.node-all." + testAllButHash + ".js" + } + }, + { + name: "node", + target: "node", + output: { + filename: "bundle.node-hash." + testHash + ".js" + } + }, + { + name: "async-node-all", + target: "async-node", + output: { + filename: "bundle.async-node-all." + testAllButHash + ".js" + } + }, + { + name: "async-node-hash", + target: "async-node", + output: { + filename: "bundle.async-node-hash." + testHash + ".js" + } + }, + { + name: "web-all", + target: "web", + output: { + filename: "bundle.web-all." + testAllButHash + ".js" + } + }, + { + name: "web-hash", + target: "web", + output: { + filename: "bundle.web-hash." + testHash + ".js" + } + } +];