webpack/lib/Module.js

627 lines
16 KiB
JavaScript
Raw Normal View History

2013-01-31 01:49:25 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
const DependenciesBlock = require("./DependenciesBlock");
const Template = require("./Template");
2018-07-30 23:08:51 +08:00
const SortableSet = require("./util/SortableSet");
2018-07-30 23:08:51 +08:00
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./Chunk")} Chunk */
2018-07-25 18:38:34 +08:00
/** @typedef {import("./ChunkGroup")} ChunkGroup */
/** @typedef {import("./Compilation")} Compilation */
2018-07-25 18:38:34 +08:00
/** @typedef {import("./Dependency")} Dependency */
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
/** @typedef {import("./ModuleGraph")} ModuleGraph */
2018-07-30 23:08:51 +08:00
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("./WebpackError")} WebpackError */
/** @typedef {import("./util/createHash").Hash} Hash */
2018-04-12 15:18:53 +08:00
/**
* @typedef {Object} SourceContext
* @property {DependencyTemplates} dependencyTemplates the dependency templates
* @property {RuntimeTemplate} runtimeTemplate the runtime template
* @property {ModuleGraph} moduleGraph the module graph
* @property {string=} type the type of source that should be generated
*/
2018-07-20 22:24:35 +08:00
/**
* @typedef {Object} LibIdentOptions
* @property {string} context absolute context path to which lib ident is relative to
*/
/**
* @typedef {Object} KnownBuildMeta
* @property {(boolean | string[])=} providedExports
* @property {("named" | "namespace")=} exportsType
*/
/** @typedef {KnownBuildMeta & Record<string, any>} BuildMeta */
const EMPTY_RESOLVE_OPTIONS = {};
const optimizationBailoutSymbol = Symbol("optimization bailout");
const usedExportsSymbol = Symbol("used exports");
let debugId = 1000;
2017-06-19 20:13:44 +08:00
const sortById = (a, b) => {
return a.id - b.id;
};
2017-06-19 20:13:44 +08:00
const sortByDebugId = (a, b) => {
return a.debugId - b.debugId;
};
const getIndexMap = set => {
set.sort();
const map = new Map();
let idx = 0;
for (const item of set) {
map.set(item, idx++);
}
return map;
};
const getJoinedString = set => {
set.sort();
return Array.from(set).join(",");
};
2018-04-12 15:18:53 +08:00
/** @typedef {(requestShortener: RequestShortener) => string} OptimizationBailoutFunction */
2017-06-19 20:13:44 +08:00
class Module extends DependenciesBlock {
2018-01-31 04:40:44 +08:00
constructor(type, context = null) {
super();
2018-04-12 15:18:53 +08:00
/** @type {string} */
2017-10-30 20:56:57 +08:00
this.type = type;
2018-04-12 15:18:53 +08:00
/** @type {string} */
2018-01-31 04:40:44 +08:00
this.context = context;
// Unique Id
2018-04-12 15:18:53 +08:00
/** @type {number} */
this.debugId = debugId++;
// Hash
2018-04-12 15:18:53 +08:00
/** @type {string} */
this.hash = undefined;
2018-04-12 15:18:53 +08:00
/** @type {string} */
this.renderedHash = undefined;
// Info from Factory
2018-05-15 18:20:17 +08:00
/** @type {TODO} */
this.resolveOptions = EMPTY_RESOLVE_OPTIONS;
2018-04-12 15:18:53 +08:00
/** @type {object} */
this.factoryMeta = {};
// Info from Build
/** @type {WebpackError[]} */
this.warnings = [];
/** @type {WebpackError[]} */
this.errors = [];
/** @type {BuildMeta} */
this.buildMeta = undefined;
2018-04-12 15:18:53 +08:00
/** @type {object} */
this.buildInfo = undefined;
// Graph (per Compilation)
2018-06-06 12:11:24 +08:00
/** @type {SortableSet<Chunk>} */
this._chunks = new SortableSet(undefined, sortById);
// Info from Compilation (per Compilation)
/** @type {number|string} */
this.id = null;
2018-04-12 15:18:53 +08:00
/** @type {number} */
this.index = null;
2018-04-12 15:18:53 +08:00
/** @type {number} */
this.index2 = null;
2018-04-12 15:18:53 +08:00
/** @type {number} */
this.depth = null;
2018-04-12 15:18:53 +08:00
/** @type {Module} */
this.issuer = null;
2018-04-12 15:18:53 +08:00
/** @type {undefined | object} */
this.profile = undefined;
2018-04-12 15:18:53 +08:00
/** @type {boolean} */
this.prefetched = false;
2018-04-12 15:18:53 +08:00
/** @type {boolean} */
this.built = false;
2018-04-12 15:18:53 +08:00
/** @type {boolean} */
2018-03-28 13:49:10 +08:00
this.useSourceMap = false;
}
/**
* @deprecated moved to .buildInfo.exportsArgument
* @returns {string} name of the exports argument
*/
get exportsArgument() {
2018-02-25 09:00:20 +08:00
return (this.buildInfo && this.buildInfo.exportsArgument) || "exports";
}
/**
* @deprecated moved to .buildInfo.moduleArgument
* @returns {string} name of the module argument
*/
get moduleArgument() {
2018-02-25 09:00:20 +08:00
return (this.buildInfo && this.buildInfo.moduleArgument) || "module";
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {(string | OptimizationBailoutFunction)[]} optimization bailouts
*/
getOptimizationBailout(moduleGraph) {
const meta = moduleGraph.getMeta(this);
const list = meta[optimizationBailoutSymbol];
if (list === undefined) {
return (meta[optimizationBailoutSymbol] = []);
}
return list;
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {false | true | SortableSet<string> | null} the used exports
* false: module is not used at all.
* true: the module namespace/object export is used.
* SortableSet<string>: these export names are used.
* empty SortableSet<string>: module is used but no export.
* null: unknown, worst case should be assumed.
*/
getUsedExports(moduleGraph) {
const value = moduleGraph.getMeta(this)[usedExportsSymbol];
return value === undefined ? null : value;
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @param {false | true | SortableSet<string>} usedExports the used exports
* @returns {void}
*/
setUsedExports(moduleGraph, usedExports) {
moduleGraph.getMeta(this)[usedExportsSymbol] = usedExports;
}
2018-07-25 18:38:34 +08:00
/**
* disconnect the module from the graph
* @returns {void}
*/
disconnect() {
this.hash = undefined;
this.renderedHash = undefined;
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;
super.disconnect();
}
2013-01-31 01:49:25 +08:00
2018-07-25 18:38:34 +08:00
/**
* @returns {void}
*/
unseal() {
this.id = null;
this.index = null;
this.index2 = null;
this.depth = null;
2017-04-19 04:30:18 +08:00
this._chunks.clear();
super.unseal();
}
2013-01-31 01:49:25 +08:00
2018-07-25 18:38:34 +08:00
/**
* 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);
}
2018-07-25 18:38:34 +08:00
/**
* @param {Chunk} chunk added chunk
* @returns {boolean} true, if the chunk could be added
*/
addChunk(chunk) {
2018-02-25 09:00:20 +08:00
if (this._chunks.has(chunk)) return false;
2017-04-19 04:30:18 +08:00
this._chunks.add(chunk);
return true;
}
2018-07-25 18:38:34 +08:00
/**
* @param {Chunk} chunk removed chunk
* @returns {boolean} true, if the chunk could be removed
*/
removeChunk(chunk) {
2018-02-25 09:00:20 +08:00
if (this._chunks.delete(chunk)) {
chunk.removeModule(this);
return true;
}
return false;
}
2018-07-25 18:38:34 +08:00
/**
* @param {Chunk} chunk chunk to be tested
* @returns {boolean} true, if the module is in a chunk
*/
2017-04-19 04:30:18 +08:00
isInChunk(chunk) {
return this._chunks.has(chunk);
}
2018-07-25 18:38:34 +08:00
/**
* @returns {boolean} true, if the module is entry of any chunk
*/
isEntryModule() {
2018-02-25 09:00:20 +08:00
for (const chunk of this._chunks) {
if (chunk.entryModule === this) return true;
}
return false;
}
2018-07-25 18:38:34 +08:00
/**
* @param {ModuleGraph} moduleGraph the module graph
2018-07-25 18:38:34 +08:00
* @returns {boolean} true, if the module is optional
*/
isOptional(moduleGraph) {
const connections = moduleGraph.getIncomingConnections(this);
2018-02-25 09:00:20 +08:00
return (
connections.length > 0 &&
connections.every(r => r.dependency && r.dependency.optional)
2018-02-25 09:00:20 +08:00
);
}
/**
* @returns {Chunk[]} all chunks which contain the module
*/
getChunks() {
return Array.from(this._chunks);
}
2018-07-25 18:38:34 +08:00
/**
* @returns {number} the number of chunk which contain the module
*/
2017-04-19 04:30:18 +08:00
getNumberOfChunks() {
return this._chunks.size;
}
2018-07-25 18:38:34 +08:00
/**
* @returns {Iterable<Chunk>} chunks that contain the module
*/
2017-09-22 20:13:09 +08:00
get chunksIterable() {
return this._chunks;
}
2018-07-25 18:38:34 +08:00
/**
* @param {Module} otherModule some other module
* @returns {boolean} true, if modules are in the same chunks
*/
hasEqualsChunks(otherModule) {
2018-02-25 09:00:20 +08:00
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);
const a = this._chunks[Symbol.iterator]();
const b = otherModule._chunks[Symbol.iterator]();
2018-02-25 09:15:37 +08:00
// eslint-disable-next-line no-constant-condition
2018-02-25 09:00:20 +08:00
while (true) {
const aItem = a.next();
const bItem = b.next();
2018-02-25 09:00:20 +08:00
if (aItem.done) return true;
if (aItem.value !== bItem.value) return false;
}
}
2018-07-25 18:38:34 +08:00
/**
* @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;
}
2018-07-25 18:38:34 +08:00
/**
* @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;
2017-11-25 00:22:58 +08:00
}
2018-07-25 18:38:34 +08:00
/**
* @param {Chunk} chunk a chunk
* @param {ModuleGraph} moduleGraph the module graph
2018-07-25 18:38:34 +08:00
* @returns {boolean} true, if the module has any reason why "chunk" should be included
*/
hasReasonForChunk(chunk, moduleGraph) {
// check for each reason if we need the chunk
for (const connection of moduleGraph.getIncomingConnections(this)) {
const fromModule = connection.originModule;
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;
}
2018-07-25 18:38:34 +08:00
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {boolean} true if at least one other module depends on this module
2018-07-25 18:38:34 +08:00
*/
hasReasons(moduleGraph) {
return moduleGraph.getIncomingConnections(this).length > 0;
}
2018-07-25 18:38:34 +08:00
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {boolean} true, if the module is used
*/
isModuleUsed(moduleGraph) {
return this.getUsedExports(moduleGraph) !== false;
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @param {string} exportName a name of an export
* @returns {boolean} true, if the export is used
*/
isExportUsed(moduleGraph, exportName) {
const usedExports = this.getUsedExports(moduleGraph);
if (usedExports === null || usedExports === true) return true;
if (usedExports === false) return false;
return usedExports.has(exportName);
}
/**
* @param {ModuleGraph} moduleGraph the module graph
* @param {string} exportName a name of an export
* @returns {string | false} false, when module or referenced export is unused.
* string, the mangled export name when used.
2018-07-25 18:38:34 +08:00
*/
getUsedName(moduleGraph, exportName) {
const usedExports = this.getUsedExports(moduleGraph);
if (usedExports === null || usedExports === true) return exportName;
if (usedExports === false) return false;
if (!usedExports.has(exportName)) return false;
// Mangle export name if possible
2018-02-25 09:00:20 +08:00
if (this.isProvided(exportName)) {
if (this.buildMeta.exportsType === "namespace") {
const idx = usedExports
.getFromUnorderedCache(getIndexMap)
.get(exportName);
return Template.numberToIdentifer(idx);
}
if (
2018-02-25 09:00:20 +08:00
this.buildMeta.exportsType === "named" &&
!usedExports.has("default")
) {
const idx = usedExports
.getFromUnorderedCache(getIndexMap)
.get(exportName);
return Template.numberToIdentifer(idx);
}
}
return exportName;
}
2018-07-25 18:38:34 +08:00
/**
* @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) {
2018-02-25 09:00:20 +08:00
if (!Array.isArray(this.buildMeta.providedExports)) return null;
return this.buildMeta.providedExports.includes(exportName);
}
2018-07-25 18:38:34 +08:00
/**
* @returns {string} for debugging
*/
toString() {
return `Module[${this.id || this.debugId}]`;
}
2018-07-25 18:38:34 +08:00
/**
* @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
2018-07-17 22:42:05 +08:00
* @param {Compilation} compilation the compilation
* @returns {void}
*/
2018-07-17 22:42:05 +08:00
updateHash(hash, compilation) {
hash.update(`${this.id}`);
const usedExports = this.getUsedExports(compilation.moduleGraph);
if (typeof usedExports === "boolean") {
hash.update(JSON.stringify(usedExports));
} else if (!usedExports) {
hash.update("null");
} else {
hash.update(usedExports.getFromUnorderedCache(getJoinedString));
}
2018-07-17 22:42:05 +08:00
super.updateHash(hash, compilation);
}
2018-07-25 18:38:34 +08:00
/**
* Sorts items in this module
* @param {boolean=} sortChunks sort the chunks too
* @returns {void}
*/
2017-04-19 13:57:21 +08:00
sortItems(sortChunks) {
super.sortItems();
2018-02-25 09:00:20 +08:00
if (sortChunks) this._chunks.sort();
}
2018-07-25 18:38:34 +08:00
/**
* @returns {void}
*/
unbuild() {
this.dependencies.length = 0;
this.blocks.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 {SourceContext} sourceContext source context
* @returns {Source} generated source
*/
source(sourceContext) {
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");
}
2018-06-06 22:37:23 +08:00
2018-07-20 22:24:35 +08:00
/**
* @param {LibIdentOptions} options options
* @returns {string | null} an identifier for library inclusion
*/
libIdent(options) {
return null;
}
/**
* @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
*/
nameForCondition() {
return null;
}
2018-06-06 22:37:23 +08:00
/**
* @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;
}
}
2018-08-07 03:01:46 +08:00
Object.defineProperty(Module.prototype, "isUsed", {
get() {
throw new Error(
"Module.isUsed was renamed (use getUsedName, isExportUsed or isModuleUsed instead)"
);
}
});
Object.defineProperty(Module.prototype, "used", {
get() {
throw new Error("Module.used was refactored (use getUsedExports instead)");
},
set(value) {
throw new Error("Module.used was refactored (use setUsedExports instead)");
}
});
Object.defineProperty(Module.prototype, "usedExports", {
get() {
throw new Error(
"Module.usedExports was refactored (use getUsedExports instead)"
);
},
set(value) {
throw new Error(
"Module.usedExports was refactored (use setUsedExports instead)"
);
}
});
module.exports = Module;