-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
318 lines (243 loc) · 10.1 KB
/
index.js
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
const Chunk = require('webpack/lib/Chunk');
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
const loaderUtils = require('loader-utils');
class MultiCommonChunksPlugin extends CommonsChunkPlugin {
constructor(commonsChunkPluginParams, params) {
super(commonsChunkPluginParams);
this.commonChunkPrefix = params.commonChunkName || 'multi_common_chunk_';
this.outputName = params.outputName || '[name].[ext]';
this.processOutput = params.processOutput;
}
/**
* We assing multiCommonChunksUsedBy for each module
* to know how many entry chunks require this module
* For exmple
* ["app_article_browse", "app_driver_browse", "app_team_browse", "app_video_browse"]
*/
updateAvailableModulesUsage(extractableModules, chunks) {
for (const modIndex in extractableModules) {
let extractableModule = extractableModules[modIndex];
extractableModule.multiCommonChunksUsedBy = [];
chunks.forEach(chunk => {
if (chunk.modulesIterable.has(extractableModule)) {
extractableModule.multiCommonChunksUsedBy.push(chunk.name);
}
})
}
}
getCommonChunkByCommonIndex(commonChunks, index) {
return commonChunks.filter(chunk => chunk.multiCommonChunkIndex === index)[0];
}
mergeSmallCommonChunks(commonChunks, entryChunks) {
// keep track of chunk ID's that are no longer available
const removedChunkBlocks = [];
// list of small chunk id's, that must be merged into entry chunk
const smallMergedChunks = [];
// holds refenreces map for merged chunks
// require to add all required chunks on merge
const removedChunksMap = {};
const bigCommonChunks = [];
// this.minSize
let smallCommonChunks = commonChunks.filter(chunk => {
chunk.multiCommonChunkSize = this.getChunkSize(chunk);
const isSmall = chunk.multiCommonChunkSize < this.minSize;
if (!isSmall) {
bigCommonChunks.push(chunk);
}
return isSmall;
});
function moveSmallCommonChunkIntoEntry(entryChunk, commonChunk) {
commonChunk.forEachModule(mod => {
// mod.removeChunk(commonChunk);
entryChunk.addModule(mod);
mod.addChunk(entryChunk);
});
}
// TODO: sort entry chunks by total small chunk size (or by small chunks count???)
// this way we would get maximum value from merging
// we need to loop through all required commonchunks of entry chunk
// and merge those that are in smallCommmonChunks array
entryChunks.forEach(entryChunk => {
// store newly removed chunks, we will need them
// to store references to merged chunk
const newRemovedChunkIds = [];
// excule all removed ID's first
removedChunkBlocks.forEach(removedBlock => {
let chunkAddedFlag = false;
removedBlock.forEach(removedChunkId => {
if (
!chunkAddedFlag &&
removedBlock.includes(removedChunkId)
) {
chunkAddedFlag = true;
let targetRemovedChunkId = removedChunksMap[removedChunkId];
if (!entryChunk.multiCommonChunksRequired) {
console.log('entryChunk.name => ', entryChunk.name);
}
// add required target chunk only if entryChunk doesn't have it
if (!entryChunk.multiCommonChunksRequired.includes(targetRemovedChunkId)) {
entryChunk.multiCommonChunksRequired.push(targetRemovedChunkId);
}
}
const index = entryChunk.multiCommonChunksRequired.indexOf(removedChunkId);
if (index === -1) return;
entryChunk.multiCommonChunksRequired.splice(index, 1);
})
});
if (!smallCommonChunks.length) return;
// flag which indicates that we found our first match
// and we don't need to remove this chunk, just merge all other chunks
// into this one
let isFirstMatch = true;
const chunksToMerge = [];
// with each iteration merge all small chunks required by entryChunk
// and add all merged chunk ID's to removedChunkIds array
// to remove them in on other entry points
smallCommonChunks = smallCommonChunks.filter(chunk => {
const index = entryChunk.multiCommonChunksRequired.indexOf(chunk.multiCommonChunkIndex);
if (index === -1) return true;
chunksToMerge.push(chunk);
if (isFirstMatch) {
isFirstMatch = false;
} else {
newRemovedChunkIds.push(chunk.multiCommonChunkIndex);
entryChunk.multiCommonChunksRequired.splice(index, 1);
}
return false;
});
// if there are no required small chunks to merge, continue...
if (!chunksToMerge.length) return;
const mainChunk = this.mergeChunksIntoFirst(chunksToMerge);
removedChunkBlocks.push(newRemovedChunkIds);
// store correct chunks mapping, to update indexes in other entryChunks
newRemovedChunkIds.forEach(id => {
removedChunksMap[id] = mainChunk.multiCommonChunkIndex;
});
// if merged chunk is still not large enough
if (this.getChunkSize(mainChunk) < this.minSize) {
smallMergedChunks.push(mainChunk);
} else {
bigCommonChunks.push(mainChunk);
}
});
// if after all merge operations there are still small chunks
// merge all of them into one and update entry chunks to use it
if (smallMergedChunks.length) {
bigCommonChunks.push(this.mergeSmallMergedChunks(entryChunks, smallMergedChunks));
}
return bigCommonChunks;
}
mergeSmallMergedChunks(entryChunks, smallMergedChunks) {
const removeChunkIds = smallMergedChunks.map(chunk => chunk.multiCommonChunkIndex),
mainChunkId = removeChunkIds.shift();
// we need to remove all merged chunks and left only chunk we merged into
entryChunks.forEach(entryChunk => {
let chunkRemoved = false;
removeChunkIds.forEach(removeChunkId => {
const index = entryChunk.multiCommonChunksRequired.indexOf(removeChunkId);
if (index === -1) return;
chunkRemoved = true;
entryChunk.multiCommonChunksRequired.splice(index, 1);
});
if (!chunkRemoved) return;
// add required target chunk only if entryChunk doesn't have it
if (!entryChunk.multiCommonChunksRequired.includes(mainChunkId)) {
entryChunk.multiCommonChunksRequired.push(mainChunkId);
}
});
return this.mergeChunksIntoFirst(smallMergedChunks);
}
mergeChunksIntoFirst(chunks) {
const mainChunk = chunks.shift();
// unlink modules from old chunk, and link to target merge chunk
chunks.forEach(chunk => {
chunk.forEachModule(mod => {
mod.removeChunk(chunk);
mainChunk.addModule(mod);
mod.addChunk(mainChunk);
});
});
return mainChunk;
}
getChunkSize(chunk) {
const size = chunk.getModules().reduce((totalSize, module) => {
const source = this.extension === 'css' ? module.source() : module._source;
return totalSize + source.size();
}, 0);
return size;
}
getPath(source, options) {
// "/" required for interpolateName to change "name"
const resourcePath = `/${this.commonChunkPrefix}${options.index}.${this.extension}`;
return loaderUtils.interpolateName({
resourcePath
}, this.outputName, {
content: source,
});
}
removeExtractedModules(extractedChunks) {
extractedChunks.forEach(extractedChunk => {
if (!extractedChunk.multiCommonChunksExtractModules) return;
for (let index in extractedChunk.multiCommonChunksExtractModules) {
var extractedModules = extractedChunk.multiCommonChunksExtractModules[index];
extractedModules.forEach(extractedModule => {
extractedModule.removeChunk(extractedChunk);
});
}
});
}
assignCommonIndexes(extractableModules) {
var index = -1,
commonModulesIndex = {};
extractableModules.forEach(mod => {
let modKey = mod.multiCommonChunksUsedBy.join('_'),
// ExtractedModules don't have forEachChunk iterator
moduleChunksIterator = mod.forEachChunk ? mod.forEachChunk.bind(mod) : mod.chunks.forEach.bind(mod.chunks);
if (mod.multiCommonChunksUsedBy.length === 1) return;
if (!commonModulesIndex.hasOwnProperty(modKey)) {
mod.multiCommonChunkId = ++index;
commonModulesIndex[modKey] = index;
} else {
mod.multiCommonChunkId = commonModulesIndex[modKey];
}
// add list of required commonChunks to entryChunks
moduleChunksIterator(function(chunk) {
if (!chunk.multiCommonChunksRequired) {
chunk.multiCommonChunksRequired = [];
}
if (!chunk.multiCommonChunksRequired.includes(index)) {
chunk.multiCommonChunksRequired.push(index);
}
if (!chunk.multiCommonChunksExtractModules) {
chunk.multiCommonChunksExtractModules = {};
}
if (!chunk.multiCommonChunksExtractModules[index]) {
chunk.multiCommonChunksExtractModules[index] = [];
}
chunk.multiCommonChunksExtractModules[index].push(mod);
});
});
// return common chunks count
return index + 1;
}
createCommonChunks(extractableModules, commonChunksCount) {
const commonChunks = [];
// create chunks for common modules
for (let i = 0; i < commonChunksCount; i++) {
let newChunkName = `${this.commonChunkPrefix}${i}`,
newChunk;
newChunk = new Chunk(newChunkName);
newChunk.multiCommonChunkIndex = i;
commonChunks.push(newChunk);
}
extractableModules.forEach(module => {
let chunk = commonChunks[module.multiCommonChunkId];
// if module don't belogn to any common chunk, skip it
if (!chunk) return;
chunk.addModule(module);
module.addChunk(chunk);
});
return commonChunks;
}
}
module.exports = MultiCommonChunksPlugin;