forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitChunksPlugin.js
More file actions
491 lines (476 loc) · 15.6 KB
/
SplitChunksPlugin.js
File metadata and controls
491 lines (476 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const crypto = require("crypto");
const SortableSet = require("../util/SortableSet");
const GraphHelpers = require("../GraphHelpers");
const isSubset = require("../util/SetHelpers").isSubset;
const hashFilename = name => {
return crypto
.createHash("md4")
.update(name)
.digest("hex")
.slice(0, 8);
};
const sortByIdentifier = (a, b) => {
if (a.identifier() > b.identifier()) return 1;
if (a.identifier() < b.identifier()) return -1;
return 0;
};
const getRequests = chunk => {
let requests = 0;
for (const chunkGroup of chunk.groupsIterable) {
requests = Math.max(requests, chunkGroup.chunks.length);
}
return requests;
};
const getModulesSize = modules => {
let sum = 0;
for (const m of modules) sum += m.size();
return sum;
};
const isOverlap = (a, b) => {
for (const item of a.keys()) {
if (b.has(item)) return true;
}
return false;
};
const compareEntries = (a, b) => {
// 1. by priority
const diffPriority = a.cacheGroup.priority - b.cacheGroup.priority;
if (diffPriority) return diffPriority;
// 2. by number of chunks
const diffCount = a.chunks.size - b.chunks.size;
if (diffCount) return diffCount;
// 3. by size reduction
const aSizeReduce = a.size * (a.chunks.size - 1);
const bSizeReduce = b.size * (b.chunks.size - 1);
const diffSizeReduce = aSizeReduce - bSizeReduce;
if (diffSizeReduce) return diffSizeReduce;
// 4. by number of modules (to be able to compare by identifier)
const modulesA = a.modules;
const modulesB = b.modules;
const diff = modulesA.size - modulesB.size;
if (diff) return diff;
// 5. by module identifiers
modulesA.sort();
modulesB.sort();
const aI = modulesA[Symbol.iterator]();
const bI = modulesB[Symbol.iterator]();
// eslint-disable-next-line no-constant-condition
while (true) {
const aItem = aI.next();
const bItem = bI.next();
if (aItem.done) return 0;
const aModuleIdentifier = aItem.value.identifier();
const bModuleIdentifier = bItem.value.identifier();
if (aModuleIdentifier > bModuleIdentifier) return -1;
if (aModuleIdentifier < bModuleIdentifier) return 1;
}
};
module.exports = class SplitChunksPlugin {
constructor(options) {
this.options = SplitChunksPlugin.normalizeOptions(options);
}
static normalizeOptions(options = {}) {
return {
chunks: options.chunks || "all",
minSize: options.minSize || 0,
minChunks: options.minChunks || 1,
maxAsyncRequests: options.maxAsyncRequests || 1,
maxInitialRequests: options.maxInitialRequests || 1,
getName: SplitChunksPlugin.normalizeName(options.name) || (() => {}),
filename: options.filename || undefined,
getCacheGroups: SplitChunksPlugin.normalizeCacheGroups(
options.cacheGroups
)
};
}
static normalizeName(option) {
if (option === true) {
const fn = (module, chunks, cacheGroup) => {
const names = chunks.map(c => c.name);
if (!names.every(Boolean)) return;
names.sort();
let name =
(cacheGroup && cacheGroup !== "default" ? cacheGroup + "~" : "") +
names.join("~");
// 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
// been set to 100 to prevent `[name].[chunkhash].[ext]` from
// generating a 256+ character string.
if (name.length > 100) {
name = name.slice(0, 100) + "~" + hashFilename(name);
}
return name;
};
return fn;
}
if (typeof option === "string") {
const fn = () => {
return option;
};
return fn;
}
if (typeof option === "function") return option;
}
static normalizeCacheGroups(cacheGroups) {
if (typeof cacheGroups === "function") {
return cacheGroups;
}
if (cacheGroups && typeof cacheGroups === "object") {
const fn = (module, chunks) => {
let results;
for (const key of Object.keys(cacheGroups)) {
let option = cacheGroups[key];
if (option === false) continue;
if (option instanceof RegExp || typeof option === "string") {
option = {
test: option
};
}
if (typeof option === "function") {
let result = option(module);
if (result) {
if (results === undefined) results = [];
for (const r of Array.isArray(result) ? result : [result]) {
const result = Object.assign(
{
key
},
r
);
if (result.name) result.getName = () => result.name;
results.push(result);
}
}
} else if (SplitChunksPlugin.checkTest(option.test, module, chunks)) {
if (results === undefined) results = [];
results.push({
key: key,
priority: option.priority,
getName: SplitChunksPlugin.normalizeName(option.name),
chunks: option.chunks,
enforce: option.enforce,
minSize: option.minSize,
minChunks: option.minChunks,
maxAsyncRequests: option.maxAsyncRequests,
maxInitialRequests: option.maxInitialRequests,
filename: option.filename,
reuseExistingChunk: option.reuseExistingChunk
});
}
}
return results;
};
return fn;
}
const fn = () => {};
return fn;
}
static checkTest(test, module, chunks) {
if (test === undefined) return true;
if (typeof test === "function") return test(module, chunks);
if (typeof test === "boolean") return test;
const names = chunks
.map(c => c.name)
.concat(module.nameForCondition ? [module.nameForCondition()] : [])
.filter(Boolean);
if (typeof test === "string") {
for (const name of names) if (name.startsWith(test)) return true;
return false;
}
if (test instanceof RegExp) {
for (const name of names) if (test.test(name)) return true;
return false;
}
return false;
}
apply(compiler) {
compiler.hooks.thisCompilation.tap("SplitChunksPlugin", compilation => {
let alreadyOptimized = false;
compilation.hooks.unseal.tap("SplitChunksPlugin", () => {
alreadyOptimized = false;
});
compilation.hooks.optimizeChunksAdvanced.tap(
"SplitChunksPlugin",
chunks => {
if (alreadyOptimized) return;
alreadyOptimized = true;
// Give each selected chunk an index (to create strings from chunks)
const indexMap = new Map();
let index = 1;
for (const chunk of chunks) {
indexMap.set(chunk, index++);
}
const getKey = chunks => {
return Array.from(chunks, c => indexMap.get(c))
.sort()
.join();
};
// Create a list of possible combinations
const chunkSetsInGraph = new Map(); // Map<string, Set<Chunk>>
for (const module of compilation.modules) {
const chunkIndices = getKey(module.chunksIterable);
chunkSetsInGraph.set(chunkIndices, new Set(module.chunksIterable));
}
const combinations = new Map(); // Map<string, Set<Chunk>[]>
for (const [key, chunksSet] of chunkSetsInGraph) {
var array = [];
for (const set of chunkSetsInGraph.values()) {
if (isSubset(chunksSet, set)) {
array.push(set);
}
}
combinations.set(key, array);
}
// Map a list of chunks to a list of modules
// For the key the chunk "index" is used, the value is a SortableSet of modules
const chunksInfoMap = new Map();
// Walk through all modules
for (const module of compilation.modules) {
// Get array of chunks
const chunks = module.getChunks();
// Get cache group
let cacheGroups = this.options.getCacheGroups(module, chunks);
if (!Array.isArray(cacheGroups)) continue;
for (const cacheGroupSource of cacheGroups) {
const cacheGroup = {
key: cacheGroupSource.key,
priority: cacheGroupSource.priority || 0,
chunks: cacheGroupSource.chunks || this.options.chunks,
minSize:
cacheGroupSource.minSize !== undefined
? cacheGroupSource.minSize
: cacheGroupSource.enforce ? 0 : this.options.minSize,
minChunks:
cacheGroupSource.minChunks !== undefined
? cacheGroupSource.minChunks
: cacheGroupSource.enforce ? 1 : this.options.minChunks,
maxAsyncRequests:
cacheGroupSource.maxAsyncRequests !== undefined
? cacheGroupSource.maxAsyncRequests
: cacheGroupSource.enforce
? Infinity
: this.options.maxAsyncRequests,
maxInitialRequests:
cacheGroupSource.maxInitialRequests !== undefined
? cacheGroupSource.maxInitialRequests
: cacheGroupSource.enforce
? Infinity
: this.options.maxInitialRequests,
getName:
cacheGroupSource.getName !== undefined
? cacheGroupSource.getName
: this.options.getName,
filename:
cacheGroupSource.filename !== undefined
? cacheGroupSource.filename
: this.options.filename,
reuseExistingChunk: cacheGroupSource.reuseExistingChunk
};
// For all combination of chunk selection
for (const chunkCombination of combinations.get(getKey(chunks))) {
// Get indices of chunks in which this module occurs
const chunkIndices = Array.from(chunkCombination, chunk =>
indexMap.get(chunk)
);
// Break if minimum number of chunks is not reached
if (chunkIndices.length < cacheGroup.minChunks) continue;
// Select chunks by configuration
const selectedChunks =
cacheGroup.chunks === "initial"
? Array.from(chunkCombination).filter(chunk =>
chunk.canBeInitial()
)
: cacheGroup.chunks === "async"
? Array.from(chunkCombination).filter(
chunk => !chunk.canBeInitial()
)
: Array.from(chunkCombination);
// Determine name for split chunk
const name = cacheGroup.getName(
module,
selectedChunks,
cacheGroup.key
);
// Create key for maps
// When it has a name we use the name as key
// Elsewise we create the key from chunks and cache group key
// This automatically merges equal names
const chunksKey = getKey(selectedChunks);
const key =
(name && `name:${name}`) ||
`chunks:${chunksKey} key:${cacheGroup.key}`;
// Add module to maps
let info = chunksInfoMap.get(key);
if (info === undefined) {
chunksInfoMap.set(
key,
(info = {
modules: new SortableSet(undefined, sortByIdentifier),
cacheGroup,
name,
chunks: new Map(),
reusedableChunks: new Set(),
chunksKeys: new Set()
})
);
}
info.modules.add(module);
if (!info.chunksKeys.has(chunksKey)) {
info.chunksKeys.add(chunksKey);
for (const chunk of selectedChunks) {
info.chunks.set(chunk, chunk.getNumberOfModules());
}
}
}
}
}
for (const [key, info] of chunksInfoMap) {
// Get size of module lists
info.size = getModulesSize(info.modules);
if (info.size < info.cacheGroup.minSize) {
chunksInfoMap.delete(key);
}
}
let changed = false;
while (chunksInfoMap.size > 0) {
// Find best matching entry
let bestEntryKey;
let bestEntry;
for (const pair of chunksInfoMap) {
const key = pair[0];
const info = pair[1];
if (bestEntry === undefined) {
bestEntry = info;
bestEntryKey = key;
} else if (compareEntries(bestEntry, info) < 0) {
bestEntry = info;
bestEntryKey = key;
}
}
const item = bestEntry;
chunksInfoMap.delete(bestEntryKey);
let chunkName = item.name;
// Variable for the new chunk (lazy created)
let newChunk;
// When no chunk name, check if we can reuse a chunk instead of creating a new one
let isReused = false;
if (item.cacheGroup.reuseExistingChunk) {
for (const pair of item.chunks) {
if (pair[1] === item.modules.size) {
const chunk = pair[0];
if (chunk.hasEntryModule()) continue;
if (!newChunk || !newChunk.name) newChunk = chunk;
else if (
chunk.name &&
chunk.name.length < newChunk.name.length
)
newChunk = chunk;
else if (
chunk.name &&
chunk.name.length === newChunk.name.length &&
chunk.name < newChunk.name
)
newChunk = chunk;
chunkName = undefined;
isReused = true;
}
}
}
// Walk through all chunks
for (const chunk of item.chunks.keys()) {
// skip if we address ourself
if (chunk.name === chunkName || chunk === newChunk) continue;
// respect max requests when not enforced
const maxRequests = chunk.isOnlyInitial()
? item.cacheGroup.maxInitialRequests
: chunk.canBeInitial()
? Math.min(
item.cacheGroup.maxInitialRequests,
item.cacheGroup.maxAsyncRequests
)
: item.cacheGroup.maxAsyncRequests;
if (isFinite(maxRequests) && getRequests(chunk) >= maxRequests)
continue;
if (newChunk === undefined) {
// Create the new chunk
newChunk = compilation.addChunk(chunkName);
}
// Add graph connections for splitted chunk
chunk.split(newChunk);
// Remove all selected modules from the chunk
for (const module of item.modules) {
chunk.removeModule(module);
module.rewriteChunkInReasons(chunk, [newChunk]);
}
}
// If we successfully created a new chunk or reused one
if (newChunk) {
// Add a note to the chunk
newChunk.chunkReason = isReused
? "reused as split chunk"
: "split chunk";
if (item.cacheGroup.key) {
newChunk.chunkReason += ` (cache group: ${
item.cacheGroup.key
})`;
}
if (chunkName) {
newChunk.chunkReason += ` (name: ${chunkName})`;
// If the chosen name is already an entry point we remove the entry point
const entrypoint = compilation.entrypoints.get(chunkName);
if (entrypoint) {
compilation.entrypoints.delete(chunkName);
entrypoint.remove();
newChunk.entryModule = undefined;
}
}
if (item.cacheGroup.filename) {
if (!newChunk.isOnlyInitial()) {
throw new Error(
"SplitChunksPlugin: You are trying to set a filename for a chunk which is (also) loaded on demand. " +
"The runtime can only handle loading of chunks which match the chunkFilename schema. " +
"Using a custom filename would fail at runtime. " +
`(cache group: ${item.cacheGroup.key})`
);
}
newChunk.filenameTemplate = item.cacheGroup.filename;
}
if (!isReused) {
// Add all modules to the new chunk
for (const module of item.modules) {
GraphHelpers.connectChunkAndModule(newChunk, module);
}
}
// remove all modules from other entries and update size
for (const [key, info] of chunksInfoMap) {
if (isOverlap(info.chunks, item.chunks)) {
const oldSize = info.modules.size;
for (const module of item.modules) {
info.modules.delete(module);
}
if (info.modules.size === 0) {
chunksInfoMap.delete(key);
continue;
}
if (info.modules.size !== oldSize) {
info.size = getModulesSize(info.modules);
if (info.size < info.cacheGroup.minSize)
chunksInfoMap.delete(key);
}
}
}
changed = true;
}
}
if (changed) return true;
}
);
});
}
};