Skip to content

Commit 6bd937d

Browse files
committed
make things consitent
1 parent 6c1831c commit 6bd937d

9 files changed

Lines changed: 45 additions & 18 deletions

‎lib/Chunk.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,17 @@ class Chunk {
6262
}
6363

6464
/**
65-
* @return {Set} - a set containing the chunks
65+
* @return {Array} - an array containing the chunks
6666
*/
6767
getChunks() {
68+
return Array.from(this._chunks);
69+
}
70+
71+
getNumberOfChunks() {
72+
return this._chunks.size;
73+
}
74+
75+
get chunksIterable() {
6876
return this._chunks;
6977
}
7078

@@ -483,7 +491,7 @@ Object.defineProperty(Chunk.prototype, "modules", {
483491
Object.defineProperty(Chunk.prototype, "chunks", {
484492
configurable: false,
485493
get: util.deprecate(function() {
486-
return Array.from(this._chunks);
494+
return this._chunks.getFrozenArray();
487495
}, "Chunk.chunks: Use Chunk.getChunks() instead"),
488496
set() {
489497
throw new Error("Readonly. Use Chunk.addChunk/removeChunk/getChunks to access/modify chunks.");

‎lib/HotModuleReplacementPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ module.exports = class HotModuleReplacementPlugin {
166166
.replace(/\$require\$/g, this.requireFn)
167167
.replace(/\$hash\$/g, JSON.stringify(hash))
168168
.replace(/\$requestTimeout\$/g, requestTimeout)
169-
.replace(/\/\*foreachInstalledChunks\*\//g, chunk.getChunks().size > 0 ? "for(var chunkId in installedChunks)" : `var chunkId = ${JSON.stringify(chunk.id)};`)
169+
.replace(/\/\*foreachInstalledChunks\*\//g, chunk.getNumberOfChunks() > 0 ? "for(var chunkId in installedChunks)" : `var chunkId = ${JSON.stringify(chunk.id)};`)
170170
]);
171171
});
172172

‎lib/JsonpMainTemplatePlugin.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class JsonpMainTemplatePlugin {
1313
var otherChunksInEntry = chunk.entrypoints.some(function(entrypoint) {
1414
return entrypoint.chunks.length > 1;
1515
});
16-
var onDemandChunks = chunk.getChunks().size > 0;
16+
var onDemandChunks = chunk.getNumberOfChunks() > 0;
1717
return otherChunksInEntry || onDemandChunks;
1818
}
1919
mainTemplate.plugin("local-vars", function(source, chunk) {
@@ -130,7 +130,7 @@ class JsonpMainTemplatePlugin {
130130
]);
131131
});
132132
mainTemplate.plugin("require-extensions", function(source, chunk) {
133-
if(chunk.getChunks().size === 0) return source;
133+
if(chunk.getNumberOfChunks() === 0) return source;
134134

135135
return this.asString([
136136
source,
@@ -141,7 +141,6 @@ class JsonpMainTemplatePlugin {
141141
});
142142
mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
143143
if(needChunkLoadingCode(chunk)) {
144-
var jsonpFunction = this.outputOptions.jsonpFunction;
145144
return this.asString([
146145
source,
147146
"",

‎lib/MainTemplate.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ module.exports = class MainTemplate extends Template {
9999
});
100100
this.plugin("require-extensions", (source, chunk, hash) => {
101101
const buf = [];
102-
if(chunk.getChunks().size > 0) {
102+
if(chunk.getNumberOfChunks() > 0) {
103103
buf.push("// This file contains only the entry chunk.");
104104
buf.push("// The chunk loading function for additional chunks");
105105
buf.push(`${this.requireFn}.e = function requireEnsure(chunkId) {`);
@@ -199,7 +199,7 @@ module.exports = class MainTemplate extends Template {
199199

200200
entryPointInChildren(chunk) {
201201
const checkChildren = (chunk, alreadyCheckedChunks) => {
202-
for(const child of chunk.getChunks()) {
202+
for(const child of chunk.chunksIterable) {
203203
if(!alreadyCheckedChunks.has(child)) {
204204
alreadyCheckedChunks.add(child);
205205
if(child.hasEntryModule() || checkChildren(child, alreadyCheckedChunks)) {

‎lib/node/NodeMainTemplatePlugin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = class NodeMainTemplatePlugin {
1414
apply(mainTemplate) {
1515
const asyncChunkLoading = this.asyncChunkLoading;
1616
mainTemplate.plugin("local-vars", function(source, chunk) {
17-
if(chunk.getChunks().size > 0) {
17+
if(chunk.getNumberOfChunks() > 0) {
1818
return this.asString([
1919
source,
2020
"",
@@ -28,7 +28,7 @@ module.exports = class NodeMainTemplatePlugin {
2828
return source;
2929
});
3030
mainTemplate.plugin("require-extensions", function(source, chunk) {
31-
if(chunk.getChunks().size > 0) {
31+
if(chunk.getNumberOfChunks() > 0) {
3232
return this.asString([
3333
source,
3434
"",

‎lib/optimize/CommonsChunkPlugin.js‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,7 @@ Take a look at the "name"/"names" or async/children option.`);
223223
}
224224

225225
if(asyncOrNoSelectedChunk) {
226-
// nothing to do here
227-
if(!targetChunk.getChunks()) {
228-
return [];
229-
}
230-
231-
return Array.from(targetChunk.getChunks()).filter((chunk) => {
226+
return targetChunk.getChunks().filter((chunk) => {
232227
// we only are interested in on-demand chunks
233228
if(chunk.isInitial())
234229
return false;

‎lib/util/SortableSet.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = class SortableSet extends Set {
66
super(initialIterable);
77
this._sortFn = defaultSort;
88
this._lastActiveSortFn = null;
9+
this._frozenArray = null;
910
}
1011

1112
/**
@@ -14,10 +15,21 @@ module.exports = class SortableSet extends Set {
1415
*/
1516
add(value) {
1617
this._lastActiveSortFn = null;
18+
this._frozenArray = null;
1719
super.add(value);
1820
return this;
1921
}
2022

23+
delete(value) {
24+
this._frozenArray = null;
25+
return super.delete(value);
26+
}
27+
28+
clear() {
29+
this._frozenArray = null;
30+
return super.clear();
31+
}
32+
2133
/**
2234
* @param {Function} sortFn - function to sort the set
2335
* @returns {void}
@@ -42,4 +54,14 @@ module.exports = class SortableSet extends Set {
4254
sort() {
4355
this.sortWith(this._sortFn);
4456
}
57+
58+
/**
59+
* @returns {any[]} - returns content as frozen array
60+
*/
61+
getFrozenArray() {
62+
if(this._frozenArray === null) {
63+
this._frozenArray = Object.freeze(Array.from(this));
64+
}
65+
return this._frozenArray;
66+
}
4567
};

‎lib/webworker/WebWorkerMainTemplatePlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Template = require("../Template");
99
class WebWorkerMainTemplatePlugin {
1010
apply(mainTemplate) {
1111
mainTemplate.plugin("local-vars", function(source, chunk) {
12-
if(chunk.getChunks().size > 0) {
12+
if(chunk.getNumberOfChunks() > 0) {
1313
return this.asString([
1414
source,
1515
"",

‎test/WebWorkerMainTemplatePlugin.test.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const createMockChunk = (ids, chunks) => {
1111
ids: ids,
1212
_chunks: new Set(chunks),
1313
getChunks() {
14-
return this._chunks;
14+
return Array.from(this._chunks);
15+
},
16+
getNumberOfChunks() {
17+
return this._chunks.size;
1518
}
1619
};
1720
};

0 commit comments

Comments
 (0)