Skip to content

Commit acdabb1

Browse files
committed
linting, comment and minor improvements
1 parent c71dabc commit acdabb1

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

lib/WebpackOptionsApply.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,10 @@ class WebpackOptionsApply extends OptionsApply {
288288
if(options.optimization.concatenateModules)
289289
new ModuleConcatenationPlugin().apply(compiler);
290290
if(options.optimization.asyncCommonsChunks) {
291-
new AutomaticCommonsChunksPlugin(Object.assign(
292-
{},
293-
options.optimization.asyncCommonsChunks,
294-
{
295-
initialChunks: false
296-
}
297-
)).apply(compiler);
291+
const accpOptions = Object.assign({}, options.optimization.asyncCommonsChunks, {
292+
initialChunks: false
293+
});
294+
new AutomaticCommonsChunksPlugin(accpOptions).apply(compiler);
298295
}
299296
if(options.optimization.initialCommonsChunks || options.optimization.initialVendorsChunk) {
300297
let nameOption = options.optimization.initialVendorsChunk;
@@ -330,15 +327,12 @@ class WebpackOptionsApply extends OptionsApply {
330327
}
331328
}))(nameOption);
332329
}
333-
new AutomaticCommonsChunksPlugin(Object.assign(
334-
{},
335-
options.optimization.initialCommonsChunks,
336-
{
337-
initialChunks: true,
338-
onlyNamed: !options.optimization.initialCommonsChunks,
339-
name: nameOption
340-
}
341-
)).apply(compiler);
330+
const accpOptions = Object.assign({}, options.optimization.initialCommonsChunks, {
331+
initialChunks: true,
332+
onlyNamed: !options.optimization.initialCommonsChunks,
333+
name: nameOption
334+
});
335+
new AutomaticCommonsChunksPlugin(accpOptions).apply(compiler);
342336
}
343337
if(options.optimization.noEmitOnErrors)
344338
new NoEmitOnErrorsPlugin().apply(compiler);

lib/optimize/AutomaticCommonsChunksPlugin.js

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,35 +33,48 @@ module.exports = class AutomaticCommonsChunksPlugin {
3333
apply(compiler) {
3434
compiler.hooks.compilation.tap("AutomaticCommonsChunksPlugin", compilation => {
3535
compilation.hooks.optimizeChunksAdvanced.tap("AutomaticCommonsChunksPlugin", chunks => {
36+
// Give each selected chunk an index (to create strings from chunks)
3637
const indexMap = new Map();
3738
let index = 1;
3839
for(const chunk of chunks) {
3940
if(chunk.isInitial() === this.options.initialChunks)
4041
indexMap.set(chunk, index++);
4142
}
43+
// Map a list of chunks to a list of modules
44+
// For the key the chunk "index" is used, the value is a SortableSet of modules
4245
const chunksModulesMap = new Map();
46+
// Map a list of chunks to a name (not every list of chunks is mapped, only when "name" option is used)
4347
const chunksNameMap = new Map();
48+
// Walk through all modules
4449
for(const module of compilation.modules) {
50+
// Get indices of chunks in which this module occurs
4551
const chunkIndices = Array.from(module.chunksIterable, chunk => indexMap.get(chunk)).filter(Boolean);
52+
// Get name from "name" option
4653
let name = this.options.name;
4754
if(typeof name === "function")
4855
name = name(module);
4956
if(name) {
50-
chunkIndices.push(name);
57+
chunkIndices.push(`[${name}]`);
5158
} else if(this.options.onlyNamed) {
59+
// May skip unnamed chunks if "onlyNamed" is used
5260
continue;
5361
}
62+
// skip for modules which are only in one chunk or don't get a name
5463
if(chunkIndices.length <= 1) continue;
64+
// Create key for maps
5565
const key = chunkIndices.sort().join();
66+
// Add module to maps
5667
let modules = chunksModulesMap.get(key);
5768
if(modules === undefined) {
5869
chunksModulesMap.set(key, modules = new SortableSet(undefined, sortByIdentifier));
5970
if(name) {
71+
// Note name when used
6072
chunksNameMap.set(key, name);
6173
}
6274
}
6375
modules.add(module);
6476
}
77+
// Get size of module lists and sort them by name and size
6578
const entries = Array.from(chunksModulesMap.entries(), pair => {
6679
const modules = pair[1];
6780
const size = Array.from(modules, m => m.size()).reduce((a, b) => a + b, 0);
@@ -71,9 +84,8 @@ module.exports = class AutomaticCommonsChunksPlugin {
7184
size
7285
};
7386
}).sort((a, b) => {
87+
// Sort
7488
// 1. by chunk name
75-
// 2. by total modules size
76-
// 3. by module identifiers
7789
const chunkNameA = chunksNameMap.get(a.key);
7890
const chunkNameB = chunksNameMap.get(b.key);
7991
if(chunkNameA && !chunkNameB) return -1;
@@ -82,10 +94,12 @@ module.exports = class AutomaticCommonsChunksPlugin {
8294
if(chunkNameA < chunkNameB) return -1;
8395
if(chunkNameA > chunkNameB) return 1;
8496
}
97+
// 2. by total modules size
8598
const diffSize = b.size - a.size;
8699
if(diffSize) return diffSize;
87100
const modulesA = a.modules;
88101
const modulesB = b.modules;
102+
// 3. by module identifiers
89103
const diff = modulesA.size - modulesB.size;
90104
if(diff) return diff;
91105
modulesA.sort();
@@ -102,44 +116,55 @@ module.exports = class AutomaticCommonsChunksPlugin {
102116
if(aModuleIdentifier < bModuleIdentifier) return 1;
103117
}
104118
});
119+
105120
let changed = false;
106-
for(const { key, modules, size } of entries) {
107-
const chunkName = chunksNameMap.get(key);
108-
if(!chunkName && size < this.options.minSize) continue;
109-
const newChunk = compilation.addChunk(chunkName);
110-
let splitted = false;
111-
const firstModule = modules.values().next().value;
121+
// Walk though all entries
122+
for(const item of entries) {
123+
const chunkName = chunksNameMap.get(item.key);
124+
// Skip if size is smaller than minimum size
125+
if(!chunkName && item.size < this.options.minSize) continue;
126+
// Variable for the new chunk (lazy created)
127+
let newChunk;
128+
// Walk through all chunks
129+
// All modules have the same chunks so we can use the first module
130+
const firstModule = item.modules.values().next().value;
112131
for(const chunk of firstModule.chunksIterable) {
113-
// skip itself when already a chunk of the module
114-
if(newChunk === chunk) continue;
132+
// skip if we address ourself
133+
if(chunk.name === chunkName) continue;
115134
// only use selected chunks
116135
if(!indexMap.get(chunk)) continue;
117136
// respect max requests when not a named chunk
118137
if(!chunkName && getRequests(chunk) >= this.options.maxRequests) continue;
119-
splitted = true;
138+
if(newChunk === undefined) {
139+
// Create the new chunk
140+
newChunk = compilation.addChunk(chunkName);
141+
}
142+
// Add graph connections for splitted chunk
120143
chunk.split(newChunk);
121-
for(const module of modules) {
144+
// Remove all selected modules from the chunk
145+
for(const module of item.modules) {
122146
chunk.removeModule(module);
123147
module.rewriteChunkInReasons(chunk, [newChunk]);
124148
}
125149
}
126-
if(splitted) {
150+
// If we successfully creates a new chunk
151+
if(newChunk) {
152+
// If the choosen name is already an entry point we remove the entry point
127153
if(chunkName) {
128154
const entrypoint = compilation.entrypoints[chunkName];
129155
if(entrypoint) {
130156
delete compilation.entrypoints[chunkName];
131157
entrypoint.remove();
132158
}
133159
}
160+
// Add a note to the chunk
134161
newChunk.chunkReason = chunkName ? "vendors chunk" : "commons chunk";
135-
for(const module of modules) {
162+
// Add all modules to the new chunk
163+
for(const module of item.modules) {
136164
newChunk.addModule(module);
137165
module.addChunk(newChunk);
138166
}
139167
changed = true;
140-
} else if(!chunkName) {
141-
newChunk.remove("empty");
142-
chunks.splice(chunks.indexOf(newChunk), 1);
143168
}
144169
}
145170
if(changed) return true;

schemas/WebpackOptions.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,7 @@
13801380
{
13811381
"type": "object",
13821382
"additionalProperties": {
1383+
"description": "An object of multiple vendors chunks, each keys specifies the name and the value is a absolute path prefix, regular expression or matcher function",
13831384
"oneOf": [
13841385
{
13851386
"instanceof": "RegExp"

0 commit comments

Comments
 (0)