2017-05-03 15:35:00 +02:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
const util = require("util");
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
const DependenciesBlock = require("./DependenciesBlock");
|
|
|
|
const ModuleReason = require("./ModuleReason");
|
2017-08-14 05:01:11 +02:00
|
|
|
const SortableSet = require("./util/SortableSet");
|
2017-05-03 15:35:00 +02:00
|
|
|
const Template = require("./Template");
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
let debugId = 1000;
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
const sortById = (a, b) => {
|
2017-05-03 15:35:00 +02:00
|
|
|
return a.id - b.id;
|
2017-08-14 05:01:11 +02:00
|
|
|
};
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
const sortByDebugId = (a, b) => {
|
|
|
|
return a.debugId - b.debugId;
|
|
|
|
};
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
class Module extends DependenciesBlock {
|
2017-08-14 05:01:11 +02:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.context = null;
|
|
|
|
this.reasons = [];
|
|
|
|
this.debugId = debugId++;
|
|
|
|
this.id = null;
|
|
|
|
this.portableId = null;
|
|
|
|
this.index = null;
|
|
|
|
this.index2 = null;
|
|
|
|
this.depth = null;
|
|
|
|
this.used = null;
|
|
|
|
this.usedExports = null;
|
|
|
|
this.providedExports = null;
|
2017-08-14 05:01:11 +02:00
|
|
|
this._chunks = new SortableSet(undefined, sortById);
|
|
|
|
this._chunksDebugIdent = undefined;
|
2017-05-03 15:35:00 +02:00
|
|
|
this.warnings = [];
|
|
|
|
this.dependenciesWarnings = [];
|
|
|
|
this.errors = [];
|
|
|
|
this.dependenciesErrors = [];
|
|
|
|
this.strict = false;
|
|
|
|
this.meta = {};
|
2017-08-14 05:01:11 +02:00
|
|
|
this.optimizationBailout = [];
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
|
|
|
this.reasons.length = 0;
|
|
|
|
this.id = null;
|
|
|
|
this.index = null;
|
|
|
|
this.index2 = null;
|
|
|
|
this.depth = null;
|
|
|
|
this.used = null;
|
|
|
|
this.usedExports = null;
|
|
|
|
this.providedExports = null;
|
2017-08-14 05:01:11 +02:00
|
|
|
this._chunks.clear();
|
|
|
|
this._chunksDebugIdent = undefined;
|
2017-10-14 18:40:54 +02:00
|
|
|
this.optimizationBailout.length = 0;
|
2017-05-03 15:35:00 +02:00
|
|
|
super.disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
unseal() {
|
|
|
|
this.id = null;
|
|
|
|
this.index = null;
|
|
|
|
this.index2 = null;
|
|
|
|
this.depth = null;
|
2017-08-14 05:01:11 +02:00
|
|
|
this._chunks.clear();
|
|
|
|
this._chunksDebugIdent = undefined;
|
2017-05-03 15:35:00 +02:00
|
|
|
super.unseal();
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
setChunks(chunks) {
|
|
|
|
this._chunks = new SortableSet(chunks, sortById);
|
2017-10-14 18:40:54 +02:00
|
|
|
this._chunksDebugIdent = undefined;
|
2017-08-14 05:01:11 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
addChunk(chunk) {
|
2017-08-14 05:01:11 +02:00
|
|
|
this._chunks.add(chunk);
|
|
|
|
this._chunksDebugIdent = undefined;
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
removeChunk(chunk) {
|
2017-08-14 05:01:11 +02:00
|
|
|
if(this._chunks.delete(chunk)) {
|
|
|
|
this._chunksDebugIdent = undefined;
|
2017-05-03 15:35:00 +02:00
|
|
|
chunk.removeModule(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
isInChunk(chunk) {
|
|
|
|
return this._chunks.has(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
getChunkIdsIdent() {
|
|
|
|
if(this._chunksDebugIdent !== undefined) return this._chunksDebugIdent;
|
|
|
|
this._chunks.sortWith(sortByDebugId);
|
|
|
|
const chunks = this._chunks;
|
|
|
|
const list = [];
|
|
|
|
for(const chunk of chunks) {
|
|
|
|
const debugId = chunk.debugId;
|
|
|
|
|
|
|
|
if(typeof debugId !== "number") {
|
|
|
|
return this._chunksDebugIdent = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
list.push(debugId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._chunksDebugIdent = list.join(",");
|
|
|
|
}
|
|
|
|
|
|
|
|
forEachChunk(fn) {
|
|
|
|
this._chunks.forEach(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
mapChunks(fn) {
|
|
|
|
return Array.from(this._chunks, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
getChunks() {
|
|
|
|
return Array.from(this._chunks);
|
|
|
|
}
|
|
|
|
|
|
|
|
getNumberOfChunks() {
|
|
|
|
return this._chunks.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
hasEqualsChunks(otherModule) {
|
|
|
|
if(this._chunks.size !== otherModule._chunks.size) return false;
|
|
|
|
this._chunks.sortWith(sortByDebugId);
|
|
|
|
otherModule._chunks.sortWith(sortByDebugId);
|
|
|
|
const a = this._chunks[Symbol.iterator]();
|
|
|
|
const b = otherModule._chunks[Symbol.iterator]();
|
|
|
|
while(true) { // eslint-disable-line
|
|
|
|
const aItem = a.next();
|
|
|
|
const bItem = b.next();
|
|
|
|
if(aItem.done) return true;
|
|
|
|
if(aItem.value !== bItem.value) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
addReason(module, dependency) {
|
|
|
|
this.reasons.push(new ModuleReason(module, dependency));
|
|
|
|
}
|
|
|
|
|
|
|
|
removeReason(module, dependency) {
|
|
|
|
for(let i = 0; i < this.reasons.length; i++) {
|
|
|
|
let r = this.reasons[i];
|
|
|
|
if(r.module === module && r.dependency === dependency) {
|
|
|
|
this.reasons.splice(i, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hasReasonForChunk(chunk) {
|
2017-08-14 05:01:11 +02:00
|
|
|
for(let i = 0; i < this.reasons.length; i++) {
|
|
|
|
if(this.reasons[i].hasChunk(chunk))
|
2017-05-03 15:35:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rewriteChunkInReasons(oldChunk, newChunks) {
|
2017-08-14 05:01:11 +02:00
|
|
|
for(let i = 0; i < this.reasons.length; i++) {
|
|
|
|
this.reasons[i].rewriteChunks(oldChunk, newChunks);
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isUsed(exportName) {
|
|
|
|
if(this.used === null) return exportName;
|
|
|
|
if(!exportName) return !!this.used;
|
|
|
|
if(!this.used) return false;
|
|
|
|
if(!this.usedExports) return false;
|
|
|
|
if(this.usedExports === true) return exportName;
|
|
|
|
let idx = this.usedExports.indexOf(exportName);
|
|
|
|
if(idx < 0) return false;
|
|
|
|
if(this.isProvided(exportName))
|
|
|
|
return Template.numberToIdentifer(idx);
|
|
|
|
return exportName;
|
|
|
|
}
|
|
|
|
|
|
|
|
isProvided(exportName) {
|
|
|
|
if(!Array.isArray(this.providedExports))
|
|
|
|
return null;
|
|
|
|
return this.providedExports.indexOf(exportName) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return `Module[${this.id || this.debugId}]`;
|
|
|
|
}
|
|
|
|
|
|
|
|
needRebuild(fileTimestamps, contextTimestamps) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateHash(hash) {
|
|
|
|
hash.update(this.id + "" + this.used);
|
|
|
|
hash.update(JSON.stringify(this.usedExports));
|
|
|
|
super.updateHash(hash);
|
|
|
|
}
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
sortItems(sortChunks) {
|
2017-05-03 15:35:00 +02:00
|
|
|
super.sortItems();
|
2017-08-14 05:01:11 +02:00
|
|
|
if(sortChunks)
|
|
|
|
this._chunks.sort();
|
|
|
|
this.reasons.sort((a, b) => sortById(a.module, b.module));
|
|
|
|
if(Array.isArray(this.usedExports)) {
|
|
|
|
this.usedExports.sort();
|
|
|
|
}
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unbuild() {
|
|
|
|
this.disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperty(Module.prototype, "entry", {
|
|
|
|
configurable: false,
|
|
|
|
get() {
|
|
|
|
throw new Error("Module.entry was removed. Use Chunk.entryModule");
|
|
|
|
},
|
|
|
|
set() {
|
|
|
|
throw new Error("Module.entry was removed. Use Chunk.entryModule");
|
|
|
|
}
|
|
|
|
});
|
2017-08-14 05:01:11 +02:00
|
|
|
|
|
|
|
Object.defineProperty(Module.prototype, "chunks", {
|
|
|
|
configurable: false,
|
|
|
|
get: util.deprecate(function() {
|
|
|
|
return Array.from(this._chunks);
|
|
|
|
}, "Module.chunks: Use Module.forEachChunk/mapChunks/getNumberOfChunks/isInChunk/addChunk/removeChunk instead"),
|
|
|
|
set() {
|
|
|
|
throw new Error("Readonly. Use Module.addChunk/removeChunk to modify chunks.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
Module.prototype.identifier = null;
|
|
|
|
Module.prototype.readableIdentifier = null;
|
|
|
|
Module.prototype.build = null;
|
|
|
|
Module.prototype.source = null;
|
|
|
|
Module.prototype.size = null;
|
|
|
|
Module.prototype.nameForCondition = null;
|
|
|
|
|
|
|
|
module.exports = Module;
|