2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-04-04 05:22:23 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-04-19 04:30:18 +08:00
|
|
|
const util = require("util");
|
2017-06-18 12:33:07 +08:00
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
const DependenciesBlock = require("./DependenciesBlock");
|
|
|
|
const ModuleReason = require("./ModuleReason");
|
2017-06-18 12:33:07 +08:00
|
|
|
const SortableSet = require("./util/SortableSet");
|
2017-04-04 05:22:23 +08:00
|
|
|
const Template = require("./Template");
|
|
|
|
|
|
|
|
let debugId = 1000;
|
|
|
|
|
2017-06-19 20:13:44 +08:00
|
|
|
const sortById = (a, b) => {
|
|
|
|
return a.id - b.id;
|
|
|
|
};
|
2017-06-18 12:33:07 +08:00
|
|
|
|
2017-06-19 20:13:44 +08:00
|
|
|
const sortByDebugId = (a, b) => {
|
|
|
|
return a.debugId - b.debugId;
|
|
|
|
};
|
2017-06-18 12:33:07 +08:00
|
|
|
|
2017-11-06 18:26:16 +08:00
|
|
|
const getFrozenArray = set => Object.freeze(Array.from(set));
|
2017-11-03 17:17:08 +08:00
|
|
|
|
|
|
|
const getDebugIdent = set => {
|
|
|
|
set.sortWith(sortByDebugId);
|
|
|
|
const chunks = set;
|
|
|
|
const list = [];
|
|
|
|
for(const chunk of chunks) {
|
|
|
|
const debugId = chunk.debugId;
|
|
|
|
|
|
|
|
if(typeof debugId !== "number") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
list.push(debugId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.join(",");
|
|
|
|
};
|
|
|
|
|
2017-06-19 20:13:44 +08:00
|
|
|
class Module extends DependenciesBlock {
|
2017-06-18 12:33:07 +08:00
|
|
|
|
2017-10-30 20:56:57 +08:00
|
|
|
constructor(type) {
|
2017-04-04 05:22:23 +08:00
|
|
|
super();
|
2017-10-30 20:56:57 +08:00
|
|
|
this.type = type;
|
2017-11-06 20:02:35 +08:00
|
|
|
|
|
|
|
// Unique Id
|
|
|
|
this.debugId = debugId++;
|
|
|
|
|
|
|
|
// Info from Factory
|
|
|
|
// TODO refactor: pass as constructor argument
|
2017-04-04 05:22:23 +08:00
|
|
|
this.context = null;
|
2017-11-06 20:02:35 +08:00
|
|
|
|
|
|
|
// Info from Build
|
|
|
|
this.sideEffectFree = false;
|
|
|
|
this.warnings = [];
|
|
|
|
this.errors = [];
|
|
|
|
this.strict = false;
|
|
|
|
this.meta = {};
|
|
|
|
this.exportsArgument = undefined;
|
|
|
|
this.moduleArgument = undefined;
|
|
|
|
this.assets = null;
|
|
|
|
this.fileDependencies = undefined;
|
|
|
|
this.contextDependencies = undefined;
|
|
|
|
|
|
|
|
// Graph (per Compilation)
|
2017-04-04 05:22:23 +08:00
|
|
|
this.reasons = [];
|
2017-11-06 20:02:35 +08:00
|
|
|
this._chunks = new SortableSet(undefined, sortById);
|
|
|
|
|
|
|
|
// Info from Compilation (per Compilation)
|
2017-04-04 05:22:23 +08:00
|
|
|
this.id = null;
|
|
|
|
this.index = null;
|
|
|
|
this.index2 = null;
|
|
|
|
this.depth = null;
|
2017-11-06 20:02:35 +08:00
|
|
|
this.issuer = null;
|
|
|
|
this.profile = undefined;
|
|
|
|
this.prefetched = false;
|
|
|
|
this.built = false;
|
|
|
|
|
|
|
|
// Info from Optimization (per Compilation)
|
2017-04-04 05:22:23 +08:00
|
|
|
this.used = null;
|
|
|
|
this.usedExports = null;
|
|
|
|
this.providedExports = null;
|
2017-05-28 21:25:07 +08:00
|
|
|
this.optimizationBailout = [];
|
2016-07-13 17:03:14 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
disconnect() {
|
|
|
|
this.reasons.length = 0;
|
2017-11-06 20:02:35 +08:00
|
|
|
this._chunks.clear();
|
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
this.id = null;
|
|
|
|
this.index = null;
|
|
|
|
this.index2 = null;
|
|
|
|
this.depth = null;
|
2017-11-06 20:02:35 +08:00
|
|
|
this.issuer = null;
|
|
|
|
this.profile = undefined;
|
|
|
|
this.prefetched = false;
|
|
|
|
this.built = false;
|
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
this.used = null;
|
|
|
|
this.usedExports = null;
|
|
|
|
this.providedExports = null;
|
2017-08-24 05:16:54 +08:00
|
|
|
this.optimizationBailout.length = 0;
|
2017-04-04 05:22:23 +08:00
|
|
|
super.disconnect();
|
2017-02-13 18:26:25 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
unseal() {
|
|
|
|
this.id = null;
|
|
|
|
this.index = null;
|
|
|
|
this.index2 = null;
|
|
|
|
this.depth = null;
|
2017-04-19 04:30:18 +08:00
|
|
|
this._chunks.clear();
|
2017-04-04 05:22:23 +08:00
|
|
|
super.unseal();
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-06-20 20:47:38 +08:00
|
|
|
setChunks(chunks) {
|
|
|
|
this._chunks = new SortableSet(chunks, sortById);
|
|
|
|
}
|
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
addChunk(chunk) {
|
2017-04-19 04:30:18 +08:00
|
|
|
this._chunks.add(chunk);
|
2017-04-04 05:22:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
removeChunk(chunk) {
|
2017-04-19 04:30:18 +08:00
|
|
|
if(this._chunks.delete(chunk)) {
|
2017-04-04 05:22:23 +08:00
|
|
|
chunk.removeModule(this);
|
2014-07-19 20:32:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
2017-04-04 05:22:23 +08:00
|
|
|
return false;
|
2014-07-19 20:32:48 +08:00
|
|
|
}
|
|
|
|
|
2017-04-19 04:30:18 +08:00
|
|
|
isInChunk(chunk) {
|
|
|
|
return this._chunks.has(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
getChunkIdsIdent() {
|
2017-11-06 19:15:23 +08:00
|
|
|
return this._chunks.getFromUnorderedCache(getDebugIdent);
|
2017-04-19 04:30:18 +08:00
|
|
|
}
|
|
|
|
|
2017-11-06 20:02:35 +08:00
|
|
|
get optional() {
|
|
|
|
return this.reasons.length > 0 && this.reasons.every(r => r.dependency.optional);
|
|
|
|
}
|
|
|
|
|
2017-04-19 04:30:18 +08:00
|
|
|
forEachChunk(fn) {
|
|
|
|
this._chunks.forEach(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
mapChunks(fn) {
|
2017-05-20 20:46:21 +08:00
|
|
|
return Array.from(this._chunks, fn);
|
2017-04-19 04:30:18 +08:00
|
|
|
}
|
|
|
|
|
2017-06-15 04:46:26 +08:00
|
|
|
getChunks() {
|
|
|
|
return Array.from(this._chunks);
|
|
|
|
}
|
|
|
|
|
2017-04-19 04:30:18 +08:00
|
|
|
getNumberOfChunks() {
|
|
|
|
return this._chunks.size;
|
|
|
|
}
|
|
|
|
|
2017-09-22 20:13:09 +08:00
|
|
|
get chunksIterable() {
|
|
|
|
return this._chunks;
|
|
|
|
}
|
|
|
|
|
2017-06-15 04:46:26 +08:00
|
|
|
hasEqualsChunks(otherModule) {
|
|
|
|
if(this._chunks.size !== otherModule._chunks.size) return false;
|
2017-06-19 20:13:44 +08:00
|
|
|
this._chunks.sortWith(sortByDebugId);
|
|
|
|
otherModule._chunks.sortWith(sortByDebugId);
|
2017-06-15 04:46:26 +08:00
|
|
|
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-04-04 05:22:23 +08:00
|
|
|
addReason(module, dependency) {
|
|
|
|
this.reasons.push(new ModuleReason(module, dependency));
|
2014-07-19 20:32:48 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-09-08 04:54:38 +08:00
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
hasReasonForChunk(chunk) {
|
2017-06-14 00:58:14 +08:00
|
|
|
for(let i = 0; i < this.reasons.length; i++) {
|
|
|
|
if(this.reasons[i].hasChunk(chunk))
|
2017-04-04 05:22:23 +08:00
|
|
|
return true;
|
2014-09-08 04:54:38 +08:00
|
|
|
}
|
2017-04-04 05:22:23 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-17 06:31:52 +08:00
|
|
|
|
2017-11-06 20:02:35 +08:00
|
|
|
hasReasons() {
|
|
|
|
return this.reasons.length > 0;
|
|
|
|
}
|
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
rewriteChunkInReasons(oldChunk, newChunks) {
|
2017-06-14 00:58:14 +08:00
|
|
|
for(let i = 0; i < this.reasons.length; i++) {
|
|
|
|
this.reasons[i].rewriteChunks(oldChunk, newChunks);
|
|
|
|
}
|
2017-04-04 05:22:23 +08:00
|
|
|
}
|
2016-07-13 17:03:14 +08:00
|
|
|
|
2017-04-04 05:22:23 +08: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;
|
|
|
|
}
|
2016-07-13 17:03:14 +08:00
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
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);
|
|
|
|
}
|
2016-12-05 06:47:19 +08:00
|
|
|
|
2017-04-19 13:57:21 +08:00
|
|
|
sortItems(sortChunks) {
|
2017-04-04 05:22:23 +08:00
|
|
|
super.sortItems();
|
2017-04-19 13:57:21 +08:00
|
|
|
if(sortChunks)
|
2017-06-18 12:33:07 +08:00
|
|
|
this._chunks.sort();
|
2017-11-06 20:02:35 +08:00
|
|
|
this.reasons.sort((a, b) => {
|
|
|
|
if(a.module === b.module) return 0;
|
|
|
|
if(!a.module) return -1;
|
|
|
|
if(!b.module) return 1;
|
|
|
|
return sortById(a.module, b.module);
|
|
|
|
});
|
2017-06-01 20:11:04 +08:00
|
|
|
if(Array.isArray(this.usedExports)) {
|
|
|
|
this.usedExports.sort();
|
|
|
|
}
|
2017-04-04 05:22:23 +08: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-04-19 04:30:18 +08:00
|
|
|
|
|
|
|
Object.defineProperty(Module.prototype, "chunks", {
|
|
|
|
configurable: false,
|
2017-06-06 19:37:56 +08:00
|
|
|
get: util.deprecate(function() {
|
2017-11-06 19:15:23 +08:00
|
|
|
return this._chunks.getFromCache(getFrozenArray);
|
2017-04-19 04:30:18 +08:00
|
|
|
}, "Module.chunks: Use Module.forEachChunk/mapChunks/getNumberOfChunks/isInChunk/addChunk/removeChunk instead"),
|
|
|
|
set() {
|
|
|
|
throw new Error("Readonly. Use Module.addChunk/removeChunk to modify chunks.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-01-31 01:49:25 +08:00
|
|
|
Module.prototype.identifier = null;
|
|
|
|
Module.prototype.readableIdentifier = null;
|
|
|
|
Module.prototype.build = null;
|
|
|
|
Module.prototype.source = null;
|
|
|
|
Module.prototype.size = null;
|
2016-09-14 18:04:42 +08:00
|
|
|
Module.prototype.nameForCondition = null;
|
2017-04-04 05:22:23 +08:00
|
|
|
|
|
|
|
module.exports = Module;
|