Skip to content

Commit b07dc3d

Browse files
authored
Merge pull request webpack#3745 from shubheksha/refactor-CommonsChunkPlugin
refactor(ES6): upgrade commonsChunkPlugin to ES6
2 parents 44fb71a + d7384e2 commit b07dc3d

1 file changed

Lines changed: 163 additions & 166 deletions

File tree

lib/optimize/CommonsChunkPlugin.js

Lines changed: 163 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -2,183 +2,180 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var nextIdent = 0;
5+
"use strict";
6+
let nextIdent = 0;
7+
class CommonsChunkPlugin {
8+
constructor(options) {
9+
if(arguments.length > 1) {
10+
throw new Error(`Deprecation notice: CommonsChunkPlugin now only takes a single argument. Either an options
11+
object *or* the name of the chunk.
12+
Example: if your old code looked like this:
13+
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js')
14+
You would change it to:
15+
new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.bundle.js' })
16+
The available options are:
17+
name: string
18+
names: string[]
19+
filename: string
20+
minChunks: number
21+
chunks: string[]
22+
children: boolean
23+
async: boolean
24+
minSize: number`);
25+
}
626

7-
function CommonsChunkPlugin(options) {
8-
if(arguments.length > 1) {
9-
throw new Error("Deprecation notice: CommonsChunkPlugin now only takes a single argument. Either an options " +
10-
"object *or* the name of the chunk.\n" +
11-
"Example: if your old code looked like this:\n" +
12-
" new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.bundle.js')\n\n" +
13-
"You would change it to:\n" +
14-
" new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.bundle.js' })\n\n" +
15-
"The available options are:\n" +
16-
" name: string\n" +
17-
" names: string[]\n" +
18-
" filename: string\n" +
19-
" minChunks: number\n" +
20-
" chunks: string[]\n" +
21-
" children: boolean\n" +
22-
" async: boolean\n" +
23-
" minSize: number\n");
27+
if(Array.isArray(options) || typeof options === "string") {
28+
options = {
29+
name: options
30+
};
31+
}
32+
this.chunkNames = options.name || options.names;
33+
this.filenameTemplate = options.filename;
34+
this.minChunks = options.minChunks;
35+
this.selectedChunks = options.chunks;
36+
if(options.children) this.selectedChunks = false;
37+
this.async = options.async;
38+
this.minSize = options.minSize;
39+
this.ident = __filename + (nextIdent++);
2440
}
25-
if(Array.isArray(options) || typeof options === "string") {
26-
options = {
27-
name: options
28-
};
29-
}
30-
this.chunkNames = options.name || options.names;
31-
this.filenameTemplate = options.filename;
32-
this.minChunks = options.minChunks;
33-
this.selectedChunks = options.chunks;
34-
if(options.children) this.selectedChunks = false;
35-
this.async = options.async;
36-
this.minSize = options.minSize;
37-
this.ident = __filename + (nextIdent++);
38-
}
39-
40-
module.exports = CommonsChunkPlugin;
41-
CommonsChunkPlugin.prototype.apply = function(compiler) {
42-
var chunkNames = this.chunkNames;
43-
var filenameTemplate = this.filenameTemplate;
44-
var minChunks = this.minChunks;
45-
var selectedChunks = this.selectedChunks;
46-
var asyncOption = this.async;
47-
var minSize = this.minSize;
48-
var ident = this.ident;
49-
compiler.plugin("this-compilation", function(compilation) {
50-
compilation.plugin(["optimize-chunks", "optimize-extracted-chunks"], function(chunks) {
51-
// only optimize once
52-
if(compilation[ident]) return;
53-
compilation[ident] = true;
41+
apply(compiler) {
42+
const chunkNames = this.chunkNames;
43+
const filenameTemplate = this.filenameTemplate;
44+
const minChunks = this.minChunks;
45+
const selectedChunks = this.selectedChunks;
46+
const asyncOption = this.async;
47+
const minSize = this.minSize;
48+
const ident = this.ident;
49+
compiler.plugin("this-compilation", (compilation) => {
50+
compilation.plugin(["optimize-chunks", "optimize-extracted-chunks"], (chunks) => {
51+
// only optimize once
52+
if(compilation[ident]) return;
53+
compilation[ident] = true;
5454

55-
var commonChunks;
56-
if(!chunkNames && (selectedChunks === false || asyncOption)) {
57-
commonChunks = chunks;
58-
} else if(Array.isArray(chunkNames) || typeof chunkNames === "string") {
59-
commonChunks = [].concat(chunkNames).map(function(chunkName) {
60-
var chunk = chunks.filter(function(chunk) {
61-
return chunk.name === chunkName;
62-
})[0];
63-
if(!chunk) {
64-
chunk = this.addChunk(chunkName);
65-
}
66-
return chunk;
67-
}, this);
68-
} else {
69-
throw new Error("Invalid chunkNames argument");
70-
}
71-
commonChunks.forEach(function processCommonChunk(commonChunk, idx) {
72-
var usedChunks;
73-
if(Array.isArray(selectedChunks)) {
74-
usedChunks = chunks.filter(function(chunk) {
75-
if(chunk === commonChunk) return false;
76-
return selectedChunks.indexOf(chunk.name) >= 0;
77-
});
78-
} else if(selectedChunks === false || asyncOption) {
79-
usedChunks = (commonChunk.chunks || []).filter(function(chunk) {
80-
// we can only move modules from this chunk if the "commonChunk" is the only parent
81-
return asyncOption || chunk.parents.length === 1;
55+
let commonChunks;
56+
if(!chunkNames && (selectedChunks === false || asyncOption)) {
57+
commonChunks = chunks;
58+
} else if(Array.isArray(chunkNames) || typeof chunkNames === "string") {
59+
commonChunks = [].concat(chunkNames).map((chunkName) => {
60+
let chunk = chunks.filter((chunk) => chunk.name === chunkName)[0];
61+
if(!chunk) {
62+
chunk = compilation.addChunk(chunkName);
63+
}
64+
return chunk;
8265
});
8366
} else {
84-
if(commonChunk.parents.length > 0) {
85-
compilation.errors.push(new Error("CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (" + commonChunk.name + ")"));
86-
return;
87-
}
88-
usedChunks = chunks.filter(function(chunk) {
89-
var found = commonChunks.indexOf(chunk);
90-
if(found >= idx) return false;
91-
return chunk.hasRuntime();
92-
});
67+
throw new Error("Invalid chunkNames argument");
9368
}
94-
if(asyncOption) {
95-
var asyncChunk = this.addChunk(typeof asyncOption === "string" ? asyncOption : undefined);
96-
asyncChunk.chunkReason = "async commons chunk";
97-
asyncChunk.extraAsync = true;
98-
asyncChunk.addParent(commonChunk);
99-
commonChunk.addChunk(asyncChunk);
100-
commonChunk = asyncChunk;
101-
}
102-
var reallyUsedModules = [];
103-
if(minChunks !== Infinity) {
104-
var commonModulesCount = [];
105-
var commonModules = [];
106-
usedChunks.forEach(function(chunk) {
107-
chunk.modules.forEach(function(module) {
108-
var idx = commonModules.indexOf(module);
109-
if(idx < 0) {
110-
commonModules.push(module);
111-
commonModulesCount.push(1);
112-
} else {
113-
commonModulesCount[idx]++;
114-
}
69+
commonChunks.forEach(function processCommonChunk(commonChunk, idx) {
70+
let usedChunks;
71+
if(Array.isArray(selectedChunks)) {
72+
usedChunks = chunks.filter(chunk => chunk !== commonChunk && selectedChunks.indexOf(chunk.name) >= 0);
73+
} else if(selectedChunks === false || asyncOption) {
74+
usedChunks = (commonChunk.chunks || []).filter((chunk) => {
75+
// we can only move modules from this chunk if the "commonChunk" is the only parent
76+
return asyncOption || chunk.parents.length === 1;
11577
});
116-
});
117-
var _minChunks = (minChunks || Math.max(2, usedChunks.length));
118-
commonModulesCount.forEach(function(count, idx) {
119-
var module = commonModules[idx];
120-
if(typeof minChunks === "function") {
121-
if(!minChunks(module, count))
122-
return;
123-
} else if(count < _minChunks) {
78+
} else {
79+
if(commonChunk.parents.length > 0) {
80+
compilation.errors.push(new Error("CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (" + commonChunk.name + ")"));
12481
return;
12582
}
126-
if(module.chunkCondition && !module.chunkCondition(commonChunk))
127-
return;
128-
reallyUsedModules.push(module);
129-
});
130-
}
131-
if(minSize) {
132-
var size = reallyUsedModules.reduce(function(a, b) {
133-
return a + b.size();
134-
}, 0);
135-
if(size < minSize)
136-
return;
137-
}
138-
var reallyUsedChunks = [];
139-
reallyUsedModules.forEach(function(module) {
140-
usedChunks.forEach(function(chunk) {
141-
if(module.removeChunk(chunk)) {
142-
if(reallyUsedChunks.indexOf(chunk) < 0)
143-
reallyUsedChunks.push(chunk);
144-
}
145-
});
146-
commonChunk.addModule(module);
147-
module.addChunk(commonChunk);
148-
});
149-
if(asyncOption) {
150-
reallyUsedChunks.forEach(function(chunk) {
151-
if(chunk.isInitial())
152-
return;
153-
chunk.blocks.forEach(function(block) {
154-
block.chunks.unshift(commonChunk);
155-
commonChunk.addBlock(block);
83+
usedChunks = chunks.filter((chunk) => {
84+
const found = commonChunks.indexOf(chunk);
85+
if(found >= idx) return false;
86+
return chunk.hasRuntime();
15687
});
157-
});
158-
asyncChunk.origins = reallyUsedChunks.map(function(chunk) {
159-
return chunk.origins.map(function(origin) {
160-
var newOrigin = Object.create(origin);
161-
newOrigin.reasons = (origin.reasons || []).slice();
162-
newOrigin.reasons.push("async commons");
163-
return newOrigin;
88+
}
89+
let asyncChunk;
90+
if(asyncOption) {
91+
asyncChunk = compilation.addChunk(typeof asyncOption === "string" ? asyncOption : undefined);
92+
asyncChunk.chunkReason = "async commons chunk";
93+
asyncChunk.extraAsync = true;
94+
asyncChunk.addParent(commonChunk);
95+
commonChunk.addChunk(asyncChunk);
96+
commonChunk = asyncChunk;
97+
}
98+
const reallyUsedModules = [];
99+
if(minChunks !== Infinity) {
100+
const commonModulesCount = [];
101+
const commonModules = [];
102+
usedChunks.forEach((chunk) => {
103+
chunk.modules.forEach((module) => {
104+
const idx = commonModules.indexOf(module);
105+
if(idx < 0) {
106+
commonModules.push(module);
107+
commonModulesCount.push(1);
108+
} else {
109+
commonModulesCount[idx]++;
110+
}
111+
});
164112
});
165-
}).reduce(function(arr, a) {
166-
arr.push.apply(arr, a);
167-
return arr;
168-
}, []);
169-
} else {
170-
usedChunks.forEach(function(chunk) {
171-
chunk.parents = [commonChunk];
172-
chunk.entrypoints.forEach(function(ep) {
173-
ep.insertChunk(commonChunk, chunk);
113+
const _minChunks = (minChunks || Math.max(2, usedChunks.length));
114+
commonModulesCount.forEach((count, idx) => {
115+
const module = commonModules[idx];
116+
if(typeof minChunks === "function") {
117+
if(!minChunks(module, count))
118+
return;
119+
} else if(count < _minChunks) {
120+
return;
121+
}
122+
if(module.chunkCondition && !module.chunkCondition(commonChunk))
123+
return;
124+
reallyUsedModules.push(module);
174125
});
175-
commonChunk.addChunk(chunk);
126+
}
127+
if(minSize) {
128+
const size = reallyUsedModules.reduce((a, b) => {
129+
return a + b.size();
130+
}, 0);
131+
if(size < minSize)
132+
return;
133+
}
134+
const reallyUsedChunks = new Set();
135+
reallyUsedModules.forEach((module) => {
136+
usedChunks.forEach((chunk) => {
137+
if(module.removeChunk(chunk)) {
138+
reallyUsedChunks.add(chunk);
139+
}
140+
});
141+
commonChunk.addModule(module);
142+
module.addChunk(commonChunk);
176143
});
177-
}
178-
if(filenameTemplate)
179-
commonChunk.filenameTemplate = filenameTemplate;
180-
}, this);
181-
return true;
144+
if(asyncOption) {
145+
for(const chunk of reallyUsedChunks) {
146+
if(chunk.isInitial()) continue;
147+
chunk.blocks.forEach((block) => {
148+
block.chunks.unshift(commonChunk);
149+
commonChunk.addBlock(block);
150+
});
151+
}
152+
asyncChunk.origins = Array.from(reallyUsedChunks).map((chunk) => {
153+
return chunk.origins.map((origin) => {
154+
const newOrigin = Object.create(origin);
155+
newOrigin.reasons = (origin.reasons || []).slice();
156+
newOrigin.reasons.push("async commons");
157+
return newOrigin;
158+
});
159+
}).reduce((arr, a) => {
160+
arr.push.apply(arr, a);
161+
return arr;
162+
}, []);
163+
} else {
164+
usedChunks.forEach((chunk) => {
165+
chunk.parents = [commonChunk];
166+
chunk.entrypoints.forEach((ep) => {
167+
ep.insertChunk(commonChunk, chunk);
168+
});
169+
commonChunk.addChunk(chunk);
170+
});
171+
}
172+
if(filenameTemplate)
173+
commonChunk.filenameTemplate = filenameTemplate;
174+
});
175+
return true;
176+
});
182177
});
183-
});
184-
};
178+
}
179+
}
180+
181+
module.exports = CommonsChunkPlugin;

0 commit comments

Comments
 (0)