aboutsummaryrefslogtreecommitdiff
path: root/node_modules/webpack/lib/Chunk.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/webpack/lib/Chunk.js')
-rw-r--r--node_modules/webpack/lib/Chunk.js120
1 files changed, 100 insertions, 20 deletions
diff --git a/node_modules/webpack/lib/Chunk.js b/node_modules/webpack/lib/Chunk.js
index 6962fd6c7..8e7cef13c 100644
--- a/node_modules/webpack/lib/Chunk.js
+++ b/node_modules/webpack/lib/Chunk.js
@@ -4,15 +4,23 @@
*/
"use strict";
+const util = require("util");
const compareLocations = require("./compareLocations");
+const SortableSet = require("./util/SortableSet");
let debugId = 1000;
-const byId = (a, b) => {
+const sortById = (a, b) => {
if(a.id < b.id) return -1;
if(b.id < a.id) return 1;
return 0;
};
+const sortByIdentifier = (a, b) => {
+ if(a.identifier() > b.identifier()) return 1;
+ if(a.identifier() < b.identifier()) return -1;
+ return 0;
+};
+
class Chunk {
constructor(name, module, loc) {
@@ -20,7 +28,7 @@ class Chunk {
this.ids = null;
this.debugId = debugId++;
this.name = name;
- this.modules = [];
+ this._modules = new SortableSet(undefined, sortByIdentifier);
this.entrypoints = [];
this.chunks = [];
this.parents = [];
@@ -88,7 +96,11 @@ class Chunk {
}
addModule(module) {
- return this.addToCollection(this.modules, module);
+ if(!this._modules.has(module)) {
+ this._modules.add(module);
+ return true;
+ }
+ return false;
}
addBlock(block) {
@@ -96,9 +108,7 @@ class Chunk {
}
removeModule(module) {
- const idx = this.modules.indexOf(module);
- if(idx >= 0) {
- this.modules.splice(idx, 1);
+ if(this._modules.delete(module)) {
module.removeChunk(this);
return true;
}
@@ -133,9 +143,65 @@ class Chunk {
});
}
+ setModules(modules) {
+ this._modules = new SortableSet(modules, sortByIdentifier);
+ }
+
+ getNumberOfModules() {
+ return this._modules.size;
+ }
+
+ get modulesIterable() {
+ return this._modules;
+ }
+
+ forEachModule(fn) {
+ this._modules.forEach(fn);
+ }
+
+ mapModules(fn) {
+ return Array.from(this._modules, fn);
+ }
+
+ compareTo(otherChunk) {
+ this._modules.sort();
+ otherChunk._modules.sort();
+ if(this._modules.size > otherChunk._modules.size) return -1;
+ if(this._modules.size < otherChunk._modules.size) return 1;
+ const a = this._modules[Symbol.iterator]();
+ const b = otherChunk._modules[Symbol.iterator]();
+ while(true) { // eslint-disable-line
+ const aItem = a.next();
+ const bItem = b.next();
+ if(aItem.done) return 0;
+ const aModuleIdentifier = aItem.value.identifier();
+ const bModuleIdentifier = bItem.value.identifier();
+ if(aModuleIdentifier > bModuleIdentifier) return -1;
+ if(aModuleIdentifier < bModuleIdentifier) return 1;
+ }
+ }
+
+ containsModule(module) {
+ return this._modules.has(module);
+ }
+
+ getModules() {
+ return Array.from(this._modules);
+ }
+
+ getModulesIdent() {
+ this._modules.sort();
+ let str = "";
+ this._modules.forEach(m => {
+ str += m.identifier() + "#";
+ });
+ return str;
+ }
+
remove(reason) {
// cleanup modules
- this.modules.slice().forEach(module => {
+ // Array.from is used here to create a clone, because removeChunk modifies this._modules
+ Array.from(this._modules).forEach(module => {
module.removeChunk(this);
});
@@ -219,9 +285,10 @@ class Chunk {
return false;
}
- const otherChunkModules = otherChunk.modules.slice();
+ // Array.from is used here to create a clone, because moveModule modifies otherChunk._modules
+ const otherChunkModules = Array.from(otherChunk._modules);
otherChunkModules.forEach(module => otherChunk.moveModule(module, this));
- otherChunk.modules.length = 0;
+ otherChunk._modules.clear();
otherChunk.parents.forEach(parentChunk => parentChunk.replaceChunk(otherChunk, this));
otherChunk.parents.length = 0;
@@ -276,14 +343,14 @@ class Chunk {
}
isEmpty() {
- return this.modules.length === 0;
+ return this._modules.size === 0;
}
updateHash(hash) {
hash.update(`${this.id} `);
hash.update(this.ids ? this.ids.join(",") : "");
hash.update(`${this.name || ""} `);
- this.modules.forEach(m => m.updateHash(hash));
+ this._modules.forEach(m => m.updateHash(hash));
}
canBeIntegrated(otherChunk) {
@@ -307,8 +374,8 @@ class Chunk {
modulesSize() {
let count = 0;
- for(let i = 0; i < this.modules.length; i++) {
- count += this.modules[i].size();
+ for(const module of this._modules) {
+ count += module.size();
}
return count;
}
@@ -325,9 +392,8 @@ class Chunk {
let integratedModulesSize = this.modulesSize();
// only count modules that do not exist in this chunk!
- for(let i = 0; i < otherChunk.modules.length; i++) {
- const otherModule = otherChunk.modules[i];
- if(this.modules.indexOf(otherModule) === -1) {
+ for(const otherModule of otherChunk._modules) {
+ if(!this._modules.has(otherModule)) {
integratedModulesSize += otherModule.size();
}
}
@@ -355,8 +421,12 @@ class Chunk {
};
}
+ sortModules(sortByFn) {
+ this._modules.sortWith(sortByFn || sortById);
+ }
+
sortItems() {
- this.modules.sort(byId);
+ this.sortModules();
this.origins.sort((a, b) => {
const aIdent = a.module.identifier();
const bIdent = b.module.identifier();
@@ -368,12 +438,12 @@ class Chunk {
if(origin.reasons)
origin.reasons.sort();
});
- this.parents.sort(byId);
- this.chunks.sort(byId);
+ this.parents.sort(sortById);
+ this.chunks.sort(sortById);
}
toString() {
- return `Chunk[${this.modules.join()}]`;
+ return `Chunk[${Array.from(this._modules).join()}]`;
}
checkConstraints() {
@@ -393,4 +463,14 @@ class Chunk {
}
}
+Object.defineProperty(Chunk.prototype, "modules", {
+ configurable: false,
+ get: util.deprecate(function() {
+ return Array.from(this._modules);
+ }, "Chunk.modules is deprecated. Use Chunk.getNumberOfModules/mapModules/forEachModule/containsModule instead."),
+ set: util.deprecate(function(value) {
+ this.setModules(value);
+ }, "Chunk.modules is deprecated. Use Chunk.addModule/removeModule instead.")
+});
+
module.exports = Chunk;