From 14968acc000d40dcc67b7ba8911dca40ce611f51 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 2 Jul 2018 10:15:13 +0200 Subject: [PATCH 1/3] add automaticNamePrefix option allow automaticNameDelimiter on cacheGroup level --- lib/WebpackOptionsDefaulter.js | 2 ++ lib/optimize/SplitChunksPlugin.js | 33 +++++++++++++++++-------------- schemas/WebpackOptions.json | 9 +++++++++ 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/WebpackOptionsDefaulter.js b/lib/WebpackOptionsDefaulter.js index 3f9da0b72d2..f711b27de2f 100644 --- a/lib/WebpackOptionsDefaulter.js +++ b/lib/WebpackOptionsDefaulter.js @@ -233,11 +233,13 @@ class WebpackOptionsDefaulter extends OptionsDefaulter { this.set("optimization.splitChunks.name", true); this.set("optimization.splitChunks.cacheGroups", {}); this.set("optimization.splitChunks.cacheGroups.default", { + automaticNamePrefix: "", reuseExistingChunk: true, minChunks: 2, priority: -20 }); this.set("optimization.splitChunks.cacheGroups.vendors", { + automaticNamePrefix: "vendors", test: /[\\/]node_modules[\\/]/, priority: -10 }); diff --git a/lib/optimize/SplitChunksPlugin.js b/lib/optimize/SplitChunksPlugin.js index d56e2699399..c33fec9db3e 100644 --- a/lib/optimize/SplitChunksPlugin.js +++ b/lib/optimize/SplitChunksPlugin.js @@ -115,15 +115,11 @@ module.exports = class SplitChunksPlugin { minChunks: options.minChunks || 1, maxAsyncRequests: options.maxAsyncRequests || 1, maxInitialRequests: options.maxInitialRequests || 1, - getName: - SplitChunksPlugin.normalizeName({ - name: options.name, - automaticNameDelimiter: options.automaticNameDelimiter - }) || (() => {}), hidePathInfo: options.hidePathInfo || false, filename: options.filename || undefined, getCacheGroups: SplitChunksPlugin.normalizeCacheGroups({ cacheGroups: options.cacheGroups, + name: options.name, automaticNameDelimiter: options.automaticNameDelimiter }), fallbackCacheGroup: SplitChunksPlugin.normalizeFallbackCacheGroup( @@ -133,7 +129,7 @@ module.exports = class SplitChunksPlugin { }; } - static normalizeName({ name, automaticNameDelimiter }) { + static normalizeName({ name, automaticNameDelimiter, automaticNamePrefix }) { if (name === true) { const cache = new Map(); const fn = (module, chunks, cacheGroup) => { @@ -150,10 +146,12 @@ module.exports = class SplitChunksPlugin { return; } names.sort(); - let name = - (cacheGroup && cacheGroup !== "default" - ? cacheGroup + automaticNameDelimiter - : "") + names.join(automaticNameDelimiter); + const prefix = + typeof automaticNamePrefix === "string" + ? automaticNamePrefix + : cacheGroup; + const namePrefix = prefix ? prefix + automaticNameDelimiter : ""; + let name = namePrefix + names.join(automaticNameDelimiter); // Filenames and paths can't be too long otherwise an // ENAMETOOLONG error is raised. If the generated name if too // long, it is truncated and a hash is appended. The limit has @@ -210,7 +208,7 @@ module.exports = class SplitChunksPlugin { }; } - static normalizeCacheGroups({ cacheGroups, automaticNameDelimiter }) { + static normalizeCacheGroups({ cacheGroups, name, automaticNameDelimiter }) { if (typeof cacheGroups === "function") { // TODO webpack 5 remove this if (cacheGroups.length !== 1) { @@ -249,10 +247,15 @@ module.exports = class SplitChunksPlugin { results.push({ key: key, priority: option.priority, - getName: SplitChunksPlugin.normalizeName({ - name: option.name, - automaticNameDelimiter - }), + getName: + SplitChunksPlugin.normalizeName({ + name: option.name || name, + automaticNameDelimiter: + typeof option.automaticNameDelimiter === "string" + ? option.automaticNameDelimiter + : automaticNameDelimiter, + automaticNamePrefix: option.automaticNamePrefix + }) || (() => {}), chunksFilter: SplitChunksPlugin.normalizeChunksFilter( option.chunks ), diff --git a/schemas/WebpackOptions.json b/schemas/WebpackOptions.json index 7581fe8dd22..0ee1440b1fb 100644 --- a/schemas/WebpackOptions.json +++ b/schemas/WebpackOptions.json @@ -1529,6 +1529,15 @@ } ] }, + "automaticNameDelimiter": { + "description": "Sets the name delimiter for created chunks", + "type": "string", + "minLength": 1 + }, + "automaticNamePrefix": { + "description": "Sets the name prefix for created chunks", + "type": "string" + }, "filename": { "description": "Sets the template for the filename for created chunks (Only works for initial chunks)", "type": "string", From 9344801fb781418bc76eade7f3ce65c9790d488e Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 5 Jul 2018 14:39:07 +0200 Subject: [PATCH 2/3] fix wrong priority handling when merging multiple cache groups by name --- lib/optimize/SplitChunksPlugin.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/optimize/SplitChunksPlugin.js b/lib/optimize/SplitChunksPlugin.js index c33fec9db3e..da1469e0f6a 100644 --- a/lib/optimize/SplitChunksPlugin.js +++ b/lib/optimize/SplitChunksPlugin.js @@ -488,6 +488,12 @@ module.exports = class SplitChunksPlugin { chunksKeys: new Set() }) ); + } else { + if (info.cacheGroup !== cacheGroup) { + if (info.cacheGroup.priority < cacheGroup.priority) { + info.cacheGroup = cacheGroup; + } + } } info.modules.add(module); info.size += module.size(); From 0c2ea9d5d0e50c33547291c178d55e1bc6cf78ad Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 5 Jul 2018 14:39:17 +0200 Subject: [PATCH 3/3] add test case --- .../__snapshots__/StatsTestCases.test.js.snap | 35 ++++++++++++++++++ .../split-chunks-automatic-name/a.js | 5 +++ .../split-chunks-automatic-name/b.js | 5 +++ .../split-chunks-automatic-name/c.js | 5 +++ .../split-chunks-automatic-name/d.js | 1 + .../split-chunks-automatic-name/e.js | 1 + .../split-chunks-automatic-name/f.js | 1 + .../split-chunks-automatic-name/index.js | 3 ++ .../node_modules/x.js | 1 + .../node_modules/y.js | 1 + .../node_modules/z.js | 1 + .../webpack.config.js | 37 +++++++++++++++++++ 12 files changed, 96 insertions(+) create mode 100644 test/statsCases/split-chunks-automatic-name/a.js create mode 100644 test/statsCases/split-chunks-automatic-name/b.js create mode 100644 test/statsCases/split-chunks-automatic-name/c.js create mode 100644 test/statsCases/split-chunks-automatic-name/d.js create mode 100644 test/statsCases/split-chunks-automatic-name/e.js create mode 100644 test/statsCases/split-chunks-automatic-name/f.js create mode 100644 test/statsCases/split-chunks-automatic-name/index.js create mode 100644 test/statsCases/split-chunks-automatic-name/node_modules/x.js create mode 100644 test/statsCases/split-chunks-automatic-name/node_modules/y.js create mode 100644 test/statsCases/split-chunks-automatic-name/node_modules/z.js create mode 100644 test/statsCases/split-chunks-automatic-name/webpack.config.js diff --git a/test/__snapshots__/StatsTestCases.test.js.snap b/test/__snapshots__/StatsTestCases.test.js.snap index ca7b99a36fc..eaa9047160a 100644 --- a/test/__snapshots__/StatsTestCases.test.js.snap +++ b/test/__snapshots__/StatsTestCases.test.js.snap @@ -2671,6 +2671,41 @@ Child custom-chunks-filter-in-cache-groups: [5] ./c.js 72 bytes {3} {8} [built]" `; +exports[`StatsTestCases should print correct stats for split-chunks-automatic-name 1`] = ` +"Entrypoint main = main.js +chunk {0} common~async-a~async-b~async-c.js (common~async-a~async-b~async-c) 40 bytes <{7}> ={1}= ={2}= ={3}= ={4}= ={5}= ={6}= [rendered] split chunk (cache group: vendors) (name: common~async-a~async-b~async-c) + > ./a [8] ./index.js 1:0-47 + > ./b [8] ./index.js 2:0-47 + > ./c [8] ./index.js 3:0-47 + [0] ./d.js 20 bytes {0} [built] + [1] ./node_modules/x.js 20 bytes {0} [built] +chunk {1} common~async-b~async-c.js (common~async-b~async-c) 20 bytes <{7}> ={0}= ={2}= ={4}= ={5}= ={6}= [rendered] split chunk (cache group: vendors) (name: common~async-b~async-c) + > ./b [8] ./index.js 2:0-47 + > ./c [8] ./index.js 3:0-47 + [3] ./f.js 20 bytes {1} [built] +chunk {2} common~async-a~async-b.js (common~async-a~async-b) 20 bytes <{7}> ={0}= ={1}= ={3}= ={4}= [rendered] split chunk (cache group: vendors) (name: common~async-a~async-b) + > ./a [8] ./index.js 1:0-47 + > ./b [8] ./index.js 2:0-47 + [2] ./node_modules/y.js 20 bytes {2} [built] +chunk {3} async-a.js (async-a) 107 bytes <{7}> ={0}= ={2}= [rendered] + > ./a [8] ./index.js 1:0-47 + [7] ./a.js + 1 modules 107 bytes {3} [built] + | ./a.js 72 bytes [built] + | ./e.js 20 bytes [built] +chunk {4} async-b.js (async-b) 72 bytes <{7}> ={0}= ={1}= ={2}= [rendered] + > ./b [8] ./index.js 2:0-47 + [5] ./b.js 72 bytes {4} [built] +chunk {5} async-c.js (async-c) 72 bytes <{7}> ={0}= ={1}= ={6}= [rendered] + > ./c [8] ./index.js 3:0-47 + [6] ./c.js 72 bytes {5} [built] +chunk {6} common~async-c.js (common~async-c) 20 bytes <{7}> ={0}= ={1}= ={5}= [rendered] split chunk (cache group: vendors) (name: common~async-c) + > ./c [8] ./index.js 3:0-47 + [4] ./node_modules/z.js 20 bytes {6} [built] +chunk {7} main.js (main) 147 bytes >{0}< >{1}< >{2}< >{3}< >{4}< >{5}< >{6}< [entry] [rendered] + > ./ main + [8] ./index.js 147 bytes {7} [built]" +`; + exports[`StatsTestCases should print correct stats for split-chunks-combinations 1`] = ` "Entrypoint main = main.js chunk {0} async-a~async-b.js (async-a~async-b) 134 bytes <{8}> ={1}= ={2}= [rendered] split chunk (cache group: default) (name: async-a~async-b) diff --git a/test/statsCases/split-chunks-automatic-name/a.js b/test/statsCases/split-chunks-automatic-name/a.js new file mode 100644 index 00000000000..1fcabdcfdc0 --- /dev/null +++ b/test/statsCases/split-chunks-automatic-name/a.js @@ -0,0 +1,5 @@ +import "./d"; +import "./e"; +import "x"; +import "y"; +export default "a"; diff --git a/test/statsCases/split-chunks-automatic-name/b.js b/test/statsCases/split-chunks-automatic-name/b.js new file mode 100644 index 00000000000..fd909a7b63b --- /dev/null +++ b/test/statsCases/split-chunks-automatic-name/b.js @@ -0,0 +1,5 @@ +import "./d"; +import "./f"; +import "x"; +import "y"; +export default "b"; diff --git a/test/statsCases/split-chunks-automatic-name/c.js b/test/statsCases/split-chunks-automatic-name/c.js new file mode 100644 index 00000000000..6bbf24bfe50 --- /dev/null +++ b/test/statsCases/split-chunks-automatic-name/c.js @@ -0,0 +1,5 @@ +import "./d"; +import "./f"; +import "x"; +import "z"; +export default "c"; diff --git a/test/statsCases/split-chunks-automatic-name/d.js b/test/statsCases/split-chunks-automatic-name/d.js new file mode 100644 index 00000000000..987d6d7e401 --- /dev/null +++ b/test/statsCases/split-chunks-automatic-name/d.js @@ -0,0 +1 @@ +export default "d"; diff --git a/test/statsCases/split-chunks-automatic-name/e.js b/test/statsCases/split-chunks-automatic-name/e.js new file mode 100644 index 00000000000..d97e38b22f5 --- /dev/null +++ b/test/statsCases/split-chunks-automatic-name/e.js @@ -0,0 +1 @@ +export default "e"; diff --git a/test/statsCases/split-chunks-automatic-name/f.js b/test/statsCases/split-chunks-automatic-name/f.js new file mode 100644 index 00000000000..657d4dee8a8 --- /dev/null +++ b/test/statsCases/split-chunks-automatic-name/f.js @@ -0,0 +1 @@ +export default "f"; diff --git a/test/statsCases/split-chunks-automatic-name/index.js b/test/statsCases/split-chunks-automatic-name/index.js new file mode 100644 index 00000000000..5dfec91bc71 --- /dev/null +++ b/test/statsCases/split-chunks-automatic-name/index.js @@ -0,0 +1,3 @@ +import(/* webpackChunkName: "async-a" */ "./a"); +import(/* webpackChunkName: "async-b" */ "./b"); +import(/* webpackChunkName: "async-c" */ "./c"); diff --git a/test/statsCases/split-chunks-automatic-name/node_modules/x.js b/test/statsCases/split-chunks-automatic-name/node_modules/x.js new file mode 100644 index 00000000000..3fd5ecc7a40 --- /dev/null +++ b/test/statsCases/split-chunks-automatic-name/node_modules/x.js @@ -0,0 +1 @@ +export default "x"; diff --git a/test/statsCases/split-chunks-automatic-name/node_modules/y.js b/test/statsCases/split-chunks-automatic-name/node_modules/y.js new file mode 100644 index 00000000000..413e7c09da6 --- /dev/null +++ b/test/statsCases/split-chunks-automatic-name/node_modules/y.js @@ -0,0 +1 @@ +export default "y"; diff --git a/test/statsCases/split-chunks-automatic-name/node_modules/z.js b/test/statsCases/split-chunks-automatic-name/node_modules/z.js new file mode 100644 index 00000000000..0b388750767 --- /dev/null +++ b/test/statsCases/split-chunks-automatic-name/node_modules/z.js @@ -0,0 +1 @@ +export default "z"; diff --git a/test/statsCases/split-chunks-automatic-name/webpack.config.js b/test/statsCases/split-chunks-automatic-name/webpack.config.js new file mode 100644 index 00000000000..8216c7312c7 --- /dev/null +++ b/test/statsCases/split-chunks-automatic-name/webpack.config.js @@ -0,0 +1,37 @@ +const stats = { + hash: false, + timings: false, + builtAt: false, + assets: false, + chunks: true, + chunkOrigins: true, + entrypoints: true, + modules: false +}; +module.exports = { + name: "production", + mode: "production", + entry: { + main: "./" + }, + optimization: { + splitChunks: { + chunks: "all", + minSize: 1, + cacheGroups: { + default: { + automaticNamePrefix: "common", + reuseExistingChunk: true, + minChunks: 2, + priority: -20 + }, + vendors: { + automaticNamePrefix: "common", + test: /[\\/]node_modules[\\/]/, + priority: -10 + } + } + } + }, + stats +};