Skip to content

Commit 0ec792b

Browse files
authored
Merge pull request webpack#5932 from webpack/refactor/sets
refactor more properties to Sets
2 parents 875074a + 860da9b commit 0ec792b

12 files changed

Lines changed: 148 additions & 121 deletions

‎lib/Chunk.js‎

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Chunk {
5151
this.name = name;
5252
this.entryModule = undefined;
5353
this._modules = new SortableSet(undefined, sortByIdentifier);
54-
this.entrypoints = [];
54+
this._entrypoints = new SortableSet();
5555
this._chunks = new SortableSet(undefined, sortById);
5656
this._parents = new SortableSet(undefined, sortById);
5757
this._blocks = new SortableSet();
@@ -160,13 +160,45 @@ class Chunk {
160160
return this._blocks;
161161
}
162162

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

168200
isInitial() {
169-
return this.entrypoints.length > 0;
201+
return this._entrypoints.size > 0;
170202
}
171203

172204
hasEntryModule() {
@@ -218,6 +250,14 @@ class Chunk {
218250
return false;
219251
}
220252

253+
addEntrypoint(entrypoint) {
254+
if(!this._entrypoints.has(entrypoint)) {
255+
this._entrypoints.add(entrypoint);
256+
return true;
257+
}
258+
return false;
259+
}
260+
221261
removeModule(module) {
222262
if(this._modules.delete(module)) {
223263
module.removeChunk(this);
@@ -437,7 +477,7 @@ class Chunk {
437477
parentChunk.addChunk(newChunk);
438478
newChunk._parents.add(parentChunk);
439479
}
440-
for(const entrypoint of this.entrypoints) {
480+
for(const entrypoint of this._entrypoints) {
441481
entrypoint.insertChunk(newChunk, this);
442482
}
443483
}
@@ -598,4 +638,14 @@ Object.defineProperty(Chunk.prototype, "blocks", {
598638
}, "Chunk.blocks: Use Chunk.addBlock/removeBlock/setBlocks to modify blocks.")
599639
});
600640

641+
Object.defineProperty(Chunk.prototype, "entrypoints", {
642+
configurable: false,
643+
get: util.deprecate(function() {
644+
return this._entrypoints.getFromCache(getFrozenArray);
645+
}, "Chunk.entrypoints: Use Chunk.getEntrypoints() instead"),
646+
set: util.deprecate(function(value) {
647+
this.setBlocks(value);
648+
}, "Chunk.entrypoints: Use Chunk.addEntrypoint/setEntrypoints to modify entrypoints.")
649+
});
650+
601651
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
const byId = (a, b) => {
3031
if(a.id < b.id) return -1;
@@ -47,6 +48,12 @@ const 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();
@@ -1291,55 +1298,38 @@ class Compilation extends Tapable {
12911298
}
12921299

12931300
summarizeDependencies() {
1294-
const filterDups = array => {
1295-
const newArray = [];
1296-
for(let i = 0; i < array.length; i++) {
1297-
if(i === 0 || array[i - 1] !== array[i])
1298-
newArray.push(array[i]);
1299-
}
1300-
return newArray;
1301-
};
1302-
this.fileDependencies = (this.compilationDependencies || []).slice();
1303-
this.contextDependencies = [];
1304-
this.missingDependencies = [];
1301+
this.fileDependencies = new SortableSet(this.compilationDependencies);
1302+
this.contextDependencies = new SortableSet();
1303+
this.missingDependencies = new SortableSet();
13051304

13061305
const children = this.children;
13071306
for(let indexChildren = 0; indexChildren < children.length; indexChildren++) {
13081307
const child = children[indexChildren];
13091308

1310-
this.fileDependencies = this.fileDependencies.concat(child.fileDependencies);
1311-
this.contextDependencies = this.contextDependencies.concat(child.contextDependencies);
1312-
this.missingDependencies = this.missingDependencies.concat(child.missingDependencies);
1309+
addAllToSet(this.fileDependencies, child.fileDependencies);
1310+
addAllToSet(this.contextDependencies, child.contextDependencies);
1311+
addAllToSet(this.missingDependencies, child.missingDependencies);
13131312
}
13141313

13151314
const modules = this.modules;
13161315
for(let indexModule = 0; indexModule < modules.length; indexModule++) {
13171316
const module = modules[indexModule];
13181317

13191318
if(module.fileDependencies) {
1320-
const fileDependencies = module.fileDependencies;
1321-
for(let indexFileDep = 0; indexFileDep < fileDependencies.length; indexFileDep++) {
1322-
this.fileDependencies.push(fileDependencies[indexFileDep]);
1323-
}
1319+
addAllToSet(this.fileDependencies, module.fileDependencies);
13241320
}
13251321
if(module.contextDependencies) {
1326-
const contextDependencies = module.contextDependencies;
1327-
for(let indexContextDep = 0; indexContextDep < contextDependencies.length; indexContextDep++) {
1328-
this.contextDependencies.push(contextDependencies[indexContextDep]);
1329-
}
1322+
addAllToSet(this.contextDependencies, module.contextDependencies);
13301323
}
13311324
}
13321325
this.errors.forEach(error => {
1333-
if(Array.isArray(error.missing)) {
1334-
error.missing.forEach(item => this.missingDependencies.push(item));
1326+
if(typeof error.missing === "object" && error.missing && error.missing[Symbol.iterator]) {
1327+
addAllToSet(this.missingDependencies, error.missing);
13351328
}
13361329
});
13371330
this.fileDependencies.sort();
1338-
this.fileDependencies = filterDups(this.fileDependencies);
13391331
this.contextDependencies.sort();
1340-
this.contextDependencies = filterDups(this.contextDependencies);
13411332
this.missingDependencies.sort();
1342-
this.missingDependencies = filterDups(this.missingDependencies);
13431333
}
13441334

13451335
createHash() {

‎lib/Compiler.js‎

Lines changed: 23 additions & 28 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,29 @@ 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-
}
198-
this.plugin("compilation", (compilation, data) => {
199-
data.normalModuleFactory.plugin("parser", parser => {
200-
parser.plugin(hook, fn);
191+
plugin: util.deprecate(
192+
(hook, fn) => {
193+
this.plugin("compilation", (compilation, data) => {
194+
data.normalModuleFactory.plugin("parser", parser => {
195+
parser.plugin(hook, fn);
196+
});
201197
});
202-
});
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-
}
212-
this.plugin("compilation", (compilation, data) => {
213-
data.normalModuleFactory.plugin("parser", parser => {
214-
parser.apply.apply(parser, args);
198+
},
199+
"webpack: Using compiler.parser is deprecated.\n" +
200+
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.plugin(/* ... */); });\n}); instead. "
201+
),
202+
apply: util.deprecate(
203+
(...args) => {
204+
this.plugin("compilation", (compilation, data) => {
205+
data.normalModuleFactory.plugin("parser", parser => {
206+
parser.apply(...args);
207+
});
215208
});
216-
});
217-
}
209+
},
210+
"webpack: Using compiler.parser is deprecated.\n" +
211+
"Use compiler.plugin(\"compilation\", function(compilation, data) {\n data.normalModuleFactory.plugin(\"parser\", function(parser, options) { parser.apply(/* ... */); });\n}); instead. "
212+
)
218213
};
219214

220215
this.options = {};
@@ -483,7 +478,7 @@ class Compiler extends Tapable {
483478
const params = {
484479
normalModuleFactory: this.createNormalModuleFactory(),
485480
contextModuleFactory: this.createContextModuleFactory(),
486-
compilationDependencies: []
481+
compilationDependencies: new Set()
487482
};
488483
return params;
489484
}

‎lib/ContextModule.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ContextModule extends Module {
2727

2828
// Info from Build
2929
this.builtTime = undefined;
30-
this.contextDependencies = [this.context];
30+
this.contextDependencies = new Set([this.context]);
3131

3232
if(typeof options.mode !== "string")
3333
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, (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
const needChunkLoadingCode = chunk => {
13-
var otherChunksInEntry = chunk.entrypoints.some(entrypoint => entrypoint.chunks.length > 1);
13+
var otherChunksInEntry = chunk.getEntrypoints().some(entrypoint => entrypoint.chunks.length > 1);
1414
var onDemandChunks = chunk.getNumberOfChunks() > 0;
1515
return otherChunksInEntry || onDemandChunks;
1616
};

0 commit comments

Comments
 (0)