webpack 4.9.1 → 4.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/README.md +755 -755
  2. package/SECURITY.md +9 -9
  3. package/buildin/global.js +20 -20
  4. package/buildin/harmony-module.js +24 -24
  5. package/buildin/module.js +22 -22
  6. package/hot/dev-server.js +61 -61
  7. package/hot/log-apply-result.js +44 -44
  8. package/hot/log.js +45 -45
  9. package/hot/only-dev-server.js +105 -105
  10. package/hot/poll.js +37 -37
  11. package/hot/signal.js +62 -62
  12. package/lib/APIPlugin.js +84 -84
  13. package/lib/AmdMainTemplatePlugin.js +87 -75
  14. package/lib/AsyncDependenciesBlock.js +66 -66
  15. package/lib/AsyncDependencyToInitialChunkError.js +31 -21
  16. package/lib/AutomaticPrefetchPlugin.js +50 -50
  17. package/lib/BannerPlugin.js +3 -1
  18. package/lib/BasicEvaluatedExpression.js +211 -208
  19. package/lib/CachePlugin.js +102 -95
  20. package/lib/CaseSensitiveModulesWarning.js +71 -53
  21. package/lib/Chunk.js +750 -722
  22. package/lib/ChunkGroup.js +13 -5
  23. package/lib/ChunkRenderError.js +32 -32
  24. package/lib/CompatibilityPlugin.js +63 -63
  25. package/lib/Compilation.js +1947 -1905
  26. package/lib/Compiler.js +508 -496
  27. package/lib/ConcurrentCompilationError.js +19 -19
  28. package/lib/ConstPlugin.js +258 -242
  29. package/lib/ContextExclusionPlugin.js +17 -17
  30. package/lib/ContextModule.js +749 -710
  31. package/lib/ContextModuleFactory.js +256 -245
  32. package/lib/ContextReplacementPlugin.js +133 -126
  33. package/lib/DefinePlugin.js +206 -197
  34. package/lib/DelegatedModule.js +101 -101
  35. package/lib/DelegatedModuleFactoryPlugin.js +89 -89
  36. package/lib/DelegatedPlugin.js +39 -39
  37. package/lib/DependenciesBlock.js +89 -87
  38. package/lib/DependenciesBlockVariable.js +52 -51
  39. package/lib/Dependency.js +51 -51
  40. package/lib/DllEntryPlugin.js +51 -51
  41. package/lib/DllModule.js +54 -54
  42. package/lib/DllModuleFactory.js +29 -29
  43. package/lib/DllPlugin.js +44 -42
  44. package/lib/DllReferencePlugin.js +84 -84
  45. package/lib/DynamicEntryPlugin.js +73 -71
  46. package/lib/EntryOptionPlugin.js +33 -33
  47. package/lib/EnvironmentPlugin.js +65 -65
  48. package/lib/ErrorHelpers.js +60 -57
  49. package/lib/EvalDevToolModulePlugin.js +27 -27
  50. package/lib/EvalDevToolModuleTemplatePlugin.js +61 -61
  51. package/lib/EvalSourceMapDevToolPlugin.js +41 -40
  52. package/lib/ExportPropertyMainTemplatePlugin.js +53 -40
  53. package/lib/ExtendedAPIPlugin.js +84 -84
  54. package/lib/ExternalModule.js +159 -159
  55. package/lib/ExternalModuleFactoryPlugin.js +110 -110
  56. package/lib/ExternalsPlugin.js +23 -23
  57. package/lib/FlagDependencyExportsPlugin.js +146 -146
  58. package/lib/FlagDependencyUsagePlugin.js +110 -104
  59. package/lib/FlagInitialModulesAsUsedPlugin.js +36 -36
  60. package/lib/FunctionModulePlugin.js +19 -19
  61. package/lib/FunctionModuleTemplatePlugin.js +100 -98
  62. package/lib/GraphHelpers.js +64 -64
  63. package/lib/HarmonyLinkingError.js +18 -18
  64. package/lib/HashedModuleIdsPlugin.js +53 -53
  65. package/lib/HotModuleReplacement.runtime.js +7 -3
  66. package/lib/HotModuleReplacementPlugin.js +413 -406
  67. package/lib/HotUpdateChunk.js +16 -16
  68. package/lib/HotUpdateChunkTemplate.js +78 -78
  69. package/lib/IgnorePlugin.js +71 -71
  70. package/lib/JavascriptGenerator.js +229 -228
  71. package/lib/JavascriptModulesPlugin.js +179 -184
  72. package/lib/JsonGenerator.js +42 -42
  73. package/lib/JsonModulesPlugin.js +30 -30
  74. package/lib/JsonParser.js +27 -26
  75. package/lib/LibManifestPlugin.js +86 -86
  76. package/lib/LibraryTemplatePlugin.js +153 -119
  77. package/lib/LoaderOptionsPlugin.js +53 -52
  78. package/lib/LoaderTargetPlugin.js +24 -24
  79. package/lib/MainTemplate.js +34 -9
  80. package/lib/Module.js +381 -377
  81. package/lib/ModuleBuildError.js +42 -42
  82. package/lib/ModuleDependencyError.js +35 -25
  83. package/lib/ModuleDependencyWarning.js +25 -25
  84. package/lib/ModuleError.js +28 -28
  85. package/lib/ModuleFilenameHelpers.js +178 -166
  86. package/lib/ModuleParseError.js +44 -44
  87. package/lib/ModuleReason.js +40 -40
  88. package/lib/ModuleTemplate.js +84 -84
  89. package/lib/ModuleWarning.js +30 -30
  90. package/lib/MultiCompiler.js +283 -271
  91. package/lib/MultiEntryPlugin.js +58 -58
  92. package/lib/MultiModule.js +81 -78
  93. package/lib/MultiModuleFactory.js +23 -23
  94. package/lib/MultiStats.js +92 -92
  95. package/lib/MultiWatching.js +38 -38
  96. package/lib/NamedChunksPlugin.js +29 -29
  97. package/lib/NamedModulesPlugin.js +57 -57
  98. package/lib/NoEmitOnErrorsPlugin.js +20 -20
  99. package/lib/NoModeWarning.js +23 -23
  100. package/lib/NodeStuffPlugin.js +179 -178
  101. package/lib/NormalModule.js +497 -490
  102. package/lib/NormalModuleFactory.js +501 -483
  103. package/lib/NormalModuleReplacementPlugin.js +51 -51
  104. package/lib/OptionsDefaulter.js +84 -80
  105. package/lib/Parser.js +2164 -2086
  106. package/lib/ParserHelpers.js +103 -100
  107. package/lib/PrefetchPlugin.js +37 -37
  108. package/lib/ProgressPlugin.js +231 -231
  109. package/lib/ProvidePlugin.js +86 -86
  110. package/lib/RawModule.js +56 -54
  111. package/lib/RecordIdsPlugin.js +166 -162
  112. package/lib/RemovedPluginError.js +13 -13
  113. package/lib/RequestShortener.js +81 -74
  114. package/lib/RequireJsStuffPlugin.js +69 -69
  115. package/lib/ResolverFactory.js +64 -64
  116. package/lib/RuleSet.js +555 -534
  117. package/lib/RuntimeTemplate.js +320 -277
  118. package/lib/SetVarMainTemplatePlugin.js +69 -57
  119. package/lib/SingleEntryPlugin.js +44 -44
  120. package/lib/SizeFormatHelpers.js +24 -24
  121. package/lib/SourceMapDevToolModuleOptionsPlugin.js +49 -49
  122. package/lib/SourceMapDevToolPlugin.js +301 -300
  123. package/lib/Stats.js +1408 -1367
  124. package/lib/Template.js +4 -2
  125. package/lib/TemplatedPathPlugin.js +173 -170
  126. package/lib/UmdMainTemplatePlugin.js +304 -264
  127. package/lib/UseStrictPlugin.js +48 -48
  128. package/lib/WarnCaseSensitiveModulesPlugin.js +37 -36
  129. package/lib/WarnNoModeSetPlugin.js +17 -17
  130. package/lib/WatchIgnorePlugin.js +100 -100
  131. package/lib/Watching.js +194 -193
  132. package/lib/WebpackError.js +25 -19
  133. package/lib/WebpackOptionsApply.js +421 -405
  134. package/lib/WebpackOptionsDefaulter.js +347 -344
  135. package/lib/WebpackOptionsValidationError.js +345 -316
  136. package/lib/compareLocations.js +56 -56
  137. package/lib/dependencies/AMDDefineDependency.js +137 -137
  138. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +336 -327
  139. package/lib/dependencies/AMDPlugin.js +250 -250
  140. package/lib/dependencies/AMDRequireArrayDependency.js +49 -49
  141. package/lib/dependencies/AMDRequireContextDependency.js +20 -20
  142. package/lib/dependencies/AMDRequireDependenciesBlock.js +43 -43
  143. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +273 -270
  144. package/lib/dependencies/AMDRequireDependency.js +135 -135
  145. package/lib/dependencies/CommonJsPlugin.js +161 -161
  146. package/lib/dependencies/CommonJsRequireContextDependency.js +23 -23
  147. package/lib/dependencies/CommonJsRequireDependencyParserPlugin.js +130 -130
  148. package/lib/dependencies/ConstDependency.js +33 -33
  149. package/lib/dependencies/ContextDependency.js +68 -68
  150. package/lib/dependencies/ContextDependencyTemplateAsId.js +42 -42
  151. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +38 -38
  152. package/lib/dependencies/ContextElementDependency.js +21 -21
  153. package/lib/dependencies/DelegatedExportsDependency.js +33 -33
  154. package/lib/dependencies/DependencyReference.js +18 -18
  155. package/lib/dependencies/HarmonyAcceptDependency.js +45 -45
  156. package/lib/dependencies/HarmonyAcceptImportDependency.js +23 -23
  157. package/lib/dependencies/HarmonyCompatibilityDependency.js +31 -31
  158. package/lib/dependencies/HarmonyDetectionParserPlugin.js +92 -90
  159. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +139 -139
  160. package/lib/dependencies/HarmonyExportExpressionDependency.js +53 -53
  161. package/lib/dependencies/HarmonyExportHeaderDependency.js +30 -30
  162. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +620 -603
  163. package/lib/dependencies/HarmonyExportSpecifierDependency.js +54 -54
  164. package/lib/dependencies/HarmonyImportDependency.js +104 -94
  165. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +217 -214
  166. package/lib/dependencies/HarmonyImportSideEffectDependency.js +31 -31
  167. package/lib/dependencies/HarmonyImportSpecifierDependency.js +166 -156
  168. package/lib/dependencies/HarmonyInitDependency.js +60 -60
  169. package/lib/dependencies/HarmonyModulesPlugin.js +146 -146
  170. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +26 -26
  171. package/lib/dependencies/ImportContextDependency.js +23 -23
  172. package/lib/dependencies/ImportDependenciesBlock.js +18 -18
  173. package/lib/dependencies/ImportDependency.js +34 -34
  174. package/lib/dependencies/ImportEagerDependency.js +32 -32
  175. package/lib/dependencies/ImportParserPlugin.js +233 -232
  176. package/lib/dependencies/ImportPlugin.js +82 -82
  177. package/lib/dependencies/ImportWeakDependency.js +34 -34
  178. package/lib/dependencies/JsonExportsDependency.js +26 -26
  179. package/lib/dependencies/LoaderPlugin.js +98 -93
  180. package/lib/dependencies/LocalModuleDependency.js +28 -28
  181. package/lib/dependencies/LocalModulesHelpers.js +52 -45
  182. package/lib/dependencies/ModuleDependency.js +20 -20
  183. package/lib/dependencies/ModuleDependencyTemplateAsId.js +17 -17
  184. package/lib/dependencies/ModuleDependencyTemplateAsRequireId.js +17 -17
  185. package/lib/dependencies/MultiEntryDependency.js +20 -20
  186. package/lib/dependencies/NullDependency.js +20 -20
  187. package/lib/dependencies/RequireContextDependency.js +22 -22
  188. package/lib/dependencies/RequireContextDependencyParserPlugin.js +56 -56
  189. package/lib/dependencies/RequireContextPlugin.js +143 -141
  190. package/lib/dependencies/RequireEnsureDependenciesBlock.js +33 -33
  191. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +116 -112
  192. package/lib/dependencies/RequireEnsureDependency.js +58 -58
  193. package/lib/dependencies/RequireEnsurePlugin.js +74 -74
  194. package/lib/dependencies/RequireHeaderDependency.js +26 -26
  195. package/lib/dependencies/RequireIncludeDependency.js +39 -39
  196. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +23 -23
  197. package/lib/dependencies/RequireIncludePlugin.js +61 -61
  198. package/lib/dependencies/RequireResolveContextDependency.js +23 -23
  199. package/lib/dependencies/RequireResolveDependencyParserPlugin.js +85 -85
  200. package/lib/dependencies/RequireResolveHeaderDependency.js +26 -26
  201. package/lib/dependencies/SingleEntryDependency.js +18 -18
  202. package/lib/dependencies/SystemPlugin.js +125 -125
  203. package/lib/dependencies/UnsupportedDependency.js +27 -27
  204. package/lib/dependencies/WebAssemblyImportDependency.js +48 -44
  205. package/lib/dependencies/WebpackMissingModule.js +20 -20
  206. package/lib/dependencies/getFunctionExpression.js +52 -52
  207. package/lib/formatLocation.js +61 -53
  208. package/lib/node/NodeChunkTemplatePlugin.js +31 -31
  209. package/lib/node/NodeEnvironmentPlugin.js +28 -28
  210. package/lib/node/NodeHotUpdateChunkTemplatePlugin.js +36 -36
  211. package/lib/node/NodeMainTemplate.runtime.js +27 -27
  212. package/lib/node/NodeMainTemplateAsync.runtime.js +44 -44
  213. package/lib/node/NodeMainTemplatePlugin.js +323 -320
  214. package/lib/node/NodeSourcePlugin.js +144 -140
  215. package/lib/node/NodeTargetPlugin.js +18 -18
  216. package/lib/node/NodeTemplatePlugin.js +31 -31
  217. package/lib/node/NodeWatchFileSystem.js +99 -82
  218. package/lib/node/ReadFileCompileWasmTemplatePlugin.js +52 -52
  219. package/lib/optimize/AggressiveMergingPlugin.js +87 -87
  220. package/lib/optimize/AggressiveSplittingPlugin.js +287 -281
  221. package/lib/optimize/ChunkModuleIdRangePlugin.js +68 -68
  222. package/lib/optimize/ConcatenatedModule.js +1420 -1413
  223. package/lib/optimize/EnsureChunkConditionsPlugin.js +70 -70
  224. package/lib/optimize/FlagIncludedChunksPlugin.js +99 -99
  225. package/lib/optimize/LimitChunkCountPlugin.js +66 -66
  226. package/lib/optimize/MergeDuplicateChunksPlugin.js +78 -75
  227. package/lib/optimize/MinChunkSizePlugin.js +77 -77
  228. package/lib/optimize/ModuleConcatenationPlugin.js +470 -457
  229. package/lib/optimize/OccurrenceOrderPlugin.js +133 -126
  230. package/lib/optimize/RemoveParentModulesPlugin.js +127 -117
  231. package/lib/optimize/RuntimeChunkPlugin.js +41 -41
  232. package/lib/optimize/SideEffectsFlagPlugin.js +158 -156
  233. package/lib/optimize/SplitChunksPlugin.js +709 -696
  234. package/lib/performance/AssetsOverSizeLimitWarning.js +30 -30
  235. package/lib/performance/EntrypointsOverSizeLimitWarning.js +31 -31
  236. package/lib/performance/NoAsyncChunksWarning.js +21 -21
  237. package/lib/performance/SizeLimitsPlugin.js +105 -105
  238. package/lib/util/Semaphore.js +41 -41
  239. package/lib/util/SortableSet.js +5 -2
  240. package/lib/util/StackedSetMap.js +12 -5
  241. package/lib/util/TrackingSet.js +35 -35
  242. package/lib/util/cachedMerge.js +35 -35
  243. package/lib/util/createHash.js +77 -77
  244. package/lib/util/identifier.js +76 -76
  245. package/lib/validateSchema.js +67 -67
  246. package/lib/wasm/UnsupportedWebAssemblyFeatureError.js +18 -18
  247. package/lib/wasm/WasmMainTemplatePlugin.js +310 -304
  248. package/lib/wasm/WebAssemblyGenerator.js +143 -19
  249. package/lib/wasm/WebAssemblyJavascriptGenerator.js +90 -107
  250. package/lib/wasm/WebAssemblyModulesPlugin.js +80 -80
  251. package/lib/wasm/WebAssemblyParser.js +28 -5
  252. package/lib/wasm/WebAssemblyUtils.js +48 -0
  253. package/lib/web/FetchCompileWasmTemplatePlugin.js +25 -25
  254. package/lib/web/JsonpChunkTemplatePlugin.js +47 -47
  255. package/lib/web/JsonpExportMainTemplatePlugin.js +47 -47
  256. package/lib/web/JsonpHotUpdateChunkTemplatePlugin.js +39 -39
  257. package/lib/web/JsonpMainTemplate.runtime.js +65 -64
  258. package/lib/web/JsonpMainTemplatePlugin.js +576 -574
  259. package/lib/web/JsonpTemplatePlugin.js +23 -23
  260. package/lib/webpack.js +183 -182
  261. package/lib/webpack.web.js +31 -31
  262. package/lib/webworker/WebWorkerChunkTemplatePlugin.js +35 -35
  263. package/lib/webworker/WebWorkerHotUpdateChunkTemplatePlugin.js +40 -40
  264. package/lib/webworker/WebWorkerMainTemplate.runtime.js +65 -64
  265. package/lib/webworker/WebWorkerMainTemplatePlugin.js +179 -179
  266. package/lib/webworker/WebWorkerTemplatePlugin.js +25 -25
  267. package/package.json +9 -8
  268. package/schemas/WebpackOptions.json +1988 -1988
  269. package/schemas/ajv.absolutePath.js +55 -55
  270. package/schemas/plugins/DllPlugin.json +32 -32
  271. package/schemas/plugins/DllReferencePlugin.json +99 -99
  272. package/schemas/plugins/HashedModuleIdsPlugin.json +24 -24
  273. package/schemas/plugins/LoaderOptionsPlugin.json +26 -26
  274. package/schemas/plugins/SourceMapDevToolPlugin.json +187 -187
  275. package/schemas/plugins/WatchIgnorePlugin.json +16 -16
  276. package/schemas/plugins/debug/ProfilingPlugin.json +12 -12
  277. package/schemas/plugins/optimize/AggressiveSplittingPlugin.json +22 -22
  278. package/schemas/plugins/optimize/LimitChunkCountPlugin.json +15 -15
  279. package/schemas/plugins/optimize/MinChunkSizePlugin.json +13 -13
@@ -1,696 +1,709 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- "use strict";
6
-
7
- const crypto = require("crypto");
8
- const SortableSet = require("../util/SortableSet");
9
- const GraphHelpers = require("../GraphHelpers");
10
- const { isSubset } = require("../util/SetHelpers");
11
-
12
- /** @typedef {import("../Chunk")} Chunk */
13
- /** @typedef {import("../Module")} Module */
14
-
15
- const hashFilename = name => {
16
- return crypto
17
- .createHash("md4")
18
- .update(name)
19
- .digest("hex")
20
- .slice(0, 8);
21
- };
22
-
23
- const sortByIdentifier = (a, b) => {
24
- if (a.identifier() > b.identifier()) return 1;
25
- if (a.identifier() < b.identifier()) return -1;
26
- return 0;
27
- };
28
-
29
- const getRequests = chunk => {
30
- let requests = 0;
31
- for (const chunkGroup of chunk.groupsIterable) {
32
- requests = Math.max(requests, chunkGroup.chunks.length);
33
- }
34
- return requests;
35
- };
36
-
37
- const getModulesSize = modules => {
38
- let sum = 0;
39
- for (const m of modules) sum += m.size();
40
- return sum;
41
- };
42
-
43
- const isOverlap = (a, b) => {
44
- for (const item of a.keys()) {
45
- if (b.has(item)) return true;
46
- }
47
- return false;
48
- };
49
-
50
- const compareEntries = (a, b) => {
51
- // 1. by priority
52
- const diffPriority = a.cacheGroup.priority - b.cacheGroup.priority;
53
- if (diffPriority) return diffPriority;
54
- // 2. by number of chunks
55
- const diffCount = a.chunks.size - b.chunks.size;
56
- if (diffCount) return diffCount;
57
- // 3. by size reduction
58
- const aSizeReduce = a.size * (a.chunks.size - 1);
59
- const bSizeReduce = b.size * (b.chunks.size - 1);
60
- const diffSizeReduce = aSizeReduce - bSizeReduce;
61
- if (diffSizeReduce) return diffSizeReduce;
62
- // 4. by number of modules (to be able to compare by identifier)
63
- const modulesA = a.modules;
64
- const modulesB = b.modules;
65
- const diff = modulesA.size - modulesB.size;
66
- if (diff) return diff;
67
- // 5. by module identifiers
68
- modulesA.sort();
69
- modulesB.sort();
70
- const aI = modulesA[Symbol.iterator]();
71
- const bI = modulesB[Symbol.iterator]();
72
- // eslint-disable-next-line no-constant-condition
73
- while (true) {
74
- const aItem = aI.next();
75
- const bItem = bI.next();
76
- if (aItem.done) return 0;
77
- const aModuleIdentifier = aItem.value.identifier();
78
- const bModuleIdentifier = bItem.value.identifier();
79
- if (aModuleIdentifier > bModuleIdentifier) return -1;
80
- if (aModuleIdentifier < bModuleIdentifier) return 1;
81
- }
82
- };
83
-
84
- const INITIAL_CHUNK_FILTER = chunk => chunk.canBeInitial();
85
- const ASYNC_CHUNK_FILTER = chunk => !chunk.canBeInitial();
86
- const ALL_CHUNK_FILTER = chunk => true;
87
-
88
- module.exports = class SplitChunksPlugin {
89
- constructor(options) {
90
- this.options = SplitChunksPlugin.normalizeOptions(options);
91
- }
92
-
93
- static normalizeOptions(options = {}) {
94
- return {
95
- chunksFilter: SplitChunksPlugin.normalizeChunksFilter(
96
- options.chunks || "all"
97
- ),
98
- minSize: options.minSize || 0,
99
- minChunks: options.minChunks || 1,
100
- maxAsyncRequests: options.maxAsyncRequests || 1,
101
- maxInitialRequests: options.maxInitialRequests || 1,
102
- getName:
103
- SplitChunksPlugin.normalizeName({
104
- name: options.name,
105
- automaticNameDelimiter: options.automaticNameDelimiter
106
- }) || (() => {}),
107
- filename: options.filename || undefined,
108
- getCacheGroups: SplitChunksPlugin.normalizeCacheGroups({
109
- cacheGroups: options.cacheGroups,
110
- automaticNameDelimiter: options.automaticNameDelimiter
111
- })
112
- };
113
- }
114
-
115
- static normalizeName({ name, automaticNameDelimiter }) {
116
- if (name === true) {
117
- const cache = new Map();
118
- const fn = (module, chunks, cacheGroup) => {
119
- let cacheEntry = cache.get(chunks);
120
- if (cacheEntry === undefined) {
121
- cacheEntry = {};
122
- cache.set(chunks, cacheEntry);
123
- } else if (cacheGroup in cacheEntry) {
124
- return cacheEntry[cacheGroup];
125
- }
126
- const names = chunks.map(c => c.name);
127
- if (!names.every(Boolean)) {
128
- cacheEntry[cacheGroup] = undefined;
129
- return;
130
- }
131
- names.sort();
132
- let name =
133
- (cacheGroup && cacheGroup !== "default"
134
- ? cacheGroup + automaticNameDelimiter
135
- : "") + names.join(automaticNameDelimiter);
136
- // Filenames and paths can't be too long otherwise an
137
- // ENAMETOOLONG error is raised. If the generated name if too
138
- // long, it is truncated and a hash is appended. The limit has
139
- // been set to 100 to prevent `[name].[chunkhash].[ext]` from
140
- // generating a 256+ character string.
141
- if (name.length > 100) {
142
- name =
143
- name.slice(0, 100) + automaticNameDelimiter + hashFilename(name);
144
- }
145
- cacheEntry[cacheGroup] = name;
146
- return name;
147
- };
148
- return fn;
149
- }
150
- if (typeof name === "string") {
151
- const fn = () => {
152
- return name;
153
- };
154
- return fn;
155
- }
156
- if (typeof name === "function") return name;
157
- }
158
-
159
- static normalizeChunksFilter(chunks) {
160
- if (chunks === "initial") {
161
- return INITIAL_CHUNK_FILTER;
162
- }
163
- if (chunks === "async") {
164
- return ASYNC_CHUNK_FILTER;
165
- }
166
- if (chunks === "all") {
167
- return ALL_CHUNK_FILTER;
168
- }
169
- if (typeof chunks === "function") return chunks;
170
- }
171
-
172
- static normalizeCacheGroups({ cacheGroups, automaticNameDelimiter }) {
173
- if (typeof cacheGroups === "function") {
174
- // TODO webpack 5 remove this
175
- if (cacheGroups.length !== 1)
176
- return module => cacheGroups(module, module.getChunks());
177
- return cacheGroups;
178
- }
179
- if (cacheGroups && typeof cacheGroups === "object") {
180
- const fn = module => {
181
- let results;
182
- for (const key of Object.keys(cacheGroups)) {
183
- let option = cacheGroups[key];
184
- if (option === false) continue;
185
- if (option instanceof RegExp || typeof option === "string") {
186
- option = {
187
- test: option
188
- };
189
- }
190
- if (typeof option === "function") {
191
- let result = option(module);
192
- if (result) {
193
- if (results === undefined) results = [];
194
- for (const r of Array.isArray(result) ? result : [result]) {
195
- const result = Object.assign(
196
- {
197
- key
198
- },
199
- r
200
- );
201
- if (result.name) result.getName = () => result.name;
202
- if (result.chunks) {
203
- result.chunksFilter = SplitChunksPlugin.normalizeChunksFilter(
204
- result.chunks
205
- );
206
- }
207
- results.push(result);
208
- }
209
- }
210
- } else if (SplitChunksPlugin.checkTest(option.test, module)) {
211
- if (results === undefined) results = [];
212
- results.push({
213
- key: key,
214
- priority: option.priority,
215
- getName: SplitChunksPlugin.normalizeName({
216
- name: option.name,
217
- automaticNameDelimiter
218
- }),
219
- chunksFilter: SplitChunksPlugin.normalizeChunksFilter(
220
- option.chunks
221
- ),
222
- enforce: option.enforce,
223
- minSize: option.minSize,
224
- minChunks: option.minChunks,
225
- maxAsyncRequests: option.maxAsyncRequests,
226
- maxInitialRequests: option.maxInitialRequests,
227
- filename: option.filename,
228
- reuseExistingChunk: option.reuseExistingChunk
229
- });
230
- }
231
- }
232
- return results;
233
- };
234
- return fn;
235
- }
236
- const fn = () => {};
237
- return fn;
238
- }
239
-
240
- static checkTest(test, module) {
241
- if (test === undefined) return true;
242
- if (typeof test === "function") {
243
- if (test.length !== 1) return test(module, module.getChunks());
244
- return test(module);
245
- }
246
- if (typeof test === "boolean") return test;
247
- if (typeof test === "string") {
248
- if (module.nameForCondition && module.nameForCondition().startsWith(test))
249
- return true;
250
- for (const chunk of module.chunksIterable) {
251
- if (chunk.name && chunk.name.startsWith(test)) return true;
252
- }
253
- return false;
254
- }
255
- if (test instanceof RegExp) {
256
- if (module.nameForCondition && test.test(module.nameForCondition()))
257
- return true;
258
- for (const chunk of module.chunksIterable) {
259
- if (chunk.name && test.test(chunk.name)) return true;
260
- }
261
- return false;
262
- }
263
- return false;
264
- }
265
-
266
- apply(compiler) {
267
- compiler.hooks.thisCompilation.tap("SplitChunksPlugin", compilation => {
268
- let alreadyOptimized = false;
269
- compilation.hooks.unseal.tap("SplitChunksPlugin", () => {
270
- alreadyOptimized = false;
271
- });
272
- compilation.hooks.optimizeChunksAdvanced.tap(
273
- "SplitChunksPlugin",
274
- chunks => {
275
- if (alreadyOptimized) return;
276
- alreadyOptimized = true;
277
- // Give each selected chunk an index (to create strings from chunks)
278
- const indexMap = new Map();
279
- let index = 1;
280
- for (const chunk of chunks) {
281
- indexMap.set(chunk, index++);
282
- }
283
- const getKey = chunks => {
284
- return Array.from(chunks, c => indexMap.get(c))
285
- .sort()
286
- .join();
287
- };
288
- /** @type {Map<string, Set<Chunk>>} */
289
- const chunkSetsInGraph = new Map();
290
- for (const module of compilation.modules) {
291
- const chunksKey = getKey(module.chunksIterable);
292
- if (!chunkSetsInGraph.has(chunksKey)) {
293
- chunkSetsInGraph.set(chunksKey, new Set(module.chunksIterable));
294
- }
295
- }
296
-
297
- // group these set of chunks by count
298
- // to allow to check less sets via isSubset
299
- // (only smaller sets can be subset)
300
- /** @type {Map<number, Array<Set<Chunk>>>} */
301
- const chunkSetsByCount = new Map();
302
- for (const chunksSet of chunkSetsInGraph.values()) {
303
- const count = chunksSet.size;
304
- let array = chunkSetsByCount.get(count);
305
- if (array === undefined) {
306
- array = [];
307
- chunkSetsByCount.set(count, array);
308
- }
309
- array.push(chunksSet);
310
- }
311
-
312
- // Create a list of possible combinations
313
- const combinationsCache = new Map(); // Map<string, Set<Chunk>[]>
314
-
315
- const getCombinations = key => {
316
- const chunksSet = chunkSetsInGraph.get(key);
317
- var array = [chunksSet];
318
- if (chunksSet.size > 1) {
319
- for (const [count, setArray] of chunkSetsByCount) {
320
- // "equal" is not needed because they would have been merge in the first step
321
- if (count < chunksSet.size) {
322
- for (const set of setArray) {
323
- if (isSubset(chunksSet, set)) {
324
- array.push(set);
325
- }
326
- }
327
- }
328
- }
329
- }
330
- return array;
331
- };
332
-
333
- /**
334
- * @typedef {Object} SelectedChunksResult
335
- * @property {Chunk[]} chunks the list of chunks
336
- * @property {string} key a key of the list
337
- */
338
-
339
- /**
340
- * @typedef {function(Chunk): boolean} ChunkFilterFunction
341
- */
342
-
343
- /** @type {WeakMap<Set<Chunk>, WeakMap<ChunkFilterFunction, SelectedChunksResult>>} */
344
- const selectedChunksCacheByChunksSet = new WeakMap();
345
-
346
- /**
347
- * get list and key by applying the filter function to the list
348
- * It is cached for performance reasons
349
- * @param {Set<Chunk>} chunks list of chunks
350
- * @param {ChunkFilterFunction} chunkFilter filter function for chunks
351
- * @returns {SelectedChunksResult} list and key
352
- */
353
- const getSelectedChunks = (chunks, chunkFilter) => {
354
- let entry = selectedChunksCacheByChunksSet.get(chunks);
355
- if (entry === undefined) {
356
- entry = new WeakMap();
357
- selectedChunksCacheByChunksSet.set(chunks, entry);
358
- }
359
- /** @type {SelectedChunksResult} */
360
- let entry2 = entry.get(chunkFilter);
361
- if (entry2 === undefined) {
362
- /** @type {Chunk[]} */
363
- const selectedChunks = [];
364
- for (const chunk of chunks) {
365
- if (chunkFilter(chunk)) selectedChunks.push(chunk);
366
- }
367
- entry2 = {
368
- chunks: selectedChunks,
369
- key: getKey(selectedChunks)
370
- };
371
- entry.set(chunkFilter, entry2);
372
- }
373
- return entry2;
374
- };
375
-
376
- /**
377
- * @typedef {Object} ChunksInfoItem
378
- * @property {SortableSet} modules
379
- * @property {TODO} cacheGroup
380
- * @property {string} name
381
- * @property {number} size
382
- * @property {Map<Chunk, number>} chunks
383
- * @property {Set<Chunk>} reuseableChunks
384
- * @property {Set<string>} chunksKeys
385
- */
386
-
387
- // Map a list of chunks to a list of modules
388
- // For the key the chunk "index" is used, the value is a SortableSet of modules
389
- /** @type {Map<string, ChunksInfoItem>} */
390
- const chunksInfoMap = new Map();
391
-
392
- /**
393
- * @param {TODO} cacheGroup the current cache group
394
- * @param {Chunk[]} selectedChunks chunks selected for this module
395
- * @param {string} selectedChunksKey a key of selectedChunks
396
- * @param {Module} module the current module
397
- * @returns {void}
398
- */
399
- const addModuleToChunksInfoMap = (
400
- cacheGroup,
401
- selectedChunks,
402
- selectedChunksKey,
403
- module
404
- ) => {
405
- // Break if minimum number of chunks is not reached
406
- if (selectedChunks.length < cacheGroup.minChunks) return;
407
- // Determine name for split chunk
408
- const name = cacheGroup.getName(
409
- module,
410
- selectedChunks,
411
- cacheGroup.key
412
- );
413
- // Create key for maps
414
- // When it has a name we use the name as key
415
- // Elsewise we create the key from chunks and cache group key
416
- // This automatically merges equal names
417
- const key =
418
- (name && `name:${name}`) ||
419
- `chunks:${selectedChunksKey} key:${cacheGroup.key}`;
420
- // Add module to maps
421
- let info = chunksInfoMap.get(key);
422
- if (info === undefined) {
423
- chunksInfoMap.set(
424
- key,
425
- (info = {
426
- modules: new SortableSet(undefined, sortByIdentifier),
427
- cacheGroup,
428
- name,
429
- size: 0,
430
- chunks: new Map(),
431
- reuseableChunks: new Set(),
432
- chunksKeys: new Set()
433
- })
434
- );
435
- }
436
- info.modules.add(module);
437
- info.size += module.size();
438
- if (!info.chunksKeys.has(selectedChunksKey)) {
439
- info.chunksKeys.add(selectedChunksKey);
440
- for (const chunk of selectedChunks) {
441
- info.chunks.set(chunk, chunk.getNumberOfModules());
442
- }
443
- }
444
- };
445
-
446
- // Walk through all modules
447
- for (const module of compilation.modules) {
448
- // Get cache group
449
- let cacheGroups = this.options.getCacheGroups(module);
450
- if (!Array.isArray(cacheGroups) || cacheGroups.length === 0)
451
- continue;
452
-
453
- // Prepare some values
454
- const chunksKey = getKey(module.chunksIterable);
455
- let combs = combinationsCache.get(chunksKey);
456
- if (combs === undefined) {
457
- combs = getCombinations(chunksKey);
458
- combinationsCache.set(chunksKey, combs);
459
- }
460
-
461
- for (const cacheGroupSource of cacheGroups) {
462
- const cacheGroup = {
463
- key: cacheGroupSource.key,
464
- priority: cacheGroupSource.priority || 0,
465
- chunksFilter:
466
- cacheGroupSource.chunksFilter || this.options.chunksFilter,
467
- minSize:
468
- cacheGroupSource.minSize !== undefined
469
- ? cacheGroupSource.minSize
470
- : cacheGroupSource.enforce
471
- ? 0
472
- : this.options.minSize,
473
- minChunks:
474
- cacheGroupSource.minChunks !== undefined
475
- ? cacheGroupSource.minChunks
476
- : cacheGroupSource.enforce
477
- ? 1
478
- : this.options.minChunks,
479
- maxAsyncRequests:
480
- cacheGroupSource.maxAsyncRequests !== undefined
481
- ? cacheGroupSource.maxAsyncRequests
482
- : cacheGroupSource.enforce
483
- ? Infinity
484
- : this.options.maxAsyncRequests,
485
- maxInitialRequests:
486
- cacheGroupSource.maxInitialRequests !== undefined
487
- ? cacheGroupSource.maxInitialRequests
488
- : cacheGroupSource.enforce
489
- ? Infinity
490
- : this.options.maxInitialRequests,
491
- getName:
492
- cacheGroupSource.getName !== undefined
493
- ? cacheGroupSource.getName
494
- : this.options.getName,
495
- filename:
496
- cacheGroupSource.filename !== undefined
497
- ? cacheGroupSource.filename
498
- : this.options.filename,
499
- reuseExistingChunk: cacheGroupSource.reuseExistingChunk
500
- };
501
- // For all combination of chunk selection
502
- for (const chunkCombination of combs) {
503
- // Break if minimum number of chunks is not reached
504
- if (chunkCombination.size < cacheGroup.minChunks) continue;
505
- // Select chunks by configuration
506
- const {
507
- chunks: selectedChunks,
508
- key: selectedChunksKey
509
- } = getSelectedChunks(
510
- chunkCombination,
511
- cacheGroup.chunksFilter
512
- );
513
-
514
- addModuleToChunksInfoMap(
515
- cacheGroup,
516
- selectedChunks,
517
- selectedChunksKey,
518
- module
519
- );
520
- }
521
- }
522
- }
523
-
524
- while (chunksInfoMap.size > 0) {
525
- // Find best matching entry
526
- let bestEntryKey;
527
- let bestEntry;
528
- for (const pair of chunksInfoMap) {
529
- const key = pair[0];
530
- const info = pair[1];
531
- if (info.size >= info.cacheGroup.minSize) {
532
- if (bestEntry === undefined) {
533
- bestEntry = info;
534
- bestEntryKey = key;
535
- } else if (compareEntries(bestEntry, info) < 0) {
536
- bestEntry = info;
537
- bestEntryKey = key;
538
- }
539
- }
540
- }
541
-
542
- // No suitable item left
543
- if (bestEntry === undefined) break;
544
-
545
- const item = bestEntry;
546
- chunksInfoMap.delete(bestEntryKey);
547
-
548
- let chunkName = item.name;
549
- // Variable for the new chunk (lazy created)
550
- let newChunk;
551
- // When no chunk name, check if we can reuse a chunk instead of creating a new one
552
- let isReused = false;
553
- if (item.cacheGroup.reuseExistingChunk) {
554
- for (const pair of item.chunks) {
555
- if (pair[1] === item.modules.size) {
556
- const chunk = pair[0];
557
- if (chunk.hasEntryModule()) continue;
558
- if (!newChunk || !newChunk.name) newChunk = chunk;
559
- else if (
560
- chunk.name &&
561
- chunk.name.length < newChunk.name.length
562
- )
563
- newChunk = chunk;
564
- else if (
565
- chunk.name &&
566
- chunk.name.length === newChunk.name.length &&
567
- chunk.name < newChunk.name
568
- )
569
- newChunk = chunk;
570
- chunkName = undefined;
571
- isReused = true;
572
- }
573
- }
574
- }
575
- // Check if maxRequests condition can be fullfilled
576
-
577
- const usedChunks = Array.from(item.chunks.keys()).filter(chunk => {
578
- // skip if we address ourself
579
- return (
580
- (!chunkName || chunk.name !== chunkName) && chunk !== newChunk
581
- );
582
- });
583
-
584
- // Skip when no chunk selected
585
- if (usedChunks.length === 0) continue;
586
-
587
- const chunkInLimit = usedChunks.filter(chunk => {
588
- // respect max requests when not enforced
589
- const maxRequests = chunk.isOnlyInitial()
590
- ? item.cacheGroup.maxInitialRequests
591
- : chunk.canBeInitial()
592
- ? Math.min(
593
- item.cacheGroup.maxInitialRequests,
594
- item.cacheGroup.maxAsyncRequests
595
- )
596
- : item.cacheGroup.maxAsyncRequests;
597
- return !isFinite(maxRequests) || getRequests(chunk) < maxRequests;
598
- });
599
-
600
- if (chunkInLimit.length < usedChunks.length) {
601
- for (const module of item.modules) {
602
- addModuleToChunksInfoMap(
603
- item.cacheGroup,
604
- chunkInLimit,
605
- getKey(chunkInLimit),
606
- module
607
- );
608
- }
609
- continue;
610
- }
611
-
612
- // Create the new chunk if not reusing one
613
- if (!isReused) {
614
- newChunk = compilation.addChunk(chunkName);
615
- }
616
- // Walk through all chunks
617
- for (const chunk of usedChunks) {
618
- // Add graph connections for splitted chunk
619
- chunk.split(newChunk);
620
- }
621
-
622
- // Add a note to the chunk
623
- newChunk.chunkReason = isReused
624
- ? "reused as split chunk"
625
- : "split chunk";
626
- if (item.cacheGroup.key) {
627
- newChunk.chunkReason += ` (cache group: ${item.cacheGroup.key})`;
628
- }
629
- if (chunkName) {
630
- newChunk.chunkReason += ` (name: ${chunkName})`;
631
- // If the chosen name is already an entry point we remove the entry point
632
- const entrypoint = compilation.entrypoints.get(chunkName);
633
- if (entrypoint) {
634
- compilation.entrypoints.delete(chunkName);
635
- entrypoint.remove();
636
- newChunk.entryModule = undefined;
637
- }
638
- }
639
- if (item.cacheGroup.filename) {
640
- if (!newChunk.isOnlyInitial()) {
641
- throw new Error(
642
- "SplitChunksPlugin: You are trying to set a filename for a chunk which is (also) loaded on demand. " +
643
- "The runtime can only handle loading of chunks which match the chunkFilename schema. " +
644
- "Using a custom filename would fail at runtime. " +
645
- `(cache group: ${item.cacheGroup.key})`
646
- );
647
- }
648
- newChunk.filenameTemplate = item.cacheGroup.filename;
649
- }
650
- if (!isReused) {
651
- // Add all modules to the new chunk
652
- for (const module of item.modules) {
653
- if (typeof module.chunkCondition === "function") {
654
- if (!module.chunkCondition(newChunk)) continue;
655
- }
656
- // Add module to new chunk
657
- GraphHelpers.connectChunkAndModule(newChunk, module);
658
- // Remove module from used chunks
659
- for (const chunk of usedChunks) {
660
- chunk.removeModule(module);
661
- module.rewriteChunkInReasons(chunk, [newChunk]);
662
- }
663
- }
664
- } else {
665
- // Remove all modules from used chunks
666
- for (const module of item.modules) {
667
- for (const chunk of usedChunks) {
668
- chunk.removeModule(module);
669
- module.rewriteChunkInReasons(chunk, [newChunk]);
670
- }
671
- }
672
- }
673
- // remove all modules from other entries and update size
674
- for (const [key, info] of chunksInfoMap) {
675
- if (isOverlap(info.chunks, item.chunks)) {
676
- const oldSize = info.modules.size;
677
- for (const module of item.modules) {
678
- info.modules.delete(module);
679
- }
680
- if (info.modules.size === 0) {
681
- chunksInfoMap.delete(key);
682
- continue;
683
- }
684
- if (info.modules.size !== oldSize) {
685
- info.size = getModulesSize(info.modules);
686
- if (info.size < info.cacheGroup.minSize)
687
- chunksInfoMap.delete(key);
688
- }
689
- }
690
- }
691
- }
692
- }
693
- );
694
- });
695
- }
696
- };
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+ "use strict";
6
+
7
+ const crypto = require("crypto");
8
+ const SortableSet = require("../util/SortableSet");
9
+ const GraphHelpers = require("../GraphHelpers");
10
+ const { isSubset } = require("../util/SetHelpers");
11
+
12
+ /** @typedef {import("../Chunk")} Chunk */
13
+ /** @typedef {import("../Module")} Module */
14
+
15
+ const hashFilename = name => {
16
+ return crypto
17
+ .createHash("md4")
18
+ .update(name)
19
+ .digest("hex")
20
+ .slice(0, 8);
21
+ };
22
+
23
+ const sortByIdentifier = (a, b) => {
24
+ if (a.identifier() > b.identifier()) return 1;
25
+ if (a.identifier() < b.identifier()) return -1;
26
+ return 0;
27
+ };
28
+
29
+ const getRequests = chunk => {
30
+ let requests = 0;
31
+ for (const chunkGroup of chunk.groupsIterable) {
32
+ requests = Math.max(requests, chunkGroup.chunks.length);
33
+ }
34
+ return requests;
35
+ };
36
+
37
+ const getModulesSize = modules => {
38
+ let sum = 0;
39
+ for (const m of modules) {
40
+ sum += m.size();
41
+ }
42
+ return sum;
43
+ };
44
+
45
+ const isOverlap = (a, b) => {
46
+ for (const item of a.keys()) {
47
+ if (b.has(item)) return true;
48
+ }
49
+ return false;
50
+ };
51
+
52
+ const compareEntries = (a, b) => {
53
+ // 1. by priority
54
+ const diffPriority = a.cacheGroup.priority - b.cacheGroup.priority;
55
+ if (diffPriority) return diffPriority;
56
+ // 2. by number of chunks
57
+ const diffCount = a.chunks.size - b.chunks.size;
58
+ if (diffCount) return diffCount;
59
+ // 3. by size reduction
60
+ const aSizeReduce = a.size * (a.chunks.size - 1);
61
+ const bSizeReduce = b.size * (b.chunks.size - 1);
62
+ const diffSizeReduce = aSizeReduce - bSizeReduce;
63
+ if (diffSizeReduce) return diffSizeReduce;
64
+ // 4. by number of modules (to be able to compare by identifier)
65
+ const modulesA = a.modules;
66
+ const modulesB = b.modules;
67
+ const diff = modulesA.size - modulesB.size;
68
+ if (diff) return diff;
69
+ // 5. by module identifiers
70
+ modulesA.sort();
71
+ modulesB.sort();
72
+ const aI = modulesA[Symbol.iterator]();
73
+ const bI = modulesB[Symbol.iterator]();
74
+ // eslint-disable-next-line no-constant-condition
75
+ while (true) {
76
+ const aItem = aI.next();
77
+ const bItem = bI.next();
78
+ if (aItem.done) return 0;
79
+ const aModuleIdentifier = aItem.value.identifier();
80
+ const bModuleIdentifier = bItem.value.identifier();
81
+ if (aModuleIdentifier > bModuleIdentifier) return -1;
82
+ if (aModuleIdentifier < bModuleIdentifier) return 1;
83
+ }
84
+ };
85
+
86
+ const INITIAL_CHUNK_FILTER = chunk => chunk.canBeInitial();
87
+ const ASYNC_CHUNK_FILTER = chunk => !chunk.canBeInitial();
88
+ const ALL_CHUNK_FILTER = chunk => true;
89
+
90
+ module.exports = class SplitChunksPlugin {
91
+ constructor(options) {
92
+ this.options = SplitChunksPlugin.normalizeOptions(options);
93
+ }
94
+
95
+ static normalizeOptions(options = {}) {
96
+ return {
97
+ chunksFilter: SplitChunksPlugin.normalizeChunksFilter(
98
+ options.chunks || "all"
99
+ ),
100
+ minSize: options.minSize || 0,
101
+ minChunks: options.minChunks || 1,
102
+ maxAsyncRequests: options.maxAsyncRequests || 1,
103
+ maxInitialRequests: options.maxInitialRequests || 1,
104
+ getName:
105
+ SplitChunksPlugin.normalizeName({
106
+ name: options.name,
107
+ automaticNameDelimiter: options.automaticNameDelimiter
108
+ }) || (() => {}),
109
+ filename: options.filename || undefined,
110
+ getCacheGroups: SplitChunksPlugin.normalizeCacheGroups({
111
+ cacheGroups: options.cacheGroups,
112
+ automaticNameDelimiter: options.automaticNameDelimiter
113
+ })
114
+ };
115
+ }
116
+
117
+ static normalizeName({ name, automaticNameDelimiter }) {
118
+ if (name === true) {
119
+ const cache = new Map();
120
+ const fn = (module, chunks, cacheGroup) => {
121
+ let cacheEntry = cache.get(chunks);
122
+ if (cacheEntry === undefined) {
123
+ cacheEntry = {};
124
+ cache.set(chunks, cacheEntry);
125
+ } else if (cacheGroup in cacheEntry) {
126
+ return cacheEntry[cacheGroup];
127
+ }
128
+ const names = chunks.map(c => c.name);
129
+ if (!names.every(Boolean)) {
130
+ cacheEntry[cacheGroup] = undefined;
131
+ return;
132
+ }
133
+ names.sort();
134
+ let name =
135
+ (cacheGroup && cacheGroup !== "default"
136
+ ? cacheGroup + automaticNameDelimiter
137
+ : "") + names.join(automaticNameDelimiter);
138
+ // Filenames and paths can't be too long otherwise an
139
+ // ENAMETOOLONG error is raised. If the generated name if too
140
+ // long, it is truncated and a hash is appended. The limit has
141
+ // been set to 100 to prevent `[name].[chunkhash].[ext]` from
142
+ // generating a 256+ character string.
143
+ if (name.length > 100) {
144
+ name =
145
+ name.slice(0, 100) + automaticNameDelimiter + hashFilename(name);
146
+ }
147
+ cacheEntry[cacheGroup] = name;
148
+ return name;
149
+ };
150
+ return fn;
151
+ }
152
+ if (typeof name === "string") {
153
+ const fn = () => {
154
+ return name;
155
+ };
156
+ return fn;
157
+ }
158
+ if (typeof name === "function") return name;
159
+ }
160
+
161
+ static normalizeChunksFilter(chunks) {
162
+ if (chunks === "initial") {
163
+ return INITIAL_CHUNK_FILTER;
164
+ }
165
+ if (chunks === "async") {
166
+ return ASYNC_CHUNK_FILTER;
167
+ }
168
+ if (chunks === "all") {
169
+ return ALL_CHUNK_FILTER;
170
+ }
171
+ if (typeof chunks === "function") return chunks;
172
+ }
173
+
174
+ static normalizeCacheGroups({ cacheGroups, automaticNameDelimiter }) {
175
+ if (typeof cacheGroups === "function") {
176
+ // TODO webpack 5 remove this
177
+ if (cacheGroups.length !== 1) {
178
+ return module => cacheGroups(module, module.getChunks());
179
+ }
180
+ return cacheGroups;
181
+ }
182
+ if (cacheGroups && typeof cacheGroups === "object") {
183
+ const fn = module => {
184
+ let results;
185
+ for (const key of Object.keys(cacheGroups)) {
186
+ let option = cacheGroups[key];
187
+ if (option === false) continue;
188
+ if (option instanceof RegExp || typeof option === "string") {
189
+ option = {
190
+ test: option
191
+ };
192
+ }
193
+ if (typeof option === "function") {
194
+ let result = option(module);
195
+ if (result) {
196
+ if (results === undefined) results = [];
197
+ for (const r of Array.isArray(result) ? result : [result]) {
198
+ const result = Object.assign({ key }, r);
199
+ if (result.name) result.getName = () => result.name;
200
+ if (result.chunks) {
201
+ result.chunksFilter = SplitChunksPlugin.normalizeChunksFilter(
202
+ result.chunks
203
+ );
204
+ }
205
+ results.push(result);
206
+ }
207
+ }
208
+ } else if (SplitChunksPlugin.checkTest(option.test, module)) {
209
+ if (results === undefined) results = [];
210
+ results.push({
211
+ key: key,
212
+ priority: option.priority,
213
+ getName: SplitChunksPlugin.normalizeName({
214
+ name: option.name,
215
+ automaticNameDelimiter
216
+ }),
217
+ chunksFilter: SplitChunksPlugin.normalizeChunksFilter(
218
+ option.chunks
219
+ ),
220
+ enforce: option.enforce,
221
+ minSize: option.minSize,
222
+ minChunks: option.minChunks,
223
+ maxAsyncRequests: option.maxAsyncRequests,
224
+ maxInitialRequests: option.maxInitialRequests,
225
+ filename: option.filename,
226
+ reuseExistingChunk: option.reuseExistingChunk
227
+ });
228
+ }
229
+ }
230
+ return results;
231
+ };
232
+ return fn;
233
+ }
234
+ const fn = () => {};
235
+ return fn;
236
+ }
237
+
238
+ static checkTest(test, module) {
239
+ if (test === undefined) return true;
240
+ if (typeof test === "function") {
241
+ if (test.length !== 1) {
242
+ return test(module, module.getChunks());
243
+ }
244
+ return test(module);
245
+ }
246
+ if (typeof test === "boolean") return test;
247
+ if (typeof test === "string") {
248
+ if (
249
+ module.nameForCondition &&
250
+ module.nameForCondition().startsWith(test)
251
+ ) {
252
+ return true;
253
+ }
254
+ for (const chunk of module.chunksIterable) {
255
+ if (chunk.name && chunk.name.startsWith(test)) {
256
+ return true;
257
+ }
258
+ }
259
+ return false;
260
+ }
261
+ if (test instanceof RegExp) {
262
+ if (module.nameForCondition && test.test(module.nameForCondition())) {
263
+ return true;
264
+ }
265
+ for (const chunk of module.chunksIterable) {
266
+ if (chunk.name && test.test(chunk.name)) {
267
+ return true;
268
+ }
269
+ }
270
+ return false;
271
+ }
272
+ return false;
273
+ }
274
+
275
+ apply(compiler) {
276
+ compiler.hooks.thisCompilation.tap("SplitChunksPlugin", compilation => {
277
+ let alreadyOptimized = false;
278
+ compilation.hooks.unseal.tap("SplitChunksPlugin", () => {
279
+ alreadyOptimized = false;
280
+ });
281
+ compilation.hooks.optimizeChunksAdvanced.tap(
282
+ "SplitChunksPlugin",
283
+ chunks => {
284
+ if (alreadyOptimized) return;
285
+ alreadyOptimized = true;
286
+ // Give each selected chunk an index (to create strings from chunks)
287
+ const indexMap = new Map();
288
+ let index = 1;
289
+ for (const chunk of chunks) {
290
+ indexMap.set(chunk, index++);
291
+ }
292
+ const getKey = chunks => {
293
+ return Array.from(chunks, c => indexMap.get(c))
294
+ .sort()
295
+ .join();
296
+ };
297
+ /** @type {Map<string, Set<Chunk>>} */
298
+ const chunkSetsInGraph = new Map();
299
+ for (const module of compilation.modules) {
300
+ const chunksKey = getKey(module.chunksIterable);
301
+ if (!chunkSetsInGraph.has(chunksKey)) {
302
+ chunkSetsInGraph.set(chunksKey, new Set(module.chunksIterable));
303
+ }
304
+ }
305
+
306
+ // group these set of chunks by count
307
+ // to allow to check less sets via isSubset
308
+ // (only smaller sets can be subset)
309
+ /** @type {Map<number, Array<Set<Chunk>>>} */
310
+ const chunkSetsByCount = new Map();
311
+ for (const chunksSet of chunkSetsInGraph.values()) {
312
+ const count = chunksSet.size;
313
+ let array = chunkSetsByCount.get(count);
314
+ if (array === undefined) {
315
+ array = [];
316
+ chunkSetsByCount.set(count, array);
317
+ }
318
+ array.push(chunksSet);
319
+ }
320
+
321
+ // Create a list of possible combinations
322
+ const combinationsCache = new Map(); // Map<string, Set<Chunk>[]>
323
+
324
+ const getCombinations = key => {
325
+ const chunksSet = chunkSetsInGraph.get(key);
326
+ var array = [chunksSet];
327
+ if (chunksSet.size > 1) {
328
+ for (const [count, setArray] of chunkSetsByCount) {
329
+ // "equal" is not needed because they would have been merge in the first step
330
+ if (count < chunksSet.size) {
331
+ for (const set of setArray) {
332
+ if (isSubset(chunksSet, set)) {
333
+ array.push(set);
334
+ }
335
+ }
336
+ }
337
+ }
338
+ }
339
+ return array;
340
+ };
341
+
342
+ /**
343
+ * @typedef {Object} SelectedChunksResult
344
+ * @property {Chunk[]} chunks the list of chunks
345
+ * @property {string} key a key of the list
346
+ */
347
+
348
+ /**
349
+ * @typedef {function(Chunk): boolean} ChunkFilterFunction
350
+ */
351
+
352
+ /** @type {WeakMap<Set<Chunk>, WeakMap<ChunkFilterFunction, SelectedChunksResult>>} */
353
+ const selectedChunksCacheByChunksSet = new WeakMap();
354
+
355
+ /**
356
+ * get list and key by applying the filter function to the list
357
+ * It is cached for performance reasons
358
+ * @param {Set<Chunk>} chunks list of chunks
359
+ * @param {ChunkFilterFunction} chunkFilter filter function for chunks
360
+ * @returns {SelectedChunksResult} list and key
361
+ */
362
+ const getSelectedChunks = (chunks, chunkFilter) => {
363
+ let entry = selectedChunksCacheByChunksSet.get(chunks);
364
+ if (entry === undefined) {
365
+ entry = new WeakMap();
366
+ selectedChunksCacheByChunksSet.set(chunks, entry);
367
+ }
368
+ /** @type {SelectedChunksResult} */
369
+ let entry2 = entry.get(chunkFilter);
370
+ if (entry2 === undefined) {
371
+ /** @type {Chunk[]} */
372
+ const selectedChunks = [];
373
+ for (const chunk of chunks) {
374
+ if (chunkFilter(chunk)) selectedChunks.push(chunk);
375
+ }
376
+ entry2 = {
377
+ chunks: selectedChunks,
378
+ key: getKey(selectedChunks)
379
+ };
380
+ entry.set(chunkFilter, entry2);
381
+ }
382
+ return entry2;
383
+ };
384
+
385
+ /**
386
+ * @typedef {Object} ChunksInfoItem
387
+ * @property {SortableSet} modules
388
+ * @property {TODO} cacheGroup
389
+ * @property {string} name
390
+ * @property {number} size
391
+ * @property {Map<Chunk, number>} chunks
392
+ * @property {Set<Chunk>} reuseableChunks
393
+ * @property {Set<string>} chunksKeys
394
+ */
395
+
396
+ // Map a list of chunks to a list of modules
397
+ // For the key the chunk "index" is used, the value is a SortableSet of modules
398
+ /** @type {Map<string, ChunksInfoItem>} */
399
+ const chunksInfoMap = new Map();
400
+
401
+ /**
402
+ * @param {TODO} cacheGroup the current cache group
403
+ * @param {Chunk[]} selectedChunks chunks selected for this module
404
+ * @param {string} selectedChunksKey a key of selectedChunks
405
+ * @param {Module} module the current module
406
+ * @returns {void}
407
+ */
408
+ const addModuleToChunksInfoMap = (
409
+ cacheGroup,
410
+ selectedChunks,
411
+ selectedChunksKey,
412
+ module
413
+ ) => {
414
+ // Break if minimum number of chunks is not reached
415
+ if (selectedChunks.length < cacheGroup.minChunks) return;
416
+ // Determine name for split chunk
417
+ const name = cacheGroup.getName(
418
+ module,
419
+ selectedChunks,
420
+ cacheGroup.key
421
+ );
422
+ // Create key for maps
423
+ // When it has a name we use the name as key
424
+ // Elsewise we create the key from chunks and cache group key
425
+ // This automatically merges equal names
426
+ const key =
427
+ (name && `name:${name}`) ||
428
+ `chunks:${selectedChunksKey} key:${cacheGroup.key}`;
429
+ // Add module to maps
430
+ let info = chunksInfoMap.get(key);
431
+ if (info === undefined) {
432
+ chunksInfoMap.set(
433
+ key,
434
+ (info = {
435
+ modules: new SortableSet(undefined, sortByIdentifier),
436
+ cacheGroup,
437
+ name,
438
+ size: 0,
439
+ chunks: new Map(),
440
+ reuseableChunks: new Set(),
441
+ chunksKeys: new Set()
442
+ })
443
+ );
444
+ }
445
+ info.modules.add(module);
446
+ info.size += module.size();
447
+ if (!info.chunksKeys.has(selectedChunksKey)) {
448
+ info.chunksKeys.add(selectedChunksKey);
449
+ for (const chunk of selectedChunks) {
450
+ info.chunks.set(chunk, chunk.getNumberOfModules());
451
+ }
452
+ }
453
+ };
454
+
455
+ // Walk through all modules
456
+ for (const module of compilation.modules) {
457
+ // Get cache group
458
+ let cacheGroups = this.options.getCacheGroups(module);
459
+ if (!Array.isArray(cacheGroups) || cacheGroups.length === 0) {
460
+ continue;
461
+ }
462
+
463
+ // Prepare some values
464
+ const chunksKey = getKey(module.chunksIterable);
465
+ let combs = combinationsCache.get(chunksKey);
466
+ if (combs === undefined) {
467
+ combs = getCombinations(chunksKey);
468
+ combinationsCache.set(chunksKey, combs);
469
+ }
470
+
471
+ for (const cacheGroupSource of cacheGroups) {
472
+ const cacheGroup = {
473
+ key: cacheGroupSource.key,
474
+ priority: cacheGroupSource.priority || 0,
475
+ chunksFilter:
476
+ cacheGroupSource.chunksFilter || this.options.chunksFilter,
477
+ minSize:
478
+ cacheGroupSource.minSize !== undefined
479
+ ? cacheGroupSource.minSize
480
+ : cacheGroupSource.enforce
481
+ ? 0
482
+ : this.options.minSize,
483
+ minChunks:
484
+ cacheGroupSource.minChunks !== undefined
485
+ ? cacheGroupSource.minChunks
486
+ : cacheGroupSource.enforce
487
+ ? 1
488
+ : this.options.minChunks,
489
+ maxAsyncRequests:
490
+ cacheGroupSource.maxAsyncRequests !== undefined
491
+ ? cacheGroupSource.maxAsyncRequests
492
+ : cacheGroupSource.enforce
493
+ ? Infinity
494
+ : this.options.maxAsyncRequests,
495
+ maxInitialRequests:
496
+ cacheGroupSource.maxInitialRequests !== undefined
497
+ ? cacheGroupSource.maxInitialRequests
498
+ : cacheGroupSource.enforce
499
+ ? Infinity
500
+ : this.options.maxInitialRequests,
501
+ getName:
502
+ cacheGroupSource.getName !== undefined
503
+ ? cacheGroupSource.getName
504
+ : this.options.getName,
505
+ filename:
506
+ cacheGroupSource.filename !== undefined
507
+ ? cacheGroupSource.filename
508
+ : this.options.filename,
509
+ reuseExistingChunk: cacheGroupSource.reuseExistingChunk
510
+ };
511
+ // For all combination of chunk selection
512
+ for (const chunkCombination of combs) {
513
+ // Break if minimum number of chunks is not reached
514
+ if (chunkCombination.size < cacheGroup.minChunks) continue;
515
+ // Select chunks by configuration
516
+ const {
517
+ chunks: selectedChunks,
518
+ key: selectedChunksKey
519
+ } = getSelectedChunks(
520
+ chunkCombination,
521
+ cacheGroup.chunksFilter
522
+ );
523
+
524
+ addModuleToChunksInfoMap(
525
+ cacheGroup,
526
+ selectedChunks,
527
+ selectedChunksKey,
528
+ module
529
+ );
530
+ }
531
+ }
532
+ }
533
+
534
+ while (chunksInfoMap.size > 0) {
535
+ // Find best matching entry
536
+ let bestEntryKey;
537
+ let bestEntry;
538
+ for (const pair of chunksInfoMap) {
539
+ const key = pair[0];
540
+ const info = pair[1];
541
+ if (info.size >= info.cacheGroup.minSize) {
542
+ if (bestEntry === undefined) {
543
+ bestEntry = info;
544
+ bestEntryKey = key;
545
+ } else if (compareEntries(bestEntry, info) < 0) {
546
+ bestEntry = info;
547
+ bestEntryKey = key;
548
+ }
549
+ }
550
+ }
551
+
552
+ // No suitable item left
553
+ if (bestEntry === undefined) break;
554
+
555
+ const item = bestEntry;
556
+ chunksInfoMap.delete(bestEntryKey);
557
+
558
+ let chunkName = item.name;
559
+ // Variable for the new chunk (lazy created)
560
+ let newChunk;
561
+ // When no chunk name, check if we can reuse a chunk instead of creating a new one
562
+ let isReused = false;
563
+ if (item.cacheGroup.reuseExistingChunk) {
564
+ for (const pair of item.chunks) {
565
+ if (pair[1] === item.modules.size) {
566
+ const chunk = pair[0];
567
+ if (chunk.hasEntryModule()) continue;
568
+ if (!newChunk || !newChunk.name) {
569
+ newChunk = chunk;
570
+ } else if (
571
+ chunk.name &&
572
+ chunk.name.length < newChunk.name.length
573
+ ) {
574
+ newChunk = chunk;
575
+ } else if (
576
+ chunk.name &&
577
+ chunk.name.length === newChunk.name.length &&
578
+ chunk.name < newChunk.name
579
+ ) {
580
+ newChunk = chunk;
581
+ }
582
+ chunkName = undefined;
583
+ isReused = true;
584
+ }
585
+ }
586
+ }
587
+ // Check if maxRequests condition can be fullfilled
588
+
589
+ const usedChunks = Array.from(item.chunks.keys()).filter(chunk => {
590
+ // skip if we address ourself
591
+ return (
592
+ (!chunkName || chunk.name !== chunkName) && chunk !== newChunk
593
+ );
594
+ });
595
+
596
+ // Skip when no chunk selected
597
+ if (usedChunks.length === 0) continue;
598
+
599
+ const chunkInLimit = usedChunks.filter(chunk => {
600
+ // respect max requests when not enforced
601
+ const maxRequests = chunk.isOnlyInitial()
602
+ ? item.cacheGroup.maxInitialRequests
603
+ : chunk.canBeInitial()
604
+ ? Math.min(
605
+ item.cacheGroup.maxInitialRequests,
606
+ item.cacheGroup.maxAsyncRequests
607
+ )
608
+ : item.cacheGroup.maxAsyncRequests;
609
+ return !isFinite(maxRequests) || getRequests(chunk) < maxRequests;
610
+ });
611
+
612
+ if (chunkInLimit.length < usedChunks.length) {
613
+ for (const module of item.modules) {
614
+ addModuleToChunksInfoMap(
615
+ item.cacheGroup,
616
+ chunkInLimit,
617
+ getKey(chunkInLimit),
618
+ module
619
+ );
620
+ }
621
+ continue;
622
+ }
623
+
624
+ // Create the new chunk if not reusing one
625
+ if (!isReused) {
626
+ newChunk = compilation.addChunk(chunkName);
627
+ }
628
+ // Walk through all chunks
629
+ for (const chunk of usedChunks) {
630
+ // Add graph connections for splitted chunk
631
+ chunk.split(newChunk);
632
+ }
633
+
634
+ // Add a note to the chunk
635
+ newChunk.chunkReason = isReused
636
+ ? "reused as split chunk"
637
+ : "split chunk";
638
+ if (item.cacheGroup.key) {
639
+ newChunk.chunkReason += ` (cache group: ${item.cacheGroup.key})`;
640
+ }
641
+ if (chunkName) {
642
+ newChunk.chunkReason += ` (name: ${chunkName})`;
643
+ // If the chosen name is already an entry point we remove the entry point
644
+ const entrypoint = compilation.entrypoints.get(chunkName);
645
+ if (entrypoint) {
646
+ compilation.entrypoints.delete(chunkName);
647
+ entrypoint.remove();
648
+ newChunk.entryModule = undefined;
649
+ }
650
+ }
651
+ if (item.cacheGroup.filename) {
652
+ if (!newChunk.isOnlyInitial()) {
653
+ throw new Error(
654
+ "SplitChunksPlugin: You are trying to set a filename for a chunk which is (also) loaded on demand. " +
655
+ "The runtime can only handle loading of chunks which match the chunkFilename schema. " +
656
+ "Using a custom filename would fail at runtime. " +
657
+ `(cache group: ${item.cacheGroup.key})`
658
+ );
659
+ }
660
+ newChunk.filenameTemplate = item.cacheGroup.filename;
661
+ }
662
+ if (!isReused) {
663
+ // Add all modules to the new chunk
664
+ for (const module of item.modules) {
665
+ if (typeof module.chunkCondition === "function") {
666
+ if (!module.chunkCondition(newChunk)) continue;
667
+ }
668
+ // Add module to new chunk
669
+ GraphHelpers.connectChunkAndModule(newChunk, module);
670
+ // Remove module from used chunks
671
+ for (const chunk of usedChunks) {
672
+ chunk.removeModule(module);
673
+ module.rewriteChunkInReasons(chunk, [newChunk]);
674
+ }
675
+ }
676
+ } else {
677
+ // Remove all modules from used chunks
678
+ for (const module of item.modules) {
679
+ for (const chunk of usedChunks) {
680
+ chunk.removeModule(module);
681
+ module.rewriteChunkInReasons(chunk, [newChunk]);
682
+ }
683
+ }
684
+ }
685
+ // remove all modules from other entries and update size
686
+ for (const [key, info] of chunksInfoMap) {
687
+ if (isOverlap(info.chunks, item.chunks)) {
688
+ const oldSize = info.modules.size;
689
+ for (const module of item.modules) {
690
+ info.modules.delete(module);
691
+ }
692
+ if (info.modules.size === 0) {
693
+ chunksInfoMap.delete(key);
694
+ continue;
695
+ }
696
+ if (info.modules.size !== oldSize) {
697
+ info.size = getModulesSize(info.modules);
698
+ if (info.size < info.cacheGroup.minSize) {
699
+ chunksInfoMap.delete(key);
700
+ }
701
+ }
702
+ }
703
+ }
704
+ }
705
+ }
706
+ );
707
+ });
708
+ }
709
+ };