webpack/lib/Dependency.js

252 lines
6.9 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";
2018-04-04 15:17:10 +08:00
const memoize = require("./util/memoize");
2018-05-15 18:20:17 +08:00
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./ChunkGraph")} ChunkGraph */
2018-07-11 19:05:13 +08:00
/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
2018-07-30 23:08:51 +08:00
/** @typedef {import("./Module")} Module */
2018-07-17 22:42:05 +08:00
/** @typedef {import("./ModuleGraph")} ModuleGraph */
/** @typedef {import("./ModuleGraphConnection")} ModuleGraphConnection */
/** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
2018-07-30 23:08:51 +08:00
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
2018-07-25 15:33:48 +08:00
/** @typedef {import("./WebpackError")} WebpackError */
2019-07-17 22:02:33 +08:00
/** @typedef {import("./util/Hash")} Hash */
/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
/**
* @typedef {Object} UpdateHashContext
* @property {ChunkGraph} chunkGraph
* @property {RuntimeSpec} runtime
* @property {RuntimeTemplate=} runtimeTemplate
*/
2018-05-15 18:20:17 +08:00
2020-03-03 20:37:18 +08:00
/**
* @typedef {Object} SourcePosition
* @property {number} line
* @property {number=} column
2018-06-22 02:58:54 +08:00
*/
2020-03-03 20:37:18 +08:00
/**
* @typedef {Object} RealDependencyLocation
* @property {SourcePosition} start
* @property {SourcePosition=} end
* @property {number=} index
*/
2020-03-03 20:37:18 +08:00
/**
2020-03-13 19:09:14 +08:00
* @typedef {Object} SyntheticDependencyLocation
2020-03-03 20:37:18 +08:00
* @property {string} name
* @property {number=} index
2018-05-15 18:20:17 +08:00
*/
2020-03-13 19:09:14 +08:00
/** @typedef {SyntheticDependencyLocation|RealDependencyLocation} DependencyLocation */
/**
* @typedef {Object} ExportSpec
* @property {string} name the name of the export
2019-06-12 20:00:34 +08:00
* @property {boolean=} canMangle can the export be renamed (defaults to true)
* @property {boolean=} terminalBinding is the export a terminal binding that should be checked for export star conflicts
* @property {(string | ExportSpec)[]=} exports nested exports
* @property {ModuleGraphConnection=} from when reexported: from which module
2019-06-12 20:00:34 +08:00
* @property {string[] | null=} export when reexported: from which export
* @property {boolean=} hidden export is not visible, because another export blends over it
*/
2018-07-25 15:33:48 +08:00
/**
* @typedef {Object} ExportsSpec
* @property {(string | ExportSpec)[] | true | null} exports exported names, true for unknown exports or null for no exports
* @property {Set<string>=} excludeExports when exports = true, list of unaffected exports
* @property {Set<string>=} hideExports list of maybe prior exposed, but now hidden exports
* @property {ModuleGraphConnection=} from when reexported: from which module
* @property {boolean=} canMangle can the export be renamed (defaults to true)
* @property {boolean=} terminalBinding are the exports terminal bindings that should be checked for export star conflicts
2018-07-25 15:33:48 +08:00
* @property {Module[]=} dependencies module on which the result depends on
*/
/**
2020-06-10 19:31:01 +08:00
* @typedef {Object} ReferencedExport
* @property {string[]} name name of the referenced export
* @property {boolean=} canMangle when false, referenced export can not be mangled, defaults to true
*/
const getIgnoredModule = memoize(() => {
const RawModule = require("./RawModule");
return new RawModule("/* (ignored) */", `ignored`, `(ignored)`);
});
class Dependency {
constructor() {
// TODO check if this can be moved into ModuleDependency
/** @type {boolean} */
2017-08-08 15:28:34 +08:00
this.weak = false;
// TODO check if this can be moved into ModuleDependency
/** @type {boolean} */
this.optional = false;
/** @type {DependencyLocation} */
2018-03-28 13:27:05 +08:00
this.loc = undefined;
}
2013-01-31 01:49:25 +08:00
2018-07-25 15:33:48 +08:00
/**
* @returns {string} a display name for the type of dependency
*/
get type() {
return "unknown";
}
2020-05-27 04:56:24 +08:00
/**
* @returns {string} a dependency category, typical categories are "commonjs", "amd", "esm"
2020-05-27 04:56:24 +08:00
*/
get category() {
return "unknown";
2020-05-27 04:56:24 +08:00
}
2018-07-25 15:33:48 +08:00
/**
* @returns {string | null} an identifier to merge equal requests
*/
getResourceIdentifier() {
return null;
}
2013-01-31 01:49:25 +08:00
2018-07-25 15:33:48 +08:00
/**
* Returns the referenced module and export
* @deprecated
* @param {ModuleGraph} moduleGraph module graph
* @returns {never} throws error
2018-07-25 15:33:48 +08:00
*/
getReference(moduleGraph) {
throw new Error(
"Dependency.getReference was removed in favor of Dependency.getReferencedExports, ModuleGraph.getModule and ModuleGraph.getConnection().active"
);
}
/**
* Returns list of exports referenced by this dependency
* @param {ModuleGraph} moduleGraph module graph
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
* @returns {(string[] | ReferencedExport)[]} referenced exports
*/
getReferencedExports(moduleGraph, runtime) {
return Dependency.EXPORTS_OBJECT_REFERENCED;
}
/**
* @param {ModuleGraph} moduleGraph module graph
2021-01-27 20:32:09 +08:00
* @returns {null | false | function(ModuleGraphConnection, RuntimeSpec): ConnectionState} function to determine if the connection is active
*/
getCondition(moduleGraph) {
return null;
}
2018-07-25 15:33:48 +08:00
/**
* Returns the exported names
2018-07-17 22:34:36 +08:00
* @param {ModuleGraph} moduleGraph module graph
2018-07-25 15:33:48 +08:00
* @returns {ExportsSpec | undefined} export names
*/
2018-07-17 22:34:36 +08:00
getExports(moduleGraph) {
return undefined;
}
2018-07-25 15:33:48 +08:00
/**
* Returns warnings
* @param {ModuleGraph} moduleGraph module graph
2018-07-25 15:33:48 +08:00
* @returns {WebpackError[]} warnings
*/
getWarnings(moduleGraph) {
return null;
}
2018-07-25 15:33:48 +08:00
/**
* Returns errors
* @param {ModuleGraph} moduleGraph module graph
2018-07-25 15:33:48 +08:00
* @returns {WebpackError[]} errors
*/
getErrors(moduleGraph) {
return null;
}
2018-07-25 15:33:48 +08:00
/**
* Update the hash
* @param {Hash} hash hash to be updated
* @param {UpdateHashContext} context context
2018-07-25 15:33:48 +08:00
* @returns {void}
*/
updateHash(hash, context) {}
2013-01-31 01:49:25 +08:00
/**
* implement this method to allow the occurrence order plugin to count correctly
* @returns {number} count how often the id is used in this dependency
*/
getNumberOfIdOccurrences() {
return 1;
}
2018-10-09 20:30:59 +08:00
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {ConnectionState} how this dependency connects the module to referencing modules
*/
getModuleEvaluationSideEffectsState(moduleGraph) {
return true;
}
/**
* @param {string} context context directory
* @returns {Module} a module
*/
createIgnoredModule(context) {
return getIgnoredModule();
}
2018-10-09 20:30:59 +08:00
serialize({ write }) {
write(this.weak);
write(this.optional);
write(this.loc);
}
deserialize({ read }) {
this.weak = read();
this.optional = read();
this.loc = read();
}
}
2019-10-30 14:57:55 +08:00
Dependency.NO_EXPORTS_REFERENCED = [];
Dependency.EXPORTS_OBJECT_REFERENCED = [[]];
Object.defineProperty(Dependency.prototype, "module", {
2018-07-25 15:33:48 +08:00
/**
* @deprecated
* @returns {never} throws
*/
get() {
throw new Error(
"module property was removed from Dependency (use compilation.moduleGraph.getModule(dependency) instead)"
);
},
/**
* @deprecated
* @returns {never} throws
2018-07-25 15:33:48 +08:00
*/
set() {
throw new Error(
"module property was removed from Dependency (use compilation.moduleGraph.updateModule(dependency, module) instead)"
);
}
});
Object.defineProperty(Dependency.prototype, "disconnect", {
get() {
throw new Error(
"disconnect was removed from Dependency (Dependency no longer carries graph specific information)"
);
}
});
2018-07-09 14:48:34 +08:00
module.exports = Dependency;