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-04-04 05:22:23 +08:00
|
|
|
const DependenciesBlock = require("./DependenciesBlock");
|
|
|
|
const ModuleReason = require("./ModuleReason");
|
|
|
|
const Template = require("./Template");
|
|
|
|
|
|
|
|
function addToSet(set, items) {
|
|
|
|
for(let item of items) {
|
|
|
|
if(set.indexOf(item) < 0)
|
|
|
|
set.push(item);
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
function byId(a, b) {
|
|
|
|
return a.id - b.id;
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
let debugId = 1000;
|
|
|
|
|
|
|
|
class Module extends DependenciesBlock {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.context = null;
|
|
|
|
this.reasons = [];
|
|
|
|
this.debugId = debugId++;
|
|
|
|
this.lastId = -1;
|
|
|
|
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-04-19 04:30:18 +08:00
|
|
|
this._chunks = new Set();
|
|
|
|
this._chunksIsSorted = true;
|
|
|
|
this._chunksDebugIdent = undefined;
|
2017-04-04 05:22:23 +08:00
|
|
|
this.warnings = [];
|
|
|
|
this.dependenciesWarnings = [];
|
|
|
|
this.errors = [];
|
|
|
|
this.dependenciesErrors = [];
|
|
|
|
this.strict = false;
|
|
|
|
this.meta = {};
|
2016-07-13 17:03:14 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
disconnect() {
|
|
|
|
this.reasons.length = 0;
|
|
|
|
this.lastId = this.id;
|
|
|
|
this.id = null;
|
|
|
|
this.index = null;
|
|
|
|
this.index2 = null;
|
|
|
|
this.depth = null;
|
|
|
|
this.used = null;
|
|
|
|
this.usedExports = null;
|
|
|
|
this.providedExports = null;
|
2017-04-19 04:30:18 +08:00
|
|
|
this._chunks.clear();
|
|
|
|
this._chunksDebugIdent = undefined;
|
|
|
|
this._chunksIsSorted = false;
|
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.lastId = this.id;
|
|
|
|
this.id = null;
|
|
|
|
this.index = null;
|
|
|
|
this.index2 = null;
|
|
|
|
this.depth = null;
|
2017-04-19 04:30:18 +08:00
|
|
|
this._chunks.clear();
|
|
|
|
this._chunksDebugIdent = undefined;
|
|
|
|
this._chunksIsSorted = false;
|
2017-04-04 05:22:23 +08:00
|
|
|
super.unseal();
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2017-04-04 05:22:23 +08:00
|
|
|
addChunk(chunk) {
|
2017-04-19 04:30:18 +08:00
|
|
|
this._chunks.add(chunk);
|
|
|
|
this._chunksDebugIdent = undefined;
|
|
|
|
this._chunksIsSorted = false;
|
2017-04-04 05:22:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
removeChunk(chunk) {
|
2017-04-19 04:30:18 +08:00
|
|
|
if(this._chunks.delete(chunk)) {
|
|
|
|
this._chunksDebugIdent = undefined;
|
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() {
|
|
|
|
if(this._chunksDebugIdent !== undefined) return this._chunksDebugIdent;
|
|
|
|
this._ensureChunksSorted();
|
|
|
|
const chunks = this._chunks;
|
|
|
|
const list = [];
|
|
|
|
for(let 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) {
|
|
|
|
const chunks = this._chunks;
|
|
|
|
const size = chunks.size;
|
|
|
|
const array = new Array(size);
|
|
|
|
let idx = 0;
|
|
|
|
for(let chunk of chunks) {
|
|
|
|
array[idx++] = fn(chunk, idx, chunks);
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
getNumberOfChunks() {
|
|
|
|
return this._chunks.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ensureChunksSorted() {
|
|
|
|
if(this._chunksIsSorted) return;
|
|
|
|
this._chunks = new Set(Array.from(this._chunks).sort(byId));
|
|
|
|
this._chunksIsSorted = true;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
for(let r of this.reasons) {
|
|
|
|
if(r.chunks) {
|
|
|
|
if(r.chunks.indexOf(chunk) >= 0)
|
|
|
|
return true;
|
2017-04-19 04:30:18 +08:00
|
|
|
} else if(r.module._chunks.has(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-04-04 05:22:23 +08:00
|
|
|
rewriteChunkInReasons(oldChunk, newChunks) {
|
|
|
|
this.reasons.forEach(r => {
|
|
|
|
if(!r.chunks) {
|
2017-04-19 04:30:18 +08:00
|
|
|
if(!r.module._chunks.has(oldChunk))
|
2017-04-04 05:22:23 +08:00
|
|
|
return;
|
2017-04-19 04:30:18 +08:00
|
|
|
r.chunks = Array.from(r.module._chunks);
|
2017-04-04 05:22:23 +08:00
|
|
|
}
|
|
|
|
r.chunks = r.chunks.reduce((arr, c) => {
|
|
|
|
addToSet(arr, c !== oldChunk ? [c] : newChunks);
|
|
|
|
return arr;
|
|
|
|
}, []);
|
|
|
|
});
|
|
|
|
}
|
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-04 05:22:23 +08:00
|
|
|
sortItems() {
|
|
|
|
super.sortItems();
|
2017-04-19 04:30:18 +08:00
|
|
|
this._ensureChunksSorted();
|
2017-04-04 05:22:23 +08:00
|
|
|
this.reasons.sort((a, b) => byId(a.module, b.module));
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
get: util.deprecate(() => {
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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;
|