source: trip-planner-front/node_modules/webpack/lib/Module.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 28.9 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const util = require("util");
9const ChunkGraph = require("./ChunkGraph");
10const DependenciesBlock = require("./DependenciesBlock");
11const ModuleGraph = require("./ModuleGraph");
12const RuntimeGlobals = require("./RuntimeGlobals");
13const { first } = require("./util/SetHelpers");
14const { compareChunksById } = require("./util/comparators");
15const makeSerializable = require("./util/makeSerializable");
16
17/** @typedef {import("webpack-sources").Source} Source */
18/** @typedef {import("../declarations/WebpackOptions").ResolveOptions} ResolveOptions */
19/** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
20/** @typedef {import("./Chunk")} Chunk */
21/** @typedef {import("./ChunkGroup")} ChunkGroup */
22/** @typedef {import("./Compilation")} Compilation */
23/** @typedef {import("./ConcatenationScope")} ConcatenationScope */
24/** @typedef {import("./Dependency")} Dependency */
25/** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
26/** @typedef {import("./DependencyTemplates")} DependencyTemplates */
27/** @typedef {import("./ExportsInfo").UsageStateType} UsageStateType */
28/** @typedef {import("./FileSystemInfo")} FileSystemInfo */
29/** @typedef {import("./ModuleGraphConnection").ConnectionState} ConnectionState */
30/** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */
31/** @typedef {import("./RequestShortener")} RequestShortener */
32/** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
33/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
34/** @typedef {import("./WebpackError")} WebpackError */
35/** @typedef {import("./util/Hash")} Hash */
36/** @template T @typedef {import("./util/LazySet")<T>} LazySet<T> */
37/** @template T @typedef {import("./util/SortableSet")<T>} SortableSet<T> */
38/** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
39/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
40
41/**
42 * @typedef {Object} SourceContext
43 * @property {DependencyTemplates} dependencyTemplates the dependency templates
44 * @property {RuntimeTemplate} runtimeTemplate the runtime template
45 * @property {ModuleGraph} moduleGraph the module graph
46 * @property {ChunkGraph} chunkGraph the chunk graph
47 * @property {RuntimeSpec} runtime the runtimes code should be generated for
48 * @property {string=} type the type of source that should be generated
49 */
50
51/**
52 * @typedef {Object} CodeGenerationContext
53 * @property {DependencyTemplates} dependencyTemplates the dependency templates
54 * @property {RuntimeTemplate} runtimeTemplate the runtime template
55 * @property {ModuleGraph} moduleGraph the module graph
56 * @property {ChunkGraph} chunkGraph the chunk graph
57 * @property {RuntimeSpec} runtime the runtimes code should be generated for
58 * @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
59 */
60
61/**
62 * @typedef {Object} ConcatenationBailoutReasonContext
63 * @property {ModuleGraph} moduleGraph the module graph
64 * @property {ChunkGraph} chunkGraph the chunk graph
65 */
66
67/**
68 * @typedef {Object} CodeGenerationResult
69 * @property {Map<string, Source>} sources the resulting sources for all source types
70 * @property {Map<string, any>=} data the resulting data for all source types
71 * @property {ReadonlySet<string>} runtimeRequirements the runtime requirements
72 * @property {string=} hash a hash of the code generation result (will be automatically calculated from sources and runtimeRequirements if not provided)
73 */
74
75/**
76 * @typedef {Object} LibIdentOptions
77 * @property {string} context absolute context path to which lib ident is relative to
78 * @property {Object=} associatedObjectForCache object for caching
79 */
80
81/**
82 * @typedef {Object} KnownBuildMeta
83 * @property {string=} moduleArgument
84 * @property {string=} exportsArgument
85 * @property {boolean=} strict
86 * @property {string=} moduleConcatenationBailout
87 * @property {("default" | "namespace" | "flagged" | "dynamic")=} exportsType
88 * @property {(false | "redirect" | "redirect-warn")=} defaultObject
89 * @property {boolean=} strictHarmonyModule
90 * @property {boolean=} async
91 * @property {boolean=} sideEffectFree
92 */
93
94/**
95 * @typedef {Object} NeedBuildContext
96 * @property {Compilation} compilation
97 * @property {FileSystemInfo} fileSystemInfo
98 * @property {Map<string, string | Set<string>>} valueCacheVersions
99 */
100
101/** @typedef {KnownBuildMeta & Record<string, any>} BuildMeta */
102
103const EMPTY_RESOLVE_OPTIONS = {};
104
105let debugId = 1000;
106
107const DEFAULT_TYPES_UNKNOWN = new Set(["unknown"]);
108const DEFAULT_TYPES_JS = new Set(["javascript"]);
109
110const deprecatedNeedRebuild = util.deprecate(
111 (module, context) => {
112 return module.needRebuild(
113 context.fileSystemInfo.getDeprecatedFileTimestamps(),
114 context.fileSystemInfo.getDeprecatedContextTimestamps()
115 );
116 },
117 "Module.needRebuild is deprecated in favor of Module.needBuild",
118 "DEP_WEBPACK_MODULE_NEED_REBUILD"
119);
120
121/** @typedef {(requestShortener: RequestShortener) => string} OptimizationBailoutFunction */
122
123class Module extends DependenciesBlock {
124 /**
125 * @param {string} type the module type
126 * @param {string=} context an optional context
127 * @param {string=} layer an optional layer in which the module is
128 */
129 constructor(type, context = null, layer = null) {
130 super();
131
132 /** @type {string} */
133 this.type = type;
134 /** @type {string | null} */
135 this.context = context;
136 /** @type {string | null} */
137 this.layer = layer;
138 /** @type {boolean} */
139 this.needId = true;
140
141 // Unique Id
142 /** @type {number} */
143 this.debugId = debugId++;
144
145 // Info from Factory
146 /** @type {ResolveOptions} */
147 this.resolveOptions = EMPTY_RESOLVE_OPTIONS;
148 /** @type {object | undefined} */
149 this.factoryMeta = undefined;
150 // TODO refactor this -> options object filled from Factory
151 // TODO webpack 6: use an enum
152 /** @type {boolean} */
153 this.useSourceMap = false;
154 /** @type {boolean} */
155 this.useSimpleSourceMap = false;
156
157 // Info from Build
158 /** @type {WebpackError[] | undefined} */
159 this._warnings = undefined;
160 /** @type {WebpackError[] | undefined} */
161 this._errors = undefined;
162 /** @type {BuildMeta} */
163 this.buildMeta = undefined;
164 /** @type {Record<string, any>} */
165 this.buildInfo = undefined;
166 /** @type {Dependency[] | undefined} */
167 this.presentationalDependencies = undefined;
168 }
169
170 // TODO remove in webpack 6
171 // BACKWARD-COMPAT START
172 get id() {
173 return ChunkGraph.getChunkGraphForModule(
174 this,
175 "Module.id",
176 "DEP_WEBPACK_MODULE_ID"
177 ).getModuleId(this);
178 }
179
180 set id(value) {
181 if (value === "") {
182 this.needId = false;
183 return;
184 }
185 ChunkGraph.getChunkGraphForModule(
186 this,
187 "Module.id",
188 "DEP_WEBPACK_MODULE_ID"
189 ).setModuleId(this, value);
190 }
191
192 /**
193 * @returns {string} the hash of the module
194 */
195 get hash() {
196 return ChunkGraph.getChunkGraphForModule(
197 this,
198 "Module.hash",
199 "DEP_WEBPACK_MODULE_HASH"
200 ).getModuleHash(this, undefined);
201 }
202
203 /**
204 * @returns {string} the shortened hash of the module
205 */
206 get renderedHash() {
207 return ChunkGraph.getChunkGraphForModule(
208 this,
209 "Module.renderedHash",
210 "DEP_WEBPACK_MODULE_RENDERED_HASH"
211 ).getRenderedModuleHash(this, undefined);
212 }
213
214 get profile() {
215 return ModuleGraph.getModuleGraphForModule(
216 this,
217 "Module.profile",
218 "DEP_WEBPACK_MODULE_PROFILE"
219 ).getProfile(this);
220 }
221
222 set profile(value) {
223 ModuleGraph.getModuleGraphForModule(
224 this,
225 "Module.profile",
226 "DEP_WEBPACK_MODULE_PROFILE"
227 ).setProfile(this, value);
228 }
229
230 get index() {
231 return ModuleGraph.getModuleGraphForModule(
232 this,
233 "Module.index",
234 "DEP_WEBPACK_MODULE_INDEX"
235 ).getPreOrderIndex(this);
236 }
237
238 set index(value) {
239 ModuleGraph.getModuleGraphForModule(
240 this,
241 "Module.index",
242 "DEP_WEBPACK_MODULE_INDEX"
243 ).setPreOrderIndex(this, value);
244 }
245
246 get index2() {
247 return ModuleGraph.getModuleGraphForModule(
248 this,
249 "Module.index2",
250 "DEP_WEBPACK_MODULE_INDEX2"
251 ).getPostOrderIndex(this);
252 }
253
254 set index2(value) {
255 ModuleGraph.getModuleGraphForModule(
256 this,
257 "Module.index2",
258 "DEP_WEBPACK_MODULE_INDEX2"
259 ).setPostOrderIndex(this, value);
260 }
261
262 get depth() {
263 return ModuleGraph.getModuleGraphForModule(
264 this,
265 "Module.depth",
266 "DEP_WEBPACK_MODULE_DEPTH"
267 ).getDepth(this);
268 }
269
270 set depth(value) {
271 ModuleGraph.getModuleGraphForModule(
272 this,
273 "Module.depth",
274 "DEP_WEBPACK_MODULE_DEPTH"
275 ).setDepth(this, value);
276 }
277
278 get issuer() {
279 return ModuleGraph.getModuleGraphForModule(
280 this,
281 "Module.issuer",
282 "DEP_WEBPACK_MODULE_ISSUER"
283 ).getIssuer(this);
284 }
285
286 set issuer(value) {
287 ModuleGraph.getModuleGraphForModule(
288 this,
289 "Module.issuer",
290 "DEP_WEBPACK_MODULE_ISSUER"
291 ).setIssuer(this, value);
292 }
293
294 get usedExports() {
295 return ModuleGraph.getModuleGraphForModule(
296 this,
297 "Module.usedExports",
298 "DEP_WEBPACK_MODULE_USED_EXPORTS"
299 ).getUsedExports(this, undefined);
300 }
301
302 /**
303 * @deprecated
304 * @returns {(string | OptimizationBailoutFunction)[]} list
305 */
306 get optimizationBailout() {
307 return ModuleGraph.getModuleGraphForModule(
308 this,
309 "Module.optimizationBailout",
310 "DEP_WEBPACK_MODULE_OPTIMIZATION_BAILOUT"
311 ).getOptimizationBailout(this);
312 }
313
314 get optional() {
315 return this.isOptional(
316 ModuleGraph.getModuleGraphForModule(
317 this,
318 "Module.optional",
319 "DEP_WEBPACK_MODULE_OPTIONAL"
320 )
321 );
322 }
323
324 addChunk(chunk) {
325 const chunkGraph = ChunkGraph.getChunkGraphForModule(
326 this,
327 "Module.addChunk",
328 "DEP_WEBPACK_MODULE_ADD_CHUNK"
329 );
330 if (chunkGraph.isModuleInChunk(this, chunk)) return false;
331 chunkGraph.connectChunkAndModule(chunk, this);
332 return true;
333 }
334
335 removeChunk(chunk) {
336 return ChunkGraph.getChunkGraphForModule(
337 this,
338 "Module.removeChunk",
339 "DEP_WEBPACK_MODULE_REMOVE_CHUNK"
340 ).disconnectChunkAndModule(chunk, this);
341 }
342
343 isInChunk(chunk) {
344 return ChunkGraph.getChunkGraphForModule(
345 this,
346 "Module.isInChunk",
347 "DEP_WEBPACK_MODULE_IS_IN_CHUNK"
348 ).isModuleInChunk(this, chunk);
349 }
350
351 isEntryModule() {
352 return ChunkGraph.getChunkGraphForModule(
353 this,
354 "Module.isEntryModule",
355 "DEP_WEBPACK_MODULE_IS_ENTRY_MODULE"
356 ).isEntryModule(this);
357 }
358
359 getChunks() {
360 return ChunkGraph.getChunkGraphForModule(
361 this,
362 "Module.getChunks",
363 "DEP_WEBPACK_MODULE_GET_CHUNKS"
364 ).getModuleChunks(this);
365 }
366
367 getNumberOfChunks() {
368 return ChunkGraph.getChunkGraphForModule(
369 this,
370 "Module.getNumberOfChunks",
371 "DEP_WEBPACK_MODULE_GET_NUMBER_OF_CHUNKS"
372 ).getNumberOfModuleChunks(this);
373 }
374
375 get chunksIterable() {
376 return ChunkGraph.getChunkGraphForModule(
377 this,
378 "Module.chunksIterable",
379 "DEP_WEBPACK_MODULE_CHUNKS_ITERABLE"
380 ).getOrderedModuleChunksIterable(this, compareChunksById);
381 }
382
383 /**
384 * @param {string} exportName a name of an export
385 * @returns {boolean | null} true, if the export is provided why the module.
386 * null, if it's unknown.
387 * false, if it's not provided.
388 */
389 isProvided(exportName) {
390 return ModuleGraph.getModuleGraphForModule(
391 this,
392 "Module.usedExports",
393 "DEP_WEBPACK_MODULE_USED_EXPORTS"
394 ).isExportProvided(this, exportName);
395 }
396 // BACKWARD-COMPAT END
397
398 /**
399 * @deprecated moved to .buildInfo.exportsArgument
400 * @returns {string} name of the exports argument
401 */
402 get exportsArgument() {
403 return (this.buildInfo && this.buildInfo.exportsArgument) || "exports";
404 }
405
406 /**
407 * @deprecated moved to .buildInfo.moduleArgument
408 * @returns {string} name of the module argument
409 */
410 get moduleArgument() {
411 return (this.buildInfo && this.buildInfo.moduleArgument) || "module";
412 }
413
414 /**
415 * @param {ModuleGraph} moduleGraph the module graph
416 * @param {boolean} strict the importing module is strict
417 * @returns {"namespace" | "default-only" | "default-with-named" | "dynamic"} export type
418 * "namespace": Exports is already a namespace object. namespace = exports.
419 * "dynamic": Check at runtime if __esModule is set. When set: namespace = { ...exports, default: exports }. When not set: namespace = { default: exports }.
420 * "default-only": Provide a namespace object with only default export. namespace = { default: exports }
421 * "default-with-named": Provide a namespace object with named and default export. namespace = { ...exports, default: exports }
422 */
423 getExportsType(moduleGraph, strict) {
424 switch (this.buildMeta && this.buildMeta.exportsType) {
425 case "flagged":
426 return strict ? "default-with-named" : "namespace";
427 case "namespace":
428 return "namespace";
429 case "default":
430 switch (this.buildMeta.defaultObject) {
431 case "redirect":
432 return "default-with-named";
433 case "redirect-warn":
434 return strict ? "default-only" : "default-with-named";
435 default:
436 return "default-only";
437 }
438 case "dynamic": {
439 if (strict) return "default-with-named";
440 // Try to figure out value of __esModule by following reexports
441 const handleDefault = () => {
442 switch (this.buildMeta.defaultObject) {
443 case "redirect":
444 case "redirect-warn":
445 return "default-with-named";
446 default:
447 return "default-only";
448 }
449 };
450 const exportInfo = moduleGraph.getReadOnlyExportInfo(
451 this,
452 "__esModule"
453 );
454 if (exportInfo.provided === false) {
455 return handleDefault();
456 }
457 const target = exportInfo.getTarget(moduleGraph);
458 if (
459 !target ||
460 !target.export ||
461 target.export.length !== 1 ||
462 target.export[0] !== "__esModule"
463 ) {
464 return "dynamic";
465 }
466 switch (
467 target.module.buildMeta &&
468 target.module.buildMeta.exportsType
469 ) {
470 case "flagged":
471 case "namespace":
472 return "namespace";
473 case "default":
474 return handleDefault();
475 default:
476 return "dynamic";
477 }
478 }
479 default:
480 return strict ? "default-with-named" : "dynamic";
481 }
482 }
483
484 /**
485 * @param {Dependency} presentationalDependency dependency being tied to module.
486 * This is a Dependency without edge in the module graph. It's only for presentation.
487 * @returns {void}
488 */
489 addPresentationalDependency(presentationalDependency) {
490 if (this.presentationalDependencies === undefined) {
491 this.presentationalDependencies = [];
492 }
493 this.presentationalDependencies.push(presentationalDependency);
494 }
495
496 /**
497 * Removes all dependencies and blocks
498 * @returns {void}
499 */
500 clearDependenciesAndBlocks() {
501 if (this.presentationalDependencies !== undefined) {
502 this.presentationalDependencies.length = 0;
503 }
504 super.clearDependenciesAndBlocks();
505 }
506
507 /**
508 * @param {WebpackError} warning the warning
509 * @returns {void}
510 */
511 addWarning(warning) {
512 if (this._warnings === undefined) {
513 this._warnings = [];
514 }
515 this._warnings.push(warning);
516 }
517
518 /**
519 * @returns {Iterable<WebpackError> | undefined} list of warnings if any
520 */
521 getWarnings() {
522 return this._warnings;
523 }
524
525 /**
526 * @returns {number} number of warnings
527 */
528 getNumberOfWarnings() {
529 return this._warnings !== undefined ? this._warnings.length : 0;
530 }
531
532 /**
533 * @param {WebpackError} error the error
534 * @returns {void}
535 */
536 addError(error) {
537 if (this._errors === undefined) {
538 this._errors = [];
539 }
540 this._errors.push(error);
541 }
542
543 /**
544 * @returns {Iterable<WebpackError> | undefined} list of errors if any
545 */
546 getErrors() {
547 return this._errors;
548 }
549
550 /**
551 * @returns {number} number of errors
552 */
553 getNumberOfErrors() {
554 return this._errors !== undefined ? this._errors.length : 0;
555 }
556
557 /**
558 * removes all warnings and errors
559 * @returns {void}
560 */
561 clearWarningsAndErrors() {
562 if (this._warnings !== undefined) {
563 this._warnings.length = 0;
564 }
565 if (this._errors !== undefined) {
566 this._errors.length = 0;
567 }
568 }
569
570 /**
571 * @param {ModuleGraph} moduleGraph the module graph
572 * @returns {boolean} true, if the module is optional
573 */
574 isOptional(moduleGraph) {
575 let hasConnections = false;
576 for (const r of moduleGraph.getIncomingConnections(this)) {
577 if (
578 !r.dependency ||
579 !r.dependency.optional ||
580 !r.isTargetActive(undefined)
581 ) {
582 return false;
583 }
584 hasConnections = true;
585 }
586 return hasConnections;
587 }
588
589 /**
590 * @param {ChunkGraph} chunkGraph the chunk graph
591 * @param {Chunk} chunk a chunk
592 * @param {Chunk=} ignoreChunk chunk to be ignored
593 * @returns {boolean} true, if the module is accessible from "chunk" when ignoring "ignoreChunk"
594 */
595 isAccessibleInChunk(chunkGraph, chunk, ignoreChunk) {
596 // Check if module is accessible in ALL chunk groups
597 for (const chunkGroup of chunk.groupsIterable) {
598 if (!this.isAccessibleInChunkGroup(chunkGraph, chunkGroup)) return false;
599 }
600 return true;
601 }
602
603 /**
604 * @param {ChunkGraph} chunkGraph the chunk graph
605 * @param {ChunkGroup} chunkGroup a chunk group
606 * @param {Chunk=} ignoreChunk chunk to be ignored
607 * @returns {boolean} true, if the module is accessible from "chunkGroup" when ignoring "ignoreChunk"
608 */
609 isAccessibleInChunkGroup(chunkGraph, chunkGroup, ignoreChunk) {
610 const queue = new Set([chunkGroup]);
611
612 // Check if module is accessible from all items of the queue
613 queueFor: for (const cg of queue) {
614 // 1. If module is in one of the chunks of the group we can continue checking the next items
615 // because it's accessible.
616 for (const chunk of cg.chunks) {
617 if (chunk !== ignoreChunk && chunkGraph.isModuleInChunk(this, chunk))
618 continue queueFor;
619 }
620 // 2. If the chunk group is initial, we can break here because it's not accessible.
621 if (chunkGroup.isInitial()) return false;
622 // 3. Enqueue all parents because it must be accessible from ALL parents
623 for (const parent of chunkGroup.parentsIterable) queue.add(parent);
624 }
625 // When we processed through the whole list and we didn't bailout, the module is accessible
626 return true;
627 }
628
629 /**
630 * @param {Chunk} chunk a chunk
631 * @param {ModuleGraph} moduleGraph the module graph
632 * @param {ChunkGraph} chunkGraph the chunk graph
633 * @returns {boolean} true, if the module has any reason why "chunk" should be included
634 */
635 hasReasonForChunk(chunk, moduleGraph, chunkGraph) {
636 // check for each reason if we need the chunk
637 for (const [
638 fromModule,
639 connections
640 ] of moduleGraph.getIncomingConnectionsByOriginModule(this)) {
641 if (!connections.some(c => c.isTargetActive(chunk.runtime))) continue;
642 for (const originChunk of chunkGraph.getModuleChunksIterable(
643 fromModule
644 )) {
645 // return true if module this is not reachable from originChunk when ignoring chunk
646 if (!this.isAccessibleInChunk(chunkGraph, originChunk, chunk))
647 return true;
648 }
649 }
650 return false;
651 }
652
653 /**
654 * @param {ModuleGraph} moduleGraph the module graph
655 * @param {RuntimeSpec} runtime the runtime
656 * @returns {boolean} true if at least one other module depends on this module
657 */
658 hasReasons(moduleGraph, runtime) {
659 for (const c of moduleGraph.getIncomingConnections(this)) {
660 if (c.isTargetActive(runtime)) return true;
661 }
662 return false;
663 }
664
665 /**
666 * @returns {string} for debugging
667 */
668 toString() {
669 return `Module[${this.debugId}: ${this.identifier()}]`;
670 }
671
672 /**
673 * @param {NeedBuildContext} context context info
674 * @param {function(WebpackError=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
675 * @returns {void}
676 */
677 needBuild(context, callback) {
678 callback(
679 null,
680 !this.buildMeta ||
681 this.needRebuild === Module.prototype.needRebuild ||
682 deprecatedNeedRebuild(this, context)
683 );
684 }
685
686 /**
687 * @deprecated Use needBuild instead
688 * @param {Map<string, number|null>} fileTimestamps timestamps of files
689 * @param {Map<string, number|null>} contextTimestamps timestamps of directories
690 * @returns {boolean} true, if the module needs a rebuild
691 */
692 needRebuild(fileTimestamps, contextTimestamps) {
693 return true;
694 }
695
696 /**
697 * @param {Hash} hash the hash used to track dependencies
698 * @param {UpdateHashContext} context context
699 * @returns {void}
700 */
701 updateHash(
702 hash,
703 context = {
704 chunkGraph: ChunkGraph.getChunkGraphForModule(
705 this,
706 "Module.updateHash",
707 "DEP_WEBPACK_MODULE_UPDATE_HASH"
708 ),
709 runtime: undefined
710 }
711 ) {
712 const { chunkGraph, runtime } = context;
713 hash.update(chunkGraph.getModuleGraphHash(this, runtime));
714 if (this.presentationalDependencies !== undefined) {
715 for (const dep of this.presentationalDependencies) {
716 dep.updateHash(hash, context);
717 }
718 }
719 super.updateHash(hash, context);
720 }
721
722 /**
723 * @returns {void}
724 */
725 invalidateBuild() {
726 // should be overridden to support this feature
727 }
728
729 /* istanbul ignore next */
730 /**
731 * @abstract
732 * @returns {string} a unique identifier of the module
733 */
734 identifier() {
735 const AbstractMethodError = require("./AbstractMethodError");
736 throw new AbstractMethodError();
737 }
738
739 /* istanbul ignore next */
740 /**
741 * @abstract
742 * @param {RequestShortener} requestShortener the request shortener
743 * @returns {string} a user readable identifier of the module
744 */
745 readableIdentifier(requestShortener) {
746 const AbstractMethodError = require("./AbstractMethodError");
747 throw new AbstractMethodError();
748 }
749
750 /* istanbul ignore next */
751 /**
752 * @abstract
753 * @param {WebpackOptions} options webpack options
754 * @param {Compilation} compilation the compilation
755 * @param {ResolverWithOptions} resolver the resolver
756 * @param {InputFileSystem} fs the file system
757 * @param {function(WebpackError=): void} callback callback function
758 * @returns {void}
759 */
760 build(options, compilation, resolver, fs, callback) {
761 const AbstractMethodError = require("./AbstractMethodError");
762 throw new AbstractMethodError();
763 }
764
765 /**
766 * @abstract
767 * @returns {Set<string>} types available (do not mutate)
768 */
769 getSourceTypes() {
770 // Better override this method to return the correct types
771 if (this.source === Module.prototype.source) {
772 return DEFAULT_TYPES_UNKNOWN;
773 } else {
774 return DEFAULT_TYPES_JS;
775 }
776 }
777
778 /**
779 * @abstract
780 * @deprecated Use codeGeneration() instead
781 * @param {DependencyTemplates} dependencyTemplates the dependency templates
782 * @param {RuntimeTemplate} runtimeTemplate the runtime template
783 * @param {string=} type the type of source that should be generated
784 * @returns {Source} generated source
785 */
786 source(dependencyTemplates, runtimeTemplate, type = "javascript") {
787 if (this.codeGeneration === Module.prototype.codeGeneration) {
788 const AbstractMethodError = require("./AbstractMethodError");
789 throw new AbstractMethodError();
790 }
791 const chunkGraph = ChunkGraph.getChunkGraphForModule(
792 this,
793 "Module.source() is deprecated. Use Compilation.codeGenerationResults.getSource(module, runtime, type) instead",
794 "DEP_WEBPACK_MODULE_SOURCE"
795 );
796 /** @type {CodeGenerationContext} */
797 const codeGenContext = {
798 dependencyTemplates,
799 runtimeTemplate,
800 moduleGraph: chunkGraph.moduleGraph,
801 chunkGraph,
802 runtime: undefined
803 };
804 const sources = this.codeGeneration(codeGenContext).sources;
805 return type ? sources.get(type) : sources.get(first(this.getSourceTypes()));
806 }
807
808 /* istanbul ignore next */
809 /**
810 * @abstract
811 * @param {string=} type the source type for which the size should be estimated
812 * @returns {number} the estimated size of the module (must be non-zero)
813 */
814 size(type) {
815 const AbstractMethodError = require("./AbstractMethodError");
816 throw new AbstractMethodError();
817 }
818
819 /**
820 * @param {LibIdentOptions} options options
821 * @returns {string | null} an identifier for library inclusion
822 */
823 libIdent(options) {
824 return null;
825 }
826
827 /**
828 * @returns {string | null} absolute path which should be used for condition matching (usually the resource path)
829 */
830 nameForCondition() {
831 return null;
832 }
833
834 /**
835 * @param {ConcatenationBailoutReasonContext} context context
836 * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
837 */
838 getConcatenationBailoutReason(context) {
839 return `Module Concatenation is not implemented for ${this.constructor.name}`;
840 }
841
842 /**
843 * @param {ModuleGraph} moduleGraph the module graph
844 * @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only
845 */
846 getSideEffectsConnectionState(moduleGraph) {
847 return true;
848 }
849
850 /**
851 * @param {CodeGenerationContext} context context for code generation
852 * @returns {CodeGenerationResult} result
853 */
854 codeGeneration(context) {
855 // Best override this method
856 const sources = new Map();
857 for (const type of this.getSourceTypes()) {
858 if (type !== "unknown") {
859 sources.set(
860 type,
861 this.source(
862 context.dependencyTemplates,
863 context.runtimeTemplate,
864 type
865 )
866 );
867 }
868 }
869 return {
870 sources,
871 runtimeRequirements: new Set([
872 RuntimeGlobals.module,
873 RuntimeGlobals.exports,
874 RuntimeGlobals.require
875 ])
876 };
877 }
878
879 /**
880 * @param {Chunk} chunk the chunk which condition should be checked
881 * @param {Compilation} compilation the compilation
882 * @returns {boolean} true, if the chunk is ok for the module
883 */
884 chunkCondition(chunk, compilation) {
885 return true;
886 }
887
888 /**
889 * Assuming this module is in the cache. Update the (cached) module with
890 * the fresh module from the factory. Usually updates internal references
891 * and properties.
892 * @param {Module} module fresh module
893 * @returns {void}
894 */
895 updateCacheModule(module) {
896 this.type = module.type;
897 this.layer = module.layer;
898 this.context = module.context;
899 this.factoryMeta = module.factoryMeta;
900 this.resolveOptions = module.resolveOptions;
901 }
902
903 /**
904 * Module should be unsafe cached. Get data that's needed for that.
905 * This data will be passed to restoreFromUnsafeCache later.
906 * @returns {object} cached data
907 */
908 getUnsafeCacheData() {
909 return {
910 factoryMeta: this.factoryMeta,
911 resolveOptions: this.resolveOptions
912 };
913 }
914
915 /**
916 * restore unsafe cache data
917 * @param {object} unsafeCacheData data from getUnsafeCacheData
918 * @param {NormalModuleFactory} normalModuleFactory the normal module factory handling the unsafe caching
919 */
920 _restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory) {
921 this.factoryMeta = unsafeCacheData.factoryMeta;
922 this.resolveOptions = unsafeCacheData.resolveOptions;
923 }
924
925 /**
926 * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
927 */
928 cleanupForCache() {
929 this.factoryMeta = undefined;
930 this.resolveOptions = undefined;
931 }
932
933 /**
934 * @returns {Source | null} the original source for the module before webpack transformation
935 */
936 originalSource() {
937 return null;
938 }
939
940 /**
941 * @param {LazySet<string>} fileDependencies set where file dependencies are added to
942 * @param {LazySet<string>} contextDependencies set where context dependencies are added to
943 * @param {LazySet<string>} missingDependencies set where missing dependencies are added to
944 * @param {LazySet<string>} buildDependencies set where build dependencies are added to
945 */
946 addCacheDependencies(
947 fileDependencies,
948 contextDependencies,
949 missingDependencies,
950 buildDependencies
951 ) {}
952
953 serialize(context) {
954 const { write } = context;
955 write(this.type);
956 write(this.layer);
957 write(this.context);
958 write(this.resolveOptions);
959 write(this.factoryMeta);
960 write(this.useSourceMap);
961 write(this.useSimpleSourceMap);
962 write(
963 this._warnings !== undefined && this._warnings.length === 0
964 ? undefined
965 : this._warnings
966 );
967 write(
968 this._errors !== undefined && this._errors.length === 0
969 ? undefined
970 : this._errors
971 );
972 write(this.buildMeta);
973 write(this.buildInfo);
974 write(this.presentationalDependencies);
975 super.serialize(context);
976 }
977
978 deserialize(context) {
979 const { read } = context;
980 this.type = read();
981 this.layer = read();
982 this.context = read();
983 this.resolveOptions = read();
984 this.factoryMeta = read();
985 this.useSourceMap = read();
986 this.useSimpleSourceMap = read();
987 this._warnings = read();
988 this._errors = read();
989 this.buildMeta = read();
990 this.buildInfo = read();
991 this.presentationalDependencies = read();
992 super.deserialize(context);
993 }
994}
995
996makeSerializable(Module, "webpack/lib/Module");
997
998// TODO remove in webpack 6
999Object.defineProperty(Module.prototype, "hasEqualsChunks", {
1000 get() {
1001 throw new Error(
1002 "Module.hasEqualsChunks was renamed (use hasEqualChunks instead)"
1003 );
1004 }
1005});
1006
1007// TODO remove in webpack 6
1008Object.defineProperty(Module.prototype, "isUsed", {
1009 get() {
1010 throw new Error(
1011 "Module.isUsed was renamed (use getUsedName, isExportUsed or isModuleUsed instead)"
1012 );
1013 }
1014});
1015
1016// TODO remove in webpack 6
1017Object.defineProperty(Module.prototype, "errors", {
1018 get: util.deprecate(
1019 /**
1020 * @this {Module}
1021 * @returns {WebpackError[]} array
1022 */
1023 function () {
1024 if (this._errors === undefined) {
1025 this._errors = [];
1026 }
1027 return this._errors;
1028 },
1029 "Module.errors was removed (use getErrors instead)",
1030 "DEP_WEBPACK_MODULE_ERRORS"
1031 )
1032});
1033
1034// TODO remove in webpack 6
1035Object.defineProperty(Module.prototype, "warnings", {
1036 get: util.deprecate(
1037 /**
1038 * @this {Module}
1039 * @returns {WebpackError[]} array
1040 */
1041 function () {
1042 if (this._warnings === undefined) {
1043 this._warnings = [];
1044 }
1045 return this._warnings;
1046 },
1047 "Module.warnings was removed (use getWarnings instead)",
1048 "DEP_WEBPACK_MODULE_WARNINGS"
1049 )
1050});
1051
1052// TODO remove in webpack 6
1053Object.defineProperty(Module.prototype, "used", {
1054 get() {
1055 throw new Error(
1056 "Module.used was refactored (use ModuleGraph.getUsedExports instead)"
1057 );
1058 },
1059 set(value) {
1060 throw new Error(
1061 "Module.used was refactored (use ModuleGraph.setUsedExports instead)"
1062 );
1063 }
1064});
1065
1066module.exports = Module;
Note: See TracBrowser for help on using the repository browser.