webpack/lib/Module.js

532 lines
14 KiB
JavaScript

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const DependenciesBlock = require("./DependenciesBlock");
const ModuleReason = require("./ModuleReason");
const Template = require("./Template");
const SortableSet = require("./util/SortableSet");
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./Chunk")} Chunk */
/** @typedef {import("./ChunkGroup")} ChunkGroup */
/** @typedef {import("./Compilation")} Compilation */
/** @typedef {import("./Dependency")} Dependency */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("./WebpackError")} WebpackError */
/** @typedef {import("./util/createHash").Hash} Hash */
const EMPTY_RESOLVE_OPTIONS = {};
let debugId = 1000;
const sortById = (a, b) => {
return a.id - b.id;
};
const sortByDebugId = (a, b) => {
return a.debugId - b.debugId;
};
/** @typedef {(requestShortener: RequestShortener) => string} OptimizationBailoutFunction */
class Module extends DependenciesBlock {
constructor(type, context = null) {
super();
/** @type {string} */
this.type = type;
/** @type {string} */
this.context = context;
// Unique Id
/** @type {number} */
this.debugId = debugId++;
// Hash
/** @type {string} */
this.hash = undefined;
/** @type {string} */
this.renderedHash = undefined;
// Info from Factory
/** @type {TODO} */
this.resolveOptions = EMPTY_RESOLVE_OPTIONS;
/** @type {object} */
this.factoryMeta = {};
// Info from Build
/** @type {WebpackError[]} */
this.warnings = [];
/** @type {WebpackError[]} */
this.errors = [];
/** @type {object} */
this.buildMeta = undefined;
/** @type {object} */
this.buildInfo = undefined;
// Graph (per Compilation)
/** @type {ModuleReason[]} */
this.reasons = [];
/** @type {SortableSet<Chunk>} */
this._chunks = new SortableSet(undefined, sortById);
// Info from Compilation (per Compilation)
/** @type {number|string} */
this.id = null;
/** @type {number} */
this.index = null;
/** @type {number} */
this.index2 = null;
/** @type {number} */
this.depth = null;
/** @type {Module} */
this.issuer = null;
/** @type {undefined | object} */
this.profile = undefined;
/** @type {boolean} */
this.prefetched = false;
/** @type {boolean} */
this.built = false;
// Info from Optimization (per Compilation)
/** @type {null | boolean} */
this.used = null;
/** @type {false | true | string[]} */
this.usedExports = null;
/** @type {(string | OptimizationBailoutFunction)[]} */
this.optimizationBailout = [];
/** @type {boolean} */
this.useSourceMap = false;
}
/**
* @deprecated moved to .buildInfo.exportsArgument
* @returns {string} name of the exports argument
*/
get exportsArgument() {
return (this.buildInfo && this.buildInfo.exportsArgument) || "exports";
}
/**
* @deprecated moved to .buildInfo.moduleArgument
* @returns {string} name of the module argument
*/
get moduleArgument() {
return (this.buildInfo && this.buildInfo.moduleArgument) || "module";
}
/**
* disconnect the module from the graph
* @returns {void}
*/
disconnect() {
this.hash = undefined;
this.renderedHash = undefined;
this.reasons.length = 0;
this._chunks.clear();
this.id = null;
this.index = null;
this.index2 = null;
this.depth = null;
this.issuer = null;
this.profile = undefined;
this.prefetched = false;
this.built = false;
this.used = null;
this.usedExports = null;
this.optimizationBailout.length = 0;
super.disconnect();
}
/**
* @returns {void}
*/
unseal() {
this.id = null;
this.index = null;
this.index2 = null;
this.depth = null;
this._chunks.clear();
super.unseal();
}
/**
* Sets the chunks to a new value
* @protected
* @param {Iterable<Chunk>} chunks the new chunks
* @returns {void}
*/
setChunks(chunks) {
this._chunks = new SortableSet(chunks, sortById);
}
/**
* @param {Chunk} chunk added chunk
* @returns {boolean} true, if the chunk could be added
*/
addChunk(chunk) {
if (this._chunks.has(chunk)) return false;
this._chunks.add(chunk);
return true;
}
/**
* @param {Chunk} chunk removed chunk
* @returns {boolean} true, if the chunk could be removed
*/
removeChunk(chunk) {
if (this._chunks.delete(chunk)) {
chunk.removeModule(this);
return true;
}
return false;
}
/**
* @param {Chunk} chunk chunk to be tested
* @returns {boolean} true, if the module is in a chunk
*/
isInChunk(chunk) {
return this._chunks.has(chunk);
}
/**
* @returns {boolean} true, if the module is entry of any chunk
*/
isEntryModule() {
for (const chunk of this._chunks) {
if (chunk.entryModule === this) return true;
}
return false;
}
/**
* @returns {boolean} true, if the module is optional
*/
get optional() {
return (
this.reasons.length > 0 &&
this.reasons.every(r => r.dependency && r.dependency.optional)
);
}
/**
* @returns {Chunk[]} all chunks which contain the module
*/
getChunks() {
return Array.from(this._chunks);
}
/**
* @returns {number} the number of chunk which contain the module
*/
getNumberOfChunks() {
return this._chunks.size;
}
/**
* @returns {Iterable<Chunk>} chunks that contain the module
*/
get chunksIterable() {
return this._chunks;
}
/**
* @param {Module} otherModule some other module
* @returns {boolean} true, if modules are in the same chunks
*/
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]();
// eslint-disable-next-line no-constant-condition
while (true) {
const aItem = a.next();
const bItem = b.next();
if (aItem.done) return true;
if (aItem.value !== bItem.value) return false;
}
}
/**
* @param {Module=} module referenced module
* @param {Dependency=} dependency referencing dependency
* @param {string=} explanation some explanation
* @returns {void}
*/
addReason(module, dependency, explanation) {
this.reasons.push(new ModuleReason(module, dependency, explanation));
}
/**
* @param {Module=} module referenced module
* @param {Dependency=} dependency referencing dependency
* @returns {boolean} true, if the reason could be removed
*/
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;
}
/**
* @param {Chunk} chunk a chunk
* @param {Chunk=} ignoreChunk chunk to be ignored
* @returns {boolean} true, if the module is accessible from "chunk" when ignoring "ignoreChunk"
*/
isAccessibleInChunk(chunk, ignoreChunk) {
// Check if module is accessible in ALL chunk groups
for (const chunkGroup of chunk.groupsIterable) {
if (!this.isAccessibleInChunkGroup(chunkGroup)) return false;
}
return true;
}
/**
* @param {ChunkGroup} chunkGroup a chunk group
* @param {Chunk=} ignoreChunk chunk to be ignored
* @returns {boolean} true, if the module is accessible from "chunkGroup" when ignoring "ignoreChunk"
*/
isAccessibleInChunkGroup(chunkGroup, ignoreChunk) {
const queue = new Set([chunkGroup]);
// Check if module is accessible from all items of the queue
queueFor: for (const cg of queue) {
// 1. If module is in one of the chunks of the group we can continue checking the next items
// because it's accessible.
for (const chunk of cg.chunks) {
if (chunk !== ignoreChunk && chunk.containsModule(this))
continue queueFor;
}
// 2. If the chunk group is initial, we can break here because it's not accessible.
if (chunkGroup.isInitial()) return false;
// 3. Enqueue all parents because it must be accessible from ALL parents
for (const parent of chunkGroup.parentsIterable) queue.add(parent);
}
// When we processed through the whole list and we didn't bailout, the module is accessible
return true;
}
/**
* @param {Chunk} chunk a chunk
* @returns {boolean} true, if the module has any reason why "chunk" should be included
*/
hasReasonForChunk(chunk) {
// check for each reason if we need the chunk
for (const reason of this.reasons) {
const fromModule = reason.module;
for (const originChunk of fromModule.chunksIterable) {
// return true if module this is not reachable from originChunk when ignoring cunk
if (!this.isAccessibleInChunk(originChunk, chunk)) return true;
}
}
return false;
}
/**
* @returns {boolean} true, if there are references to this module
*/
hasReasons() {
return this.reasons.length > 0;
}
/**
* @param {string=} exportName a name of an export
* @returns {string | boolean} true, when no "exportName" is provided and the module is used.
* false, when module or referenced export is unused.
* string, the mangled export name when used.
*/
isUsed(exportName) {
if (!exportName) return this.used !== false;
if (this.used === null || this.usedExports === null) return exportName;
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;
// Mangle export name if possible
if (this.isProvided(exportName)) {
if (this.buildMeta.exportsType === "namespace") {
return Template.numberToIdentifer(idx);
}
if (
this.buildMeta.exportsType === "named" &&
!this.usedExports.includes("default")
) {
return Template.numberToIdentifer(idx);
}
}
return exportName;
}
/**
* @param {string} exportName a name of an export
* @returns {boolean | null} true, if the export is provided why the module.
* null, if it's unknown.
* false, if it's not provided.
*/
isProvided(exportName) {
if (!Array.isArray(this.buildMeta.providedExports)) return null;
return this.buildMeta.providedExports.includes(exportName);
}
/**
* @returns {string} for debugging
*/
toString() {
return `Module[${this.id || this.debugId}]`;
}
/**
* @param {TODO} fileTimestamps timestamps of files
* @param {TODO} contextTimestamps timestamps of directories
* @returns {boolean} true, if the module needs a rebuild
*/
needRebuild(fileTimestamps, contextTimestamps) {
return true;
}
/**
* @param {Hash} hash the hash used to track dependencies
* @returns {void}
*/
updateHash(hash) {
hash.update(`${this.id}`);
hash.update(JSON.stringify(this.usedExports));
super.updateHash(hash);
}
/**
* Sorts items in this module
* @param {boolean=} sortChunks sort the chunks too
* @returns {void}
*/
sortItems(sortChunks) {
super.sortItems();
if (sortChunks) this._chunks.sort();
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);
});
if (Array.isArray(this.usedExports)) {
this.usedExports.sort();
}
}
/**
* @returns {void}
*/
unbuild() {
this.dependencies.length = 0;
this.blocks.length = 0;
this.variables.length = 0;
this.buildMeta = undefined;
this.buildInfo = undefined;
this.disconnect();
}
/**
* @abstract
* @returns {string} a unique identifier of the module
*/
identifier() {
throw new Error("Module.identifier: Must be overriden");
}
/**
* @abstract
* @param {RequestShortener} requestShortener the request shortener
* @returns {string} a user readable identifier of the module
*/
readableIdentifier(requestShortener) {
throw new Error("Module.readableIdentifier: Must be overriden");
}
/**
* @abstract
* @param {TODO} options TODO
* @param {Compilation} compilation the compilation
* @param {TODO} resolver TODO
* @param {TODO} fs the file system
* @param {function(Error=): void} callback callback function
* @returns {void}
*/
build(options, compilation, resolver, fs, callback) {
throw new Error("Module.build: Must be overriden");
}
/**
* @abstract
* @param {DependencyTemplates} dependencyTemplates the dependency templates
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @param {string=} type the type of source that should be returned
* @returns {Source} generated source
*/
source(dependencyTemplates, runtimeTemplate, type) {
throw new Error("Module.source: Must be overriden");
}
/**
* @abstract
* @returns {number} the estimated size of the module
*/
size() {
throw new Error("Module.size: Must be overriden");
}
/**
* @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
*/
nameForCondition() {
return null;
}
/**
* @param {Chunk} chunk the chunk which condition should be checked
* @returns {boolean} true, if the chunk is ok for the module
*/
chunkCondition(chunk) {
return true;
}
/**
* Assuming this module is in the cache. Update the (cached) module with
* the fresh module from the factory. Usually updates internal references
* and properties.
* @param {Module} module fresh module
* @returns {void}
*/
updateCacheModule(module) {
// do nothing
// this method can be overriden
}
/**
* @returns {Source | null} the original source for the module before webpack transformation
*/
originalSource() {
return null;
}
}
module.exports = Module;