Skip to content

Commit 51c572c

Browse files
committed
refactor more properties to Sets
1 parent 49cdb94 commit 51c572c

12 files changed

+139
-104
lines changed

lib/Chunk.js

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Chunk {
5050
this.debugId = debugId++;
5151
this.name = name;
5252
this._modules = new SortableSet(undefined, sortByIdentifier);
53-
this.entrypoints = [];
53+
this._entrypoints = new SortableSet();
5454
this._chunks = new SortableSet(undefined, sortById);
5555
this._parents = new SortableSet(undefined, sortById);
5656
this._blocks = new SortableSet();
@@ -155,13 +155,45 @@ class Chunk {
155155
return this._blocks;
156156
}
157157

158+
/**
159+
* @return {Array} - an array containing the entrypoints
160+
*/
161+
getEntrypoints() {
162+
return this._entrypoints.getFromCache(getArray);
163+
}
164+
165+
setEntrypoints(newEntrypoints) {
166+
this._entrypoints.clear();
167+
for(const p of newEntrypoints)
168+
this._entrypoints.add(p);
169+
}
170+
171+
mapEntrypoints(fn) {
172+
return Array.from(this._entrypoints, fn);
173+
}
174+
175+
getNumberOfEntrypoints() {
176+
return this._entrypoints.size;
177+
}
178+
179+
hasEntrypoint(entrypoint) {
180+
return this._entrypoints.has(entrypoint);
181+
}
182+
183+
get entrypointsIterable() {
184+
return this._entrypoints;
185+
}
186+
158187
hasRuntime() {
159-
if(this.entrypoints.length === 0) return false;
160-
return this.entrypoints[0].getRuntimeChunk() === this;
188+
for(const entrypoint of this._entrypoints) {
189+
// We only need to check the first one
190+
return entrypoint.getRuntimeChunk() === this;
191+
}
192+
return false;
161193
}
162194

163195
isInitial() {
164-
return this.entrypoints.length > 0;
196+
return this._entrypoints.size > 0;
165197
}
166198

167199
hasEntryModule() {
@@ -213,6 +245,14 @@ class Chunk {
213245
return false;
214246
}
215247

248+
addEntrypoint(entrypoint) {
249+
if(!this._entrypoints.has(entrypoint)) {
250+
this._entrypoints.add(entrypoint);
251+
return true;
252+
}
253+
return false;
254+
}
255+
216256
removeModule(module) {
217257
if(this._modules.delete(module)) {
218258
module.removeChunk(this);
@@ -432,7 +472,7 @@ class Chunk {
432472
parentChunk.addChunk(newChunk);
433473
newChunk._parents.add(parentChunk);
434474
}
435-
for(const entrypoint of this.entrypoints) {
475+
for(const entrypoint of this._entrypoints) {
436476
entrypoint.insertChunk(newChunk, this);
437477
}
438478
}
@@ -592,4 +632,14 @@ Object.defineProperty(Chunk.prototype, "blocks", {
592632
}, "Chunk.blocks: Use Chunk.addBlock/removeBlock/setBlocks to modify blocks.")
593633
});
594634

635+
Object.defineProperty(Chunk.prototype, "entrypoints", {
636+
configurable: false,
637+
get: util.deprecate(function() {
638+
return this._entrypoints.getFromCache(getFrozenArray);
639+
}, "Chunk.entrypoints: Use Chunk.getEntrypoints() instead"),
640+
set: util.deprecate(function(value) {
641+
this.setBlocks(value);
642+
}, "Chunk.entrypoints: Use Chunk.addEntrypoint/setEntrypoints to modify entrypoints.")
643+
});
644+
595645
module.exports = Chunk;

lib/Compilation.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const CachedSource = require("webpack-sources").CachedSource;
2525
const Stats = require("./Stats");
2626
const Semaphore = require("./util/Semaphore");
2727
const Queue = require("./util/Queue");
28+
const SortableSet = require("./util/SortableSet");
2829

2930
function byId(a, b) {
3031
if(a.id < b.id) return -1;
@@ -47,6 +48,12 @@ function iterationOfArrayCallback(arr, fn) {
4748
}
4849
}
4950

51+
function addAllToSet(set, otherSet) {
52+
for(const item of otherSet) {
53+
set.add(item);
54+
}
55+
}
56+
5057
class Compilation extends Tapable {
5158
constructor(compiler) {
5259
super();
@@ -1254,55 +1261,38 @@ class Compilation extends Tapable {
12541261
}
12551262

12561263
summarizeDependencies() {
1257-
function filterDups(array) {
1258-
const newArray = [];
1259-
for(let i = 0; i < array.length; i++) {
1260-
if(i === 0 || array[i - 1] !== array[i])
1261-
newArray.push(array[i]);
1262-
}
1263-
return newArray;
1264-
}
1265-
this.fileDependencies = (this.compilationDependencies || []).slice();
1266-
this.contextDependencies = [];
1267-
this.missingDependencies = [];
1264+
this.fileDependencies = new SortableSet(this.compilationDependencies);
1265+
this.contextDependencies = new SortableSet();
1266+
this.missingDependencies = new SortableSet();
12681267

12691268
const children = this.children;
12701269
for(let indexChildren = 0; indexChildren < children.length; indexChildren++) {
12711270
const child = children[indexChildren];
12721271

1273-
this.fileDependencies = this.fileDependencies.concat(child.fileDependencies);
1274-
this.contextDependencies = this.contextDependencies.concat(child.contextDependencies);
1275-
this.missingDependencies = this.missingDependencies.concat(child.missingDependencies);
1272+
addAllToSet(this.fileDependencies, child.fileDependencies);
1273+
addAllToSet(this.contextDependencies, child.contextDependencies);
1274+
addAllToSet(this.missingDependencies, child.missingDependencies);
12761275
}
12771276

12781277
const modules = this.modules;
12791278
for(let indexModule = 0; indexModule < modules.length; indexModule++) {
12801279
const module = modules[indexModule];
12811280

12821281
if(module.fileDependencies) {
1283-
const fileDependencies = module.fileDependencies;
1284-
for(let indexFileDep = 0; indexFileDep < fileDependencies.length; indexFileDep++) {
1285-
this.fileDependencies.push(fileDependencies[indexFileDep]);
1286-
}
1282+
addAllToSet(this.fileDependencies, module.fileDependencies);
12871283
}
12881284
if(module.contextDependencies) {
1289-
const contextDependencies = module.contextDependencies;
1290-
for(let indexContextDep = 0; indexContextDep < contextDependencies.length; indexContextDep++) {
1291-
this.contextDependencies.push(contextDependencies[indexContextDep]);
1292-
}
1285+
addAllToSet(this.contextDependencies, module.contextDependencies);
12931286
}
12941287
}
12951288
this.errors.forEach(error => {
1296-
if(Array.isArray(error.missing)) {
1297-
error.missing.forEach(item => this.missingDependencies.push(item));
1289+
if(typeof error.missing === "object" && error.missing && error.missing[Symbol.iterator]) {
1290+
addAllToSet(this.missingDependencies, error.missing);
12981291
}
12991292
});
13001293
this.fileDependencies.sort();
1301-
this.fileDependencies = filterDups(this.fileDependencies);
13021294
this.contextDependencies.sort();
1303-
this.contextDependencies = filterDups(this.contextDependencies);
13041295
this.missingDependencies.sort();
1305-
this.missingDependencies = filterDups(this.missingDependencies);
13061296
}
13071297

13081298
createHash() {

lib/Compiler.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"use strict";
66

77
const path = require("path");
8+
const util = require("util");
89
const Tapable = require("tapable");
910

1011
const Compilation = require("./Compilation");
@@ -104,7 +105,7 @@ class Watching {
104105
this.compiler.applyPlugins("done", stats);
105106
this.handler(null, stats);
106107
if(!this.closed) {
107-
this.watch(compilation.fileDependencies, compilation.contextDependencies, compilation.missingDependencies);
108+
this.watch(Array.from(compilation.fileDependencies), Array.from(compilation.contextDependencies), Array.from(compilation.missingDependencies));
108109
}
109110
this.callbacks.forEach(cb => cb());
110111
this.callbacks.length = 0;
@@ -186,35 +187,25 @@ class Compiler extends Tapable {
186187
loader: null,
187188
context: null
188189
};
189-
let deprecationReported = false;
190190
this.parser = {
191-
plugin: (hook, fn) => {
192-
if(!deprecationReported) {
193-
console.warn("webpack: Using compiler.parser is deprecated.\n" +
194-
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.plugin(/* ... */); });\n}); instead. " +
195-
"It was called " + new Error().stack.split("\n")[2].trim() + ".");
196-
deprecationReported = true;
197-
}
191+
plugin: util.deprecate((hook, fn) => {
198192
this.plugin("compilation", (compilation, data) => {
199193
data.normalModuleFactory.plugin("parser", parser => {
200194
parser.plugin(hook, fn);
201195
});
202196
});
203-
},
204-
apply: () => {
205-
const args = arguments;
206-
if(!deprecationReported) {
207-
console.warn("webpack: Using compiler.parser is deprecated.\n" +
208-
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.apply(/* ... */); });\n}); instead. " +
209-
"It was called " + new Error().stack.split("\n")[2].trim() + ".");
210-
deprecationReported = true;
211-
}
197+
}, "webpack: Using compiler.parser is deprecated.\n" +
198+
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.plugin(/* ... */); });\n}); instead. "
199+
),
200+
apply: util.deprecate((...args) => {
212201
this.plugin("compilation", (compilation, data) => {
213202
data.normalModuleFactory.plugin("parser", parser => {
214-
parser.apply.apply(parser, args);
203+
parser.apply(...args);
215204
});
216205
});
217-
}
206+
}, "webpack: Using compiler.parser is deprecated.\n" +
207+
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.apply(/* ... */); });\n}); instead. "
208+
)
218209
};
219210

220211
this.options = {};
@@ -486,7 +477,7 @@ class Compiler extends Tapable {
486477
const params = {
487478
normalModuleFactory: this.createNormalModuleFactory(),
488479
contextModuleFactory: this.createContextModuleFactory(),
489-
compilationDependencies: []
480+
compilationDependencies: new Set()
490481
};
491482
return params;
492483
}

lib/ContextModule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ContextModule extends Module {
2323
this.options = options;
2424
this.cacheable = true;
2525
this.context = options.resource;
26-
this.contextDependencies = [this.context];
26+
this.contextDependencies = new Set([this.context]);
2727
this.built = false;
2828
if(typeof options.mode !== "string")
2929
throw new Error("options.mode is a required option");

lib/DllReferencePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class DllReferencePlugin {
2525
compiler.plugin("before-compile", (params, callback) => {
2626
const manifest = this.options.manifest;
2727
if(typeof manifest === "string") {
28-
params.compilationDependencies.push(manifest);
28+
params.compilationDependencies.add(manifest);
2929
compiler.inputFileSystem.readFile(manifest, function(err, result) {
3030
if(err) return callback(err);
3131
params["dll reference " + manifest] = JSON.parse(result.toString("utf-8"));

lib/Entrypoint.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ class Entrypoint {
1111
}
1212

1313
unshiftChunk(chunk) {
14-
this.chunks.unshift(chunk);
15-
chunk.entrypoints.push(this);
14+
const oldIdx = this.chunks.indexOf(chunk);
15+
if(oldIdx > 0) {
16+
this.chunks.splice(oldIdx, 1);
17+
this.chunks.unshift(chunk);
18+
} else if(oldIdx < 0 && chunk.addEntrypoint(this)) {
19+
this.chunks.unshift(chunk);
20+
}
1621
}
1722

1823
insertChunk(chunk, before) {
@@ -26,22 +31,20 @@ class Entrypoint {
2631
this.chunks.splice(idx, 0, chunk);
2732
} else if(oldIdx < 0) {
2833
this.chunks.splice(idx, 0, chunk);
29-
chunk.entrypoints.push(this);
34+
chunk.addEntrypoint(this);
3035
}
3136
}
3237

3338
getFiles() {
34-
const files = [];
39+
const files = new Set();
3540

3641
for(let chunkIdx = 0; chunkIdx < this.chunks.length; chunkIdx++) {
3742
for(let fileIdx = 0; fileIdx < this.chunks[chunkIdx].files.length; fileIdx++) {
38-
if(files.indexOf(this.chunks[chunkIdx].files[fileIdx]) === -1) {
39-
files.push(this.chunks[chunkIdx].files[fileIdx]);
40-
}
43+
files.add(this.chunks[chunkIdx].files[fileIdx]);
4144
}
4245
}
4346

44-
return files;
47+
return Array.from(files);
4548
}
4649

4750
getRuntimeChunk() {

lib/JsonpChunkTemplatePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class JsonpChunkTemplatePlugin {
1515
source.add(modules);
1616
const entries = [chunk.entryModule]
1717
.filter(Boolean)
18-
.map(m => [m.id].concat(chunk.entrypoints[0].chunks.map(c => c.id)));
18+
.map(m => [m.id].concat(chunk.getEntrypoints()[0].chunks.map(c => c.id)));
1919
if(entries.length > 0) {
2020
source.add(`,${JSON.stringify(entries)}`);
2121
}

lib/JsonpMainTemplatePlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class JsonpMainTemplatePlugin {
1010

1111
apply(mainTemplate) {
1212
function needChunkLoadingCode(chunk) {
13-
var otherChunksInEntry = chunk.entrypoints.some(function(entrypoint) {
13+
var otherChunksInEntry = chunk.getEntrypoints().some(function(entrypoint) {
1414
return entrypoint.chunks.length > 1;
1515
});
1616
var onDemandChunks = chunk.getNumberOfChunks() > 0;

0 commit comments

Comments
 (0)