| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const memoize = require("./util/memoize");
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("./ChunkGraph")} ChunkGraph */
|
|---|
| 11 | /** @typedef {import("./DependenciesBlock")} DependenciesBlock */
|
|---|
| 12 | /** @typedef {import("./Module")} Module */
|
|---|
| 13 | /** @typedef {import("./ModuleGraph")} ModuleGraph */
|
|---|
| 14 | /** @typedef {import("./ModuleGraphConnection")} ModuleGraphConnection */
|
|---|
| 15 | /** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
|
|---|
| 16 | /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
|---|
| 17 | /** @typedef {import("./errors/WebpackError")} WebpackError */
|
|---|
| 18 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 19 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 20 | /** @typedef {import("./util/Hash")} Hash */
|
|---|
| 21 | /** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 22 | /** @typedef {import("./dependencies/ModuleDependency")} ModuleDependency */
|
|---|
| 23 | /**
|
|---|
| 24 | * Defines the update hash context type used by this module.
|
|---|
| 25 | * @typedef {object} UpdateHashContext
|
|---|
| 26 | * @property {ChunkGraph} chunkGraph
|
|---|
| 27 | * @property {RuntimeSpec} runtime
|
|---|
| 28 | * @property {RuntimeTemplate=} runtimeTemplate
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Defines the source position type used by this module.
|
|---|
| 33 | * @typedef {object} SourcePosition
|
|---|
| 34 | * @property {number} line
|
|---|
| 35 | * @property {number=} column
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Defines the real dependency location type used by this module.
|
|---|
| 40 | * @typedef {object} RealDependencyLocation
|
|---|
| 41 | * @property {SourcePosition} start
|
|---|
| 42 | * @property {SourcePosition=} end
|
|---|
| 43 | * @property {number=} index
|
|---|
| 44 | */
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Defines the synthetic dependency location type used by this module.
|
|---|
| 48 | * @typedef {object} SyntheticDependencyLocation
|
|---|
| 49 | * @property {string} name
|
|---|
| 50 | * @property {number=} index
|
|---|
| 51 | */
|
|---|
| 52 |
|
|---|
| 53 | /** @typedef {SyntheticDependencyLocation | RealDependencyLocation} DependencyLocation */
|
|---|
| 54 |
|
|---|
| 55 | /** @typedef {string} ExportInfoName */
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Defines the export spec type used by this module.
|
|---|
| 59 | * @typedef {object} ExportSpec
|
|---|
| 60 | * @property {ExportInfoName} name the name of the export
|
|---|
| 61 | * @property {boolean=} canMangle can the export be renamed (defaults to true)
|
|---|
| 62 | * @property {boolean=} terminalBinding is the export a terminal binding that should be checked for export star conflicts
|
|---|
| 63 | * @property {(string | ExportSpec)[]=} exports nested exports
|
|---|
| 64 | * @property {ModuleGraphConnection=} from when reexported: from which module
|
|---|
| 65 | * @property {string[] | null=} export when reexported: from which export
|
|---|
| 66 | * @property {number=} priority when reexported: with which priority
|
|---|
| 67 | * @property {boolean=} hidden export is not visible, because another export blends over it
|
|---|
| 68 | */
|
|---|
| 69 |
|
|---|
| 70 | /** @typedef {Set<string>} ExportsSpecExcludeExports */
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Defines the exports spec type used by this module.
|
|---|
| 74 | * @typedef {object} ExportsSpec
|
|---|
| 75 | * @property {(string | ExportSpec)[] | true | null} exports exported names, true for unknown exports or null for no exports
|
|---|
| 76 | * @property {ExportsSpecExcludeExports=} excludeExports when exports = true, list of unaffected exports
|
|---|
| 77 | * @property {(Set<string> | null)=} hideExports list of maybe prior exposed, but now hidden exports
|
|---|
| 78 | * @property {ModuleGraphConnection=} from when reexported: from which module
|
|---|
| 79 | * @property {number=} priority when reexported: with which priority
|
|---|
| 80 | * @property {boolean=} canMangle can the export be renamed (defaults to true)
|
|---|
| 81 | * @property {boolean=} terminalBinding are the exports terminal bindings that should be checked for export star conflicts
|
|---|
| 82 | * @property {Module[]=} dependencies module on which the result depends on
|
|---|
| 83 | */
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Defines the referenced export type used by this module.
|
|---|
| 87 | * @typedef {object} ReferencedExport
|
|---|
| 88 | * @property {string[]} name name of the referenced export
|
|---|
| 89 | * @property {boolean=} canMangle when false, referenced export can not be mangled, defaults to true
|
|---|
| 90 | */
|
|---|
| 91 |
|
|---|
| 92 | /** @typedef {string[][]} RawReferencedExports */
|
|---|
| 93 | /** @typedef {(string[] | ReferencedExport)[]} ReferencedExports */
|
|---|
| 94 |
|
|---|
| 95 | /** @typedef {(moduleGraphConnection: ModuleGraphConnection, runtime: RuntimeSpec) => ConnectionState} GetConditionFn */
|
|---|
| 96 |
|
|---|
| 97 | const TRANSITIVE = /** @type {symbol} */ (Symbol("transitive"));
|
|---|
| 98 |
|
|---|
| 99 | const getIgnoredModule = memoize(() => {
|
|---|
| 100 | const RawModule = require("./RawModule");
|
|---|
| 101 |
|
|---|
| 102 | const module = new RawModule("/* (ignored) */", "ignored", "(ignored)");
|
|---|
| 103 | module.factoryMeta = { sideEffectFree: true };
|
|---|
| 104 | return module;
|
|---|
| 105 | });
|
|---|
| 106 |
|
|---|
| 107 | class Dependency {
|
|---|
| 108 | constructor() {
|
|---|
| 109 | /** @type {Module | undefined} */
|
|---|
| 110 | this._parentModule = undefined;
|
|---|
| 111 | /** @type {DependenciesBlock | undefined} */
|
|---|
| 112 | this._parentDependenciesBlock = undefined;
|
|---|
| 113 | /** @type {number} */
|
|---|
| 114 | this._parentDependenciesBlockIndex = -1;
|
|---|
| 115 | // TODO check if this can be moved into ModuleDependency
|
|---|
| 116 | /** @type {boolean} */
|
|---|
| 117 | this.weak = false;
|
|---|
| 118 | // TODO check if this can be moved into ModuleDependency
|
|---|
| 119 | /** @type {boolean | undefined} */
|
|---|
| 120 | this.optional = false;
|
|---|
| 121 | this._locSL = 0;
|
|---|
| 122 | this._locSC = 0;
|
|---|
| 123 | this._locEL = 0;
|
|---|
| 124 | this._locEC = 0;
|
|---|
| 125 | /** @type {undefined | number} */
|
|---|
| 126 | this._locI = undefined;
|
|---|
| 127 | /** @type {undefined | string} */
|
|---|
| 128 | this._locN = undefined;
|
|---|
| 129 | /** @type {undefined | DependencyLocation} */
|
|---|
| 130 | this._loc = undefined;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * Returns a display name for the type of dependency.
|
|---|
| 135 | * @returns {string} a display name for the type of dependency
|
|---|
| 136 | */
|
|---|
| 137 | get type() {
|
|---|
| 138 | return "unknown";
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /**
|
|---|
| 142 | * Returns a dependency category, typical categories are "commonjs", "amd", "esm".
|
|---|
| 143 | * @returns {string} a dependency category, typical categories are "commonjs", "amd", "esm"
|
|---|
| 144 | */
|
|---|
| 145 | get category() {
|
|---|
| 146 | return "unknown";
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Returns location.
|
|---|
| 151 | * @returns {DependencyLocation} location
|
|---|
| 152 | */
|
|---|
| 153 | get loc() {
|
|---|
| 154 | if (this._loc !== undefined) return this._loc;
|
|---|
| 155 |
|
|---|
| 156 | /** @type {SyntheticDependencyLocation & RealDependencyLocation} */
|
|---|
| 157 | const loc = {};
|
|---|
| 158 |
|
|---|
| 159 | if (this._locSL > 0) {
|
|---|
| 160 | loc.start = { line: this._locSL, column: this._locSC };
|
|---|
| 161 | }
|
|---|
| 162 | if (this._locEL > 0) {
|
|---|
| 163 | loc.end = { line: this._locEL, column: this._locEC };
|
|---|
| 164 | }
|
|---|
| 165 | if (this._locN !== undefined) {
|
|---|
| 166 | loc.name = this._locN;
|
|---|
| 167 | }
|
|---|
| 168 | if (this._locI !== undefined) {
|
|---|
| 169 | loc.index = this._locI;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | return (this._loc = loc);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | set loc(loc) {
|
|---|
| 176 | if ("start" in loc && typeof loc.start === "object") {
|
|---|
| 177 | this._locSL = loc.start.line || 0;
|
|---|
| 178 | this._locSC = loc.start.column || 0;
|
|---|
| 179 | } else {
|
|---|
| 180 | this._locSL = 0;
|
|---|
| 181 | this._locSC = 0;
|
|---|
| 182 | }
|
|---|
| 183 | if ("end" in loc && typeof loc.end === "object") {
|
|---|
| 184 | this._locEL = loc.end.line || 0;
|
|---|
| 185 | this._locEC = loc.end.column || 0;
|
|---|
| 186 | } else {
|
|---|
| 187 | this._locEL = 0;
|
|---|
| 188 | this._locEC = 0;
|
|---|
| 189 | }
|
|---|
| 190 | this._locI = "index" in loc ? loc.index : undefined;
|
|---|
| 191 | this._locN = "name" in loc ? loc.name : undefined;
|
|---|
| 192 | this._loc = loc;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 196 | * Updates loc using the provided start line.
|
|---|
| 197 | * @param {number} startLine start line
|
|---|
| 198 | * @param {number} startColumn start column
|
|---|
| 199 | * @param {number} endLine end line
|
|---|
| 200 | * @param {number} endColumn end column
|
|---|
| 201 | */
|
|---|
| 202 | setLoc(startLine, startColumn, endLine, endColumn) {
|
|---|
| 203 | this._locSL = startLine;
|
|---|
| 204 | this._locSC = startColumn;
|
|---|
| 205 | this._locEL = endLine;
|
|---|
| 206 | this._locEC = endColumn;
|
|---|
| 207 | this._locI = undefined;
|
|---|
| 208 | this._locN = undefined;
|
|---|
| 209 | this._loc = undefined;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | /**
|
|---|
| 213 | * Returns a request context.
|
|---|
| 214 | * @returns {string | undefined} a request context
|
|---|
| 215 | */
|
|---|
| 216 | getContext() {
|
|---|
| 217 | return undefined;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /**
|
|---|
| 221 | * Returns an identifier to merge equal requests.
|
|---|
| 222 | * @returns {string | null} an identifier to merge equal requests
|
|---|
| 223 | */
|
|---|
| 224 | getResourceIdentifier() {
|
|---|
| 225 | return null;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /**
|
|---|
| 229 | * Could affect referencing module.
|
|---|
| 230 | * @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
|
|---|
| 231 | */
|
|---|
| 232 | couldAffectReferencingModule() {
|
|---|
| 233 | return TRANSITIVE;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /**
|
|---|
| 237 | * Returns the referenced module and export
|
|---|
| 238 | * @deprecated
|
|---|
| 239 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 240 | * @returns {never} throws error
|
|---|
| 241 | */
|
|---|
| 242 | getReference(moduleGraph) {
|
|---|
| 243 | throw new Error(
|
|---|
| 244 | "Dependency.getReference was removed in favor of Dependency.getReferencedExports, ModuleGraph.getModule, ModuleGraph.getConnection(), and ModuleGraphConnection.getActiveState(runtime)"
|
|---|
| 245 | );
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /**
|
|---|
| 249 | * Returns list of exports referenced by this dependency
|
|---|
| 250 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 251 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
|---|
| 252 | * @returns {ReferencedExports} referenced exports
|
|---|
| 253 | */
|
|---|
| 254 | getReferencedExports(moduleGraph, runtime) {
|
|---|
| 255 | return Dependency.EXPORTS_OBJECT_REFERENCED;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | /**
|
|---|
| 259 | * Returns function to determine if the connection is active.
|
|---|
| 260 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 261 | * @returns {null | false | GetConditionFn} function to determine if the connection is active
|
|---|
| 262 | */
|
|---|
| 263 | getCondition(moduleGraph) {
|
|---|
| 264 | return null;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /**
|
|---|
| 268 | * Returns the exported names
|
|---|
| 269 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 270 | * @returns {ExportsSpec | undefined} export names
|
|---|
| 271 | */
|
|---|
| 272 | getExports(moduleGraph) {
|
|---|
| 273 | return undefined;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | /**
|
|---|
| 277 | * Returns warnings.
|
|---|
| 278 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 279 | * @returns {WebpackError[] | null | undefined} warnings
|
|---|
| 280 | */
|
|---|
| 281 | getWarnings(moduleGraph) {
|
|---|
| 282 | return null;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /**
|
|---|
| 286 | * Returns errors.
|
|---|
| 287 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 288 | * @returns {WebpackError[] | null | undefined} errors
|
|---|
| 289 | */
|
|---|
| 290 | getErrors(moduleGraph) {
|
|---|
| 291 | return null;
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | /**
|
|---|
| 295 | * Updates the hash with the data contributed by this instance.
|
|---|
| 296 | * @param {Hash} hash hash to be updated
|
|---|
| 297 | * @param {UpdateHashContext} context context
|
|---|
| 298 | * @returns {void}
|
|---|
| 299 | */
|
|---|
| 300 | updateHash(hash, context) {}
|
|---|
| 301 |
|
|---|
| 302 | /**
|
|---|
| 303 | * implement this method to allow the occurrence order plugin to count correctly
|
|---|
| 304 | * @returns {number} count how often the id is used in this dependency
|
|---|
| 305 | */
|
|---|
| 306 | getNumberOfIdOccurrences() {
|
|---|
| 307 | return 1;
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | /**
|
|---|
| 311 | * Gets module evaluation side effects state.
|
|---|
| 312 | * @param {ModuleGraph} moduleGraph the module graph
|
|---|
| 313 | * @returns {ConnectionState} how this dependency connects the module to referencing modules
|
|---|
| 314 | */
|
|---|
| 315 | getModuleEvaluationSideEffectsState(moduleGraph) {
|
|---|
| 316 | return true;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /**
|
|---|
| 320 | * Creates an ignored module.
|
|---|
| 321 | * @param {string} context context directory
|
|---|
| 322 | * @returns {Module} ignored module
|
|---|
| 323 | */
|
|---|
| 324 | createIgnoredModule(context) {
|
|---|
| 325 | return getIgnoredModule();
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | /**
|
|---|
| 329 | * Returns true if this dependency can be concatenated
|
|---|
| 330 | * @returns {boolean} true if this dependency can be concatenated
|
|---|
| 331 | */
|
|---|
| 332 | canConcatenate() {
|
|---|
| 333 | return false;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | /**
|
|---|
| 337 | * Serializes this instance into the provided serializer context.
|
|---|
| 338 | * @param {ObjectSerializerContext} context context
|
|---|
| 339 | */
|
|---|
| 340 | serialize({ write }) {
|
|---|
| 341 | write(this.weak);
|
|---|
| 342 | write(this.optional);
|
|---|
| 343 | write(this._locSL);
|
|---|
| 344 | write(this._locSC);
|
|---|
| 345 | write(this._locEL);
|
|---|
| 346 | write(this._locEC);
|
|---|
| 347 | write(this._locI);
|
|---|
| 348 | write(this._locN);
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | /**
|
|---|
| 352 | * Restores this instance from the provided deserializer context.
|
|---|
| 353 | * @param {ObjectDeserializerContext} context context
|
|---|
| 354 | */
|
|---|
| 355 | deserialize({ read }) {
|
|---|
| 356 | this.weak = read();
|
|---|
| 357 | this.optional = read();
|
|---|
| 358 | this._locSL = read();
|
|---|
| 359 | this._locSC = read();
|
|---|
| 360 | this._locEL = read();
|
|---|
| 361 | this._locEC = read();
|
|---|
| 362 | this._locI = read();
|
|---|
| 363 | this._locN = read();
|
|---|
| 364 | }
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | /** @type {RawReferencedExports} */
|
|---|
| 368 | Dependency.NO_EXPORTS_REFERENCED = [];
|
|---|
| 369 | /** @type {RawReferencedExports} */
|
|---|
| 370 | Dependency.EXPORTS_OBJECT_REFERENCED = [[]];
|
|---|
| 371 |
|
|---|
| 372 | // TODO remove in webpack 6
|
|---|
| 373 | Object.defineProperty(Dependency.prototype, "module", {
|
|---|
| 374 | /**
|
|---|
| 375 | * Returns throws.
|
|---|
| 376 | * @deprecated
|
|---|
| 377 | * @returns {EXPECTED_ANY} throws
|
|---|
| 378 | */
|
|---|
| 379 | get() {
|
|---|
| 380 | throw new Error(
|
|---|
| 381 | "module property was removed from Dependency (use compilation.moduleGraph.getModule(dependency) instead)"
|
|---|
| 382 | );
|
|---|
| 383 | },
|
|---|
| 384 |
|
|---|
| 385 | /**
|
|---|
| 386 | * Updates module.
|
|---|
| 387 | * @deprecated
|
|---|
| 388 | * @returns {never} throws
|
|---|
| 389 | */
|
|---|
| 390 | set() {
|
|---|
| 391 | throw new Error(
|
|---|
| 392 | "module property was removed from Dependency (use compilation.moduleGraph.updateModule(dependency, module) instead)"
|
|---|
| 393 | );
|
|---|
| 394 | }
|
|---|
| 395 | });
|
|---|
| 396 |
|
|---|
| 397 | /**
|
|---|
| 398 | * Returns true if the dependency is a low priority dependency.
|
|---|
| 399 | * @param {Dependency} dependency dep
|
|---|
| 400 | * @returns {boolean} true if the dependency is a low priority dependency
|
|---|
| 401 | */
|
|---|
| 402 | Dependency.isLowPriorityDependency = (dependency) =>
|
|---|
| 403 | /** @type {ModuleDependency} */ (dependency).sourceOrder === Infinity;
|
|---|
| 404 |
|
|---|
| 405 | // TODO in webpack 6, call canConcatenate() directly on the dependency instance instead of using this static method.
|
|---|
| 406 | /**
|
|---|
| 407 | * Returns true if the dependency can be concatenated (scope hoisting).
|
|---|
| 408 | * @param {Dependency} dependency dep
|
|---|
| 409 | * @returns {boolean} true if this dependency supports concatenation
|
|---|
| 410 | */
|
|---|
| 411 | Dependency.canConcatenate = (dependency) => {
|
|---|
| 412 | if (typeof dependency.canConcatenate === "function") {
|
|---|
| 413 | return dependency.canConcatenate();
|
|---|
| 414 | }
|
|---|
| 415 | return false;
|
|---|
| 416 | };
|
|---|
| 417 |
|
|---|
| 418 | // TODO remove in webpack 6
|
|---|
| 419 | Object.defineProperty(Dependency.prototype, "disconnect", {
|
|---|
| 420 | /**
|
|---|
| 421 | * Returns throws.
|
|---|
| 422 | * @deprecated
|
|---|
| 423 | * @returns {EXPECTED_ANY} throws
|
|---|
| 424 | */
|
|---|
| 425 | get() {
|
|---|
| 426 | throw new Error(
|
|---|
| 427 | "disconnect was removed from Dependency (Dependency no longer carries graph specific information)"
|
|---|
| 428 | );
|
|---|
| 429 | }
|
|---|
| 430 | });
|
|---|
| 431 |
|
|---|
| 432 | Dependency.TRANSITIVE = TRANSITIVE;
|
|---|
| 433 |
|
|---|
| 434 | module.exports = Dependency;
|
|---|