Module.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const ChunkGraph = require("./ChunkGraph");
  8. const DependenciesBlock = require("./DependenciesBlock");
  9. const ModuleGraph = require("./ModuleGraph");
  10. const {
  11. JAVASCRIPT_TYPE,
  12. UNKNOWN_TYPE
  13. } = require("./ModuleSourceTypeConstants");
  14. const { JAVASCRIPT_TYPES } = require("./ModuleSourceTypeConstants");
  15. const RuntimeGlobals = require("./RuntimeGlobals");
  16. const { first } = require("./util/SetHelpers");
  17. const { compareChunksById } = require("./util/comparators");
  18. const makeSerializable = require("./util/makeSerializable");
  19. /** @import { Source } from "webpack-sources" */
  20. /** @import { ResolveOptions } from "../declarations/WebpackOptions" */
  21. /**
  22. * @import {
  23. * WebpackOptionsNormalizedWithDefaults as WebpackOptions
  24. * } from "./config/defaults"
  25. */
  26. /** @import Chunk from "./Chunk" */
  27. /** @import { ModuleId } from "./ChunkGraph" */
  28. /** @import ChunkGroup from "./ChunkGroup" */
  29. /** @import CodeGenerationResults from "./CodeGenerationResults" */
  30. /**
  31. * @import Compilation, {
  32. * AssetInfo,
  33. * FileSystemDependencies,
  34. * UnsafeCacheData
  35. * } from "./Compilation"
  36. */
  37. /** @import ConcatenationScope from "./ConcatenationScope" */
  38. /** @import Dependency, { UpdateHashContext } from "./Dependency" */
  39. /** @import DependencyTemplates from "./DependencyTemplates" */
  40. /** @import { AllTypes } from "./ModuleSourceTypeConstants" */
  41. /** @import FileSystemInfo from "./FileSystemInfo" */
  42. /** @import { ConnectionState } from "./ModuleGraphConnection" */
  43. /** @import { ModuleTypes } from "./ModuleTypeConstants" */
  44. /** @import { OptimizationBailouts } from "./ModuleGraph" */
  45. /** @import ModuleProfile from "./ModuleProfile" */
  46. /** @import NormalModuleFactory from "./NormalModuleFactory" */
  47. /** @import RequestShortener from "./RequestShortener" */
  48. /** @import { ResolverWithOptions } from "./ResolverFactory" */
  49. /** @import RuntimeTemplate from "./RuntimeTemplate" */
  50. /**
  51. * Defines the init fragment type used by this module.
  52. * @template T
  53. * @typedef {import("./InitFragment")<T>} InitFragment
  54. */
  55. /** @import WebpackError from "./errors/WebpackError" */
  56. /**
  57. * @import {
  58. * ObjectDeserializerContext,
  59. * ObjectSerializerContext
  60. * } from "./serialization/ObjectMiddleware"
  61. */
  62. /** @import Hash from "./util/Hash" */
  63. /** @import { InputFileSystem } from "./util/fs" */
  64. /** @import { AssociatedObjectForCache } from "./util/identifier" */
  65. /** @import { RuntimeSpec } from "./util/runtime" */
  66. /**
  67. * @template T
  68. * @typedef {import("./util/SortableSet")<T>} SortableSet
  69. */
  70. /** @typedef {"namespace" | "default-only" | "default-with-named" | "dynamic"} ExportsType */
  71. /**
  72. * Defines the shared type used by this module.
  73. * @template T
  74. * @typedef {import("./util/LazySet")<T>} LazySet<T>
  75. */
  76. /**
  77. * Defines the source context type used by this module.
  78. * @typedef {object} SourceContext
  79. * @property {DependencyTemplates} dependencyTemplates the dependency templates
  80. * @property {RuntimeTemplate} runtimeTemplate the runtime template
  81. * @property {ModuleGraph} moduleGraph the module graph
  82. * @property {ChunkGraph} chunkGraph the chunk graph
  83. * @property {RuntimeSpec} runtime the runtimes code should be generated for
  84. * @property {string=} type the type of source that should be generated
  85. */
  86. /** @typedef {AllTypes} KnownSourceType */
  87. /** @typedef {KnownSourceType | string} SourceType */
  88. /** @typedef {ReadonlySet<SourceType>} SourceTypes */
  89. /** @typedef {ReadonlySet<typeof JAVASCRIPT_TYPE | string>} BasicSourceTypes */
  90. // TODO webpack 6: compilation will be required in CodeGenerationContext
  91. /**
  92. * Defines the code generation context type used by this module.
  93. * @typedef {object} CodeGenerationContext
  94. * @property {DependencyTemplates} dependencyTemplates the dependency templates
  95. * @property {RuntimeTemplate} runtimeTemplate the runtime template
  96. * @property {ModuleGraph} moduleGraph the module graph
  97. * @property {ChunkGraph} chunkGraph the chunk graph
  98. * @property {RuntimeSpec} runtime the runtimes code should be generated for
  99. * @property {RuntimeSpec[]} runtimes all runtimes code should be generated for
  100. * @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
  101. * @property {CodeGenerationResults | undefined} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
  102. * @property {Compilation=} compilation the compilation
  103. * @property {SourceTypes=} sourceTypes source types
  104. */
  105. /**
  106. * Defines the concatenation bailout reason context type used by this module.
  107. * @typedef {object} ConcatenationBailoutReasonContext
  108. * @property {ModuleGraph} moduleGraph the module graph
  109. * @property {ChunkGraph} chunkGraph the chunk graph
  110. * @property {boolean=} concatenateCommonJsModules whether eligible CommonJS modules may be concatenated
  111. */
  112. /** @typedef {Set<string>} RuntimeRequirements */
  113. /** @typedef {ReadonlySet<string>} ReadOnlyRuntimeRequirements */
  114. /**
  115. * Defines the all code generation schemas type used by this module.
  116. * @typedef {object} AllCodeGenerationSchemas
  117. * @property {Set<string>} topLevelDeclarations top level declarations for javascript modules
  118. * @property {Set<string>} freeNames free identifier names in the rendered source for javascript modules
  119. * @property {InitFragment<EXPECTED_ANY>[]} chunkInitFragments chunk init fragments for javascript modules
  120. * @property {{ javascript?: string, ["asset-url"]?: string }} url url for asset modules
  121. * @property {string} filename a filename for asset modules
  122. * @property {AssetInfo} assetInfo an asset info for asset modules
  123. * @property {string} fullContentHash a full content hash for asset modules
  124. * @property {[{ shareScope: string, initStage: number, init: string }]} share-init share-init for modules federation
  125. */
  126. /**
  127. * Defines the code gen value type used by this module.
  128. * @template {string} K
  129. * @typedef {K extends (keyof AllCodeGenerationSchemas) ? AllCodeGenerationSchemas[K] : EXPECTED_ANY} CodeGenValue
  130. */
  131. /**
  132. * Defines the code gen map overloads type used by this module.
  133. * @typedef {object} CodeGenMapOverloads
  134. * @property {<K extends string>(key: K) => CodeGenValue<K> | undefined} get
  135. * @property {<K extends string>(key: K, value: CodeGenValue<K>) => CodeGenerationResultData} set
  136. * @property {<K extends string>(key: K) => boolean} has
  137. * @property {<K extends string>(key: K) => boolean} delete
  138. */
  139. /**
  140. * Defines the code generation result data type used by this module.
  141. * @typedef {Omit<Map<string, EXPECTED_ANY>, "get" | "set" | "has" | "delete"> & CodeGenMapOverloads} CodeGenerationResultData
  142. */
  143. /** @typedef {Map<SourceType, Source>} Sources */
  144. /**
  145. * Defines the code generation result type used by this module.
  146. * @typedef {object} CodeGenerationResult
  147. * @property {Sources} sources the resulting sources for all source types
  148. * @property {CodeGenerationResultData=} data the resulting data for all source types
  149. * @property {ReadOnlyRuntimeRequirements | null} runtimeRequirements the runtime requirements
  150. * @property {string=} hash a hash of the code generation result (will be automatically calculated from sources and runtimeRequirements if not provided)
  151. */
  152. /**
  153. * Defines the lib ident options type used by this module.
  154. * @typedef {object} LibIdentOptions
  155. * @property {string} context absolute context path to which lib ident is relative to
  156. * @property {AssociatedObjectForCache=} associatedObjectForCache object for caching
  157. */
  158. /**
  159. * Defines the build meta properties common to all module types.
  160. * Module type specific properties live in the `Known*BuildMeta` typedef of the dedicated module class.
  161. * @typedef {object} KnownBuildMeta
  162. * @property {("default" | "namespace" | "flagged" | "dynamic")=} exportsType
  163. * @property {(false | "redirect" | "redirect-warn")=} defaultObject
  164. * @property {boolean=} async
  165. * @property {boolean=} sideEffectFree
  166. * @property {Map<string, Record<string, string>>=} exportsFinalNameByRuntime using in ModuleLibraryPlugin
  167. * @property {Map<string, string>=} exportsSourceByRuntime using in ModuleLibraryPlugin
  168. * @property {Map<string, string>=} exportsBindingSourceByRuntime export definitions `ModuleLibraryPlugin` took over, re-emitted only where the module is wrapped
  169. */
  170. /**
  171. * Defines the build info properties common to all module types.
  172. * Module type specific properties live in the `Known*BuildInfo` typedef of the dedicated module class.
  173. * @typedef {object} KnownBuildInfo
  174. * @property {boolean=} cacheable
  175. * @property {string[]=} notCacheableReasons reasons why the module is not cacheable (e.g. paths of loaders that marked it)
  176. * @property {boolean=} strict
  177. * @property {string=} moduleArgument
  178. * @property {string=} exportsArgument
  179. * @property {Record<string, Source>=} assets assets added by loaders or plugins
  180. * @property {Map<string, AssetInfo | undefined>=} assetsInfo
  181. * @property {Set<string>=} topLevelDeclarations top level declaration names
  182. * @property {boolean=} isCircular true when the module is part of a circular dependency chain
  183. * @property {boolean=} usesEval true when the module calls `eval` directly
  184. * @property {number=} ineffectivePureAnnotations how many `#__PURE__` annotations sit where the parser does not read them
  185. * @property {number=} topLevelThis how many times the module reads `this` at its top level
  186. * @property {boolean=} usesTopLevelAwaitForOf module uses top-level `for await…of` or `await using`, which can't be lowered to a generator
  187. */
  188. /** @typedef {string | Set<string>} ValueCacheVersion */
  189. /** @typedef {Map<string, ValueCacheVersion>} ValueCacheVersions */
  190. /**
  191. * Defines the need build context type used by this module.
  192. * @typedef {object} NeedBuildContext
  193. * @property {Compilation} compilation
  194. * @property {FileSystemInfo} fileSystemInfo
  195. * @property {ValueCacheVersions} valueCacheVersions
  196. */
  197. /** @typedef {(err?: WebpackError | null, needBuild?: boolean) => void} NeedBuildCallback */
  198. /** @typedef {(err?: WebpackError) => void} BuildCallback */
  199. /** @typedef {KnownBuildMeta & Record<string, EXPECTED_ANY>} BuildMeta */
  200. /** @typedef {KnownBuildInfo & Record<string, EXPECTED_ANY>} BuildInfo */
  201. /**
  202. * Defines the factory meta type used by this module.
  203. * @typedef {object} FactoryMeta
  204. * @property {boolean=} sideEffectFree
  205. */
  206. const EMPTY_RESOLVE_OPTIONS = {};
  207. let debugId = 1000;
  208. /** @type {SourceTypes} */
  209. const DEFAULT_TYPES_UNKNOWN = new Set([UNKNOWN_TYPE]);
  210. const deprecatedNeedRebuild = util.deprecate(
  211. /**
  212. * Handles the callback logic for this hook.
  213. * @param {Module} module the module
  214. * @param {NeedBuildContext} context context info
  215. * @returns {boolean} true, when rebuild is needed
  216. */
  217. (module, context) =>
  218. module.needRebuild(
  219. context.fileSystemInfo.getDeprecatedFileTimestamps(),
  220. context.fileSystemInfo.getDeprecatedContextTimestamps()
  221. ),
  222. "Module.needRebuild is deprecated in favor of Module.needBuild",
  223. "DEP_WEBPACK_MODULE_NEED_REBUILD"
  224. );
  225. /** @typedef {string} LibIdent */
  226. /** @typedef {string} NameForCondition */
  227. /** @typedef {(requestShortener: RequestShortener) => string} OptimizationBailoutFunction */
  228. class Module extends DependenciesBlock {
  229. /**
  230. * Creates an instance of Module.
  231. * @param {ModuleTypes | ""} type the module type, when deserializing the type is not known and is an empty string
  232. * @param {(string | null)=} context an optional context
  233. * @param {(string | null)=} layer an optional layer in which the module is
  234. */
  235. constructor(type, context = null, layer = null) {
  236. super();
  237. /** @type {ModuleTypes} */
  238. this.type = type;
  239. /** @type {string | null} */
  240. this.context = context;
  241. /** @type {string | null} */
  242. this.layer = layer;
  243. /** @type {boolean} */
  244. this.needId = true;
  245. // Unique Id
  246. /** @type {number} */
  247. this.debugId = debugId++;
  248. // Info from Factory
  249. /** @type {ResolveOptions | undefined} */
  250. this.resolveOptions = EMPTY_RESOLVE_OPTIONS;
  251. /** @type {FactoryMeta | undefined} */
  252. this.factoryMeta = undefined;
  253. // TODO refactor this -> options object filled from Factory
  254. // TODO webpack 6: use an enum
  255. /** @type {boolean} */
  256. this.useSourceMap = false;
  257. /** @type {boolean} */
  258. this.useSimpleSourceMap = false;
  259. // Info from Build
  260. /** @type {Error[] | undefined} */
  261. this._warnings = undefined;
  262. /** @type {Error[] | undefined} */
  263. this._errors = undefined;
  264. // Subclasses with type specific build info/meta redeclare these with a narrowed type
  265. /** @type {BuildMeta | undefined} */
  266. this.buildMeta = undefined;
  267. /** @type {BuildInfo | undefined} */
  268. this.buildInfo = undefined;
  269. /** @type {Dependency[] | undefined} */
  270. this.presentationalDependencies = undefined;
  271. /** @type {Dependency[] | undefined} */
  272. this.codeGenerationDependencies = undefined;
  273. }
  274. // TODO remove in webpack 6
  275. // BACKWARD-COMPAT START
  276. /**
  277. * Returns the module id assigned by the chunk graph.
  278. * @deprecated
  279. * @returns {ModuleId | null} module id
  280. */
  281. get id() {
  282. return ChunkGraph.getChunkGraphForModule(
  283. this,
  284. "Module.id",
  285. "DEP_WEBPACK_MODULE_ID"
  286. ).getModuleId(this);
  287. }
  288. /**
  289. * Updates the module id using the provided value.
  290. * @deprecated
  291. * @param {ModuleId} value value
  292. */
  293. set id(value) {
  294. if (value === "") {
  295. this.needId = false;
  296. return;
  297. }
  298. ChunkGraph.getChunkGraphForModule(
  299. this,
  300. "Module.id",
  301. "DEP_WEBPACK_MODULE_ID"
  302. ).setModuleId(this, value);
  303. }
  304. /**
  305. * Returns the hash of the module.
  306. * @deprecated
  307. * @returns {string} the hash of the module
  308. */
  309. get hash() {
  310. return ChunkGraph.getChunkGraphForModule(
  311. this,
  312. "Module.hash",
  313. "DEP_WEBPACK_MODULE_HASH"
  314. ).getModuleHash(this, undefined);
  315. }
  316. /**
  317. * Returns the rendered hash of the module.
  318. * @deprecated
  319. * @returns {string} the shortened hash of the module
  320. */
  321. get renderedHash() {
  322. return ChunkGraph.getChunkGraphForModule(
  323. this,
  324. "Module.renderedHash",
  325. "DEP_WEBPACK_MODULE_RENDERED_HASH"
  326. ).getRenderedModuleHash(this, undefined);
  327. }
  328. /**
  329. * @deprecated
  330. * @returns {ModuleProfile | undefined} module profile
  331. */
  332. get profile() {
  333. return ModuleGraph.getModuleGraphForModule(
  334. this,
  335. "Module.profile",
  336. "DEP_WEBPACK_MODULE_PROFILE"
  337. ).getProfile(this);
  338. }
  339. /**
  340. * @deprecated
  341. * @param {ModuleProfile | undefined} value module profile
  342. */
  343. set profile(value) {
  344. ModuleGraph.getModuleGraphForModule(
  345. this,
  346. "Module.profile",
  347. "DEP_WEBPACK_MODULE_PROFILE"
  348. ).setProfile(this, value);
  349. }
  350. /**
  351. * Returns the pre-order index.
  352. * @deprecated
  353. * @returns {number | null} the pre order index
  354. */
  355. get index() {
  356. return ModuleGraph.getModuleGraphForModule(
  357. this,
  358. "Module.index",
  359. "DEP_WEBPACK_MODULE_INDEX"
  360. ).getPreOrderIndex(this);
  361. }
  362. /**
  363. * Updates the pre-order index using the provided value.
  364. * @deprecated
  365. * @param {number} value the pre order index
  366. */
  367. set index(value) {
  368. ModuleGraph.getModuleGraphForModule(
  369. this,
  370. "Module.index",
  371. "DEP_WEBPACK_MODULE_INDEX"
  372. ).setPreOrderIndex(this, value);
  373. }
  374. /**
  375. * Returns the post-order index.
  376. * @deprecated
  377. * @returns {number | null} the post order index
  378. */
  379. get index2() {
  380. return ModuleGraph.getModuleGraphForModule(
  381. this,
  382. "Module.index2",
  383. "DEP_WEBPACK_MODULE_INDEX2"
  384. ).getPostOrderIndex(this);
  385. }
  386. /**
  387. * Updates the post-order index using the provided value.
  388. * @deprecated
  389. * @param {number} value the post order index
  390. */
  391. set index2(value) {
  392. ModuleGraph.getModuleGraphForModule(
  393. this,
  394. "Module.index2",
  395. "DEP_WEBPACK_MODULE_INDEX2"
  396. ).setPostOrderIndex(this, value);
  397. }
  398. /**
  399. * Returns the depth.
  400. * @deprecated
  401. * @returns {number | null} the depth
  402. */
  403. get depth() {
  404. return ModuleGraph.getModuleGraphForModule(
  405. this,
  406. "Module.depth",
  407. "DEP_WEBPACK_MODULE_DEPTH"
  408. ).getDepth(this);
  409. }
  410. /**
  411. * Updates the depth using the provided value.
  412. * @deprecated
  413. * @param {number} value the depth
  414. */
  415. set depth(value) {
  416. ModuleGraph.getModuleGraphForModule(
  417. this,
  418. "Module.depth",
  419. "DEP_WEBPACK_MODULE_DEPTH"
  420. ).setDepth(this, value);
  421. }
  422. /**
  423. * Returns the issuer.
  424. * @deprecated
  425. * @returns {Module | null | undefined} issuer
  426. */
  427. get issuer() {
  428. return ModuleGraph.getModuleGraphForModule(
  429. this,
  430. "Module.issuer",
  431. "DEP_WEBPACK_MODULE_ISSUER"
  432. ).getIssuer(this);
  433. }
  434. /**
  435. * Updates the issuer using the provided value.
  436. * @deprecated
  437. * @param {Module | null} value issuer
  438. */
  439. set issuer(value) {
  440. ModuleGraph.getModuleGraphForModule(
  441. this,
  442. "Module.issuer",
  443. "DEP_WEBPACK_MODULE_ISSUER"
  444. ).setIssuer(this, value);
  445. }
  446. /**
  447. * @deprecated
  448. * @returns {boolean | SortableSet<string> | null} used exports
  449. */
  450. get usedExports() {
  451. return ModuleGraph.getModuleGraphForModule(
  452. this,
  453. "Module.usedExports",
  454. "DEP_WEBPACK_MODULE_USED_EXPORTS"
  455. ).getUsedExports(this, undefined);
  456. }
  457. /**
  458. * Gets optimization bailout.
  459. * @deprecated
  460. * @returns {OptimizationBailouts} list
  461. */
  462. get optimizationBailout() {
  463. return ModuleGraph.getModuleGraphForModule(
  464. this,
  465. "Module.optimizationBailout",
  466. "DEP_WEBPACK_MODULE_OPTIMIZATION_BAILOUT"
  467. ).getOptimizationBailout(this);
  468. }
  469. /**
  470. * @deprecated
  471. * @returns {boolean} true when optional, otherwise false
  472. */
  473. get optional() {
  474. return this.isOptional(
  475. ModuleGraph.getModuleGraphForModule(
  476. this,
  477. "Module.optional",
  478. "DEP_WEBPACK_MODULE_OPTIONAL"
  479. )
  480. );
  481. }
  482. /**
  483. * Adds the provided chunk to the module.
  484. * @deprecated
  485. * @param {Chunk} chunk the chunk
  486. * @returns {boolean} true, when the module was added
  487. */
  488. addChunk(chunk) {
  489. const chunkGraph = ChunkGraph.getChunkGraphForModule(
  490. this,
  491. "Module.addChunk",
  492. "DEP_WEBPACK_MODULE_ADD_CHUNK"
  493. );
  494. if (chunkGraph.isModuleInChunk(this, chunk)) return false;
  495. chunkGraph.connectChunkAndModule(chunk, this);
  496. return true;
  497. }
  498. /**
  499. * Removes the provided chunk from the module.
  500. * @deprecated
  501. * @param {Chunk} chunk the chunk
  502. * @returns {void}
  503. */
  504. removeChunk(chunk) {
  505. return ChunkGraph.getChunkGraphForModule(
  506. this,
  507. "Module.removeChunk",
  508. "DEP_WEBPACK_MODULE_REMOVE_CHUNK"
  509. ).disconnectChunkAndModule(chunk, this);
  510. }
  511. /**
  512. * Checks whether this module is in the provided chunk.
  513. * @deprecated
  514. * @param {Chunk} chunk the chunk
  515. * @returns {boolean} true, when the module is in the chunk
  516. */
  517. isInChunk(chunk) {
  518. return ChunkGraph.getChunkGraphForModule(
  519. this,
  520. "Module.isInChunk",
  521. "DEP_WEBPACK_MODULE_IS_IN_CHUNK"
  522. ).isModuleInChunk(this, chunk);
  523. }
  524. /**
  525. * @deprecated
  526. * @returns {boolean} true when is entry module, otherwise false
  527. */
  528. isEntryModule() {
  529. return ChunkGraph.getChunkGraphForModule(
  530. this,
  531. "Module.isEntryModule",
  532. "DEP_WEBPACK_MODULE_IS_ENTRY_MODULE"
  533. ).isEntryModule(this);
  534. }
  535. /**
  536. * @deprecated
  537. * @returns {Chunk[]} chunks
  538. */
  539. getChunks() {
  540. return ChunkGraph.getChunkGraphForModule(
  541. this,
  542. "Module.getChunks",
  543. "DEP_WEBPACK_MODULE_GET_CHUNKS"
  544. ).getModuleChunks(this);
  545. }
  546. /**
  547. * @deprecated
  548. * @returns {number} number of chunks
  549. */
  550. getNumberOfChunks() {
  551. return ChunkGraph.getChunkGraphForModule(
  552. this,
  553. "Module.getNumberOfChunks",
  554. "DEP_WEBPACK_MODULE_GET_NUMBER_OF_CHUNKS"
  555. ).getNumberOfModuleChunks(this);
  556. }
  557. /**
  558. * @deprecated
  559. * @returns {Iterable<Chunk>} chunks
  560. */
  561. get chunksIterable() {
  562. return ChunkGraph.getChunkGraphForModule(
  563. this,
  564. "Module.chunksIterable",
  565. "DEP_WEBPACK_MODULE_CHUNKS_ITERABLE"
  566. ).getOrderedModuleChunksIterable(this, compareChunksById);
  567. }
  568. /**
  569. * Checks whether this module provides the specified export.
  570. * @deprecated
  571. * @param {string} exportName a name of an export
  572. * @returns {boolean | null} true, if the export is provided why the module.
  573. * null, if it's unknown.
  574. * false, if it's not provided.
  575. */
  576. isProvided(exportName) {
  577. return ModuleGraph.getModuleGraphForModule(
  578. this,
  579. "Module.usedExports",
  580. "DEP_WEBPACK_MODULE_USED_EXPORTS"
  581. ).isExportProvided(this, exportName);
  582. }
  583. // BACKWARD-COMPAT END
  584. /**
  585. * Gets exports argument.
  586. * @returns {string} name of the exports argument
  587. */
  588. get exportsArgument() {
  589. return (this.buildInfo && this.buildInfo.exportsArgument) || "exports";
  590. }
  591. /**
  592. * Gets module argument.
  593. * @returns {string} name of the module argument
  594. */
  595. get moduleArgument() {
  596. return (this.buildInfo && this.buildInfo.moduleArgument) || "module";
  597. }
  598. /**
  599. * Returns export type.
  600. * @param {ModuleGraph} moduleGraph the module graph
  601. * @param {boolean | undefined} strict the importing module is strict
  602. * @returns {ExportsType} export type
  603. * "namespace": Exports is already a namespace object. namespace = exports.
  604. * "dynamic": Check at runtime if __esModule is set. When set: namespace = { ...exports, default: exports }. When not set: namespace = { default: exports }.
  605. * "default-only": Provide a namespace object with only default export. namespace = { default: exports }
  606. * "default-with-named": Provide a namespace object with named and default export. namespace = { ...exports, default: exports }
  607. */
  608. getExportsType(moduleGraph, strict) {
  609. switch (this.buildMeta && this.buildMeta.exportsType) {
  610. case "flagged":
  611. return strict ? "default-with-named" : "namespace";
  612. case "namespace":
  613. return "namespace";
  614. case "default":
  615. switch (/** @type {BuildMeta} */ (this.buildMeta).defaultObject) {
  616. case "redirect":
  617. return "default-with-named";
  618. case "redirect-warn":
  619. return strict ? "default-only" : "default-with-named";
  620. default:
  621. return "default-only";
  622. }
  623. case "dynamic": {
  624. if (strict) return "default-with-named";
  625. // Try to figure out value of __esModule by following reexports
  626. const handleDefault = () => {
  627. switch (/** @type {BuildMeta} */ (this.buildMeta).defaultObject) {
  628. case "redirect":
  629. case "redirect-warn":
  630. return "default-with-named";
  631. default:
  632. return "default-only";
  633. }
  634. };
  635. const exportInfo = moduleGraph.getReadOnlyExportInfo(
  636. this,
  637. "__esModule"
  638. );
  639. if (exportInfo.provided === false) {
  640. return handleDefault();
  641. }
  642. const target = exportInfo.getTarget(moduleGraph);
  643. if (
  644. !target ||
  645. !target.export ||
  646. target.export.length !== 1 ||
  647. target.export[0] !== "__esModule"
  648. ) {
  649. return "dynamic";
  650. }
  651. switch (
  652. target.module.buildMeta &&
  653. target.module.buildMeta.exportsType
  654. ) {
  655. case "flagged":
  656. case "namespace":
  657. return "namespace";
  658. case "default":
  659. return handleDefault();
  660. default:
  661. return "dynamic";
  662. }
  663. }
  664. default:
  665. return strict ? "default-with-named" : "dynamic";
  666. }
  667. }
  668. /**
  669. * Adds presentational dependency.
  670. * @param {Dependency} presentationalDependency dependency being tied to module.
  671. * This is a Dependency without edge in the module graph. It's only for presentation.
  672. * @returns {void}
  673. */
  674. addPresentationalDependency(presentationalDependency) {
  675. if (this.presentationalDependencies === undefined) {
  676. this.presentationalDependencies = [];
  677. }
  678. this.presentationalDependencies.push(presentationalDependency);
  679. }
  680. /**
  681. * Adds code generation dependency.
  682. * @param {Dependency} codeGenerationDependency dependency being tied to module.
  683. * This is a Dependency where the code generation result of the referenced module is needed during code generation.
  684. * The Dependency should also be added to normal dependencies via addDependency.
  685. * @returns {void}
  686. */
  687. addCodeGenerationDependency(codeGenerationDependency) {
  688. if (this.codeGenerationDependencies === undefined) {
  689. this.codeGenerationDependencies = [];
  690. }
  691. this.codeGenerationDependencies.push(codeGenerationDependency);
  692. }
  693. /**
  694. * Clear dependencies and blocks.
  695. * @returns {void}
  696. */
  697. clearDependenciesAndBlocks() {
  698. if (this.presentationalDependencies !== undefined) {
  699. this.presentationalDependencies.length = 0;
  700. }
  701. if (this.codeGenerationDependencies !== undefined) {
  702. this.codeGenerationDependencies.length = 0;
  703. }
  704. super.clearDependenciesAndBlocks();
  705. }
  706. /**
  707. * Adds the provided warning to the module.
  708. * @param {Error} warning the warning
  709. * @returns {void}
  710. */
  711. addWarning(warning) {
  712. if (this._warnings === undefined) {
  713. this._warnings = [];
  714. }
  715. this._warnings.push(warning);
  716. }
  717. /**
  718. * Returns list of warnings if any.
  719. * @returns {Error[] | undefined} list of warnings if any
  720. */
  721. getWarnings() {
  722. return this._warnings;
  723. }
  724. /**
  725. * Gets number of warnings.
  726. * @returns {number} number of warnings
  727. */
  728. getNumberOfWarnings() {
  729. return this._warnings !== undefined ? this._warnings.length : 0;
  730. }
  731. /**
  732. * Adds the provided error to the module.
  733. * @param {Error} error the error
  734. * @returns {void}
  735. */
  736. addError(error) {
  737. if (this._errors === undefined) {
  738. this._errors = [];
  739. }
  740. this._errors.push(error);
  741. }
  742. /**
  743. * Returns list of errors if any.
  744. * @returns {Error[] | undefined} list of errors if any
  745. */
  746. getErrors() {
  747. return this._errors;
  748. }
  749. /**
  750. * Gets number of errors.
  751. * @returns {number} number of errors
  752. */
  753. getNumberOfErrors() {
  754. return this._errors !== undefined ? this._errors.length : 0;
  755. }
  756. /**
  757. * removes all warnings and errors
  758. * @returns {void}
  759. */
  760. clearWarningsAndErrors() {
  761. if (this._warnings !== undefined) {
  762. this._warnings.length = 0;
  763. }
  764. if (this._errors !== undefined) {
  765. this._errors.length = 0;
  766. }
  767. }
  768. /**
  769. * Checks whether this module is optional.
  770. * @param {ModuleGraph} moduleGraph the module graph
  771. * @returns {boolean} true, if the module is optional
  772. */
  773. isOptional(moduleGraph) {
  774. let hasConnections = false;
  775. for (const r of moduleGraph.getIncomingConnections(this)) {
  776. if (
  777. !r.dependency ||
  778. !r.dependency.optional ||
  779. !r.isTargetActive(undefined)
  780. ) {
  781. return false;
  782. }
  783. hasConnections = true;
  784. }
  785. return hasConnections;
  786. }
  787. /**
  788. * Checks whether this module is accessible in chunk.
  789. * @param {ChunkGraph} chunkGraph the chunk graph
  790. * @param {Chunk} chunk a chunk
  791. * @param {Chunk=} ignoreChunk chunk to be ignored
  792. * @returns {boolean} true, if the module is accessible from "chunk" when ignoring "ignoreChunk"
  793. */
  794. isAccessibleInChunk(chunkGraph, chunk, ignoreChunk) {
  795. // Check if module is accessible in ALL chunk groups
  796. for (const chunkGroup of chunk.groupsIterable) {
  797. if (!this.isAccessibleInChunkGroup(chunkGraph, chunkGroup)) return false;
  798. }
  799. return true;
  800. }
  801. /**
  802. * Checks whether this module is accessible in chunk group.
  803. * @param {ChunkGraph} chunkGraph the chunk graph
  804. * @param {ChunkGroup} chunkGroup a chunk group
  805. * @param {Chunk=} ignoreChunk chunk to be ignored
  806. * @returns {boolean} true, if the module is accessible from "chunkGroup" when ignoring "ignoreChunk"
  807. */
  808. isAccessibleInChunkGroup(chunkGraph, chunkGroup, ignoreChunk) {
  809. const queue = new Set([chunkGroup]);
  810. // Check if module is accessible from all items of the queue
  811. queueFor: for (const cg of queue) {
  812. // 1. If module is in one of the chunks of the group we can continue checking the next items
  813. // because it's accessible.
  814. for (const chunk of cg.chunks) {
  815. if (chunk !== ignoreChunk && chunkGraph.isModuleInChunk(this, chunk)) {
  816. continue queueFor;
  817. }
  818. }
  819. // 2. If the chunk group is initial, we can break here because it's not accessible.
  820. if (chunkGroup.isInitial()) return false;
  821. // 3. Enqueue all parents because it must be accessible from ALL parents
  822. for (const parent of chunkGroup.parentsIterable) queue.add(parent);
  823. }
  824. // When we processed through the whole list and we didn't bailout, the module is accessible
  825. return true;
  826. }
  827. /**
  828. * Checks whether this module contains the chunk.
  829. * @param {Chunk} chunk a chunk
  830. * @param {ModuleGraph} moduleGraph the module graph
  831. * @param {ChunkGraph} chunkGraph the chunk graph
  832. * @returns {boolean} true, if the module has any reason why "chunk" should be included
  833. */
  834. hasReasonForChunk(chunk, moduleGraph, chunkGraph) {
  835. // check for each reason if we need the chunk
  836. for (const [
  837. fromModule,
  838. connections
  839. ] of moduleGraph.getIncomingConnectionsByOriginModule(this)) {
  840. if (!connections.some((c) => c.isTargetActive(chunk.runtime))) continue;
  841. for (const originChunk of chunkGraph.getModuleChunksIterable(
  842. /** @type {Module} */ (fromModule)
  843. )) {
  844. // return true if module this is not reachable from originChunk when ignoring chunk
  845. if (!this.isAccessibleInChunk(chunkGraph, originChunk, chunk)) {
  846. return true;
  847. }
  848. }
  849. }
  850. return false;
  851. }
  852. /**
  853. * Checks whether this module contains the module graph.
  854. * @param {ModuleGraph} moduleGraph the module graph
  855. * @param {RuntimeSpec} runtime the runtime
  856. * @returns {boolean} true if at least one other module depends on this module
  857. */
  858. hasReasons(moduleGraph, runtime) {
  859. for (const c of moduleGraph.getIncomingConnections(this)) {
  860. if (c.isTargetActive(runtime)) return true;
  861. }
  862. return false;
  863. }
  864. /**
  865. * Returns a string representation.
  866. * @returns {string} for debugging
  867. */
  868. toString() {
  869. return `Module[${this.debugId}: ${this.identifier()}]`;
  870. }
  871. /**
  872. * Checks whether the module needs to be rebuilt for the current build state.
  873. * @param {NeedBuildContext} context context info
  874. * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
  875. * @returns {void}
  876. */
  877. needBuild(context, callback) {
  878. callback(
  879. null,
  880. !this.buildMeta ||
  881. this.needRebuild === Module.prototype.needRebuild ||
  882. deprecatedNeedRebuild(this, context)
  883. );
  884. }
  885. /**
  886. * Checks whether it needs rebuild.
  887. * @deprecated Use needBuild instead
  888. * @param {Map<string, number | null>} fileTimestamps timestamps of files
  889. * @param {Map<string, number | null>} contextTimestamps timestamps of directories
  890. * @returns {boolean} true, if the module needs a rebuild
  891. */
  892. needRebuild(fileTimestamps, contextTimestamps) {
  893. return true;
  894. }
  895. /**
  896. * Updates the hash with the data contributed by this instance.
  897. * @param {Hash} hash the hash used to track dependencies
  898. * @param {UpdateHashContext} context context
  899. * @returns {void}
  900. */
  901. updateHash(
  902. hash,
  903. context = {
  904. chunkGraph: ChunkGraph.getChunkGraphForModule(
  905. this,
  906. "Module.updateHash",
  907. "DEP_WEBPACK_MODULE_UPDATE_HASH"
  908. ),
  909. runtime: undefined
  910. }
  911. ) {
  912. const { chunkGraph, runtime } = context;
  913. hash.update(chunkGraph.getModuleGraphHash(this, runtime));
  914. if (this.presentationalDependencies !== undefined) {
  915. for (const dep of this.presentationalDependencies) {
  916. dep.updateHash(hash, context);
  917. }
  918. }
  919. super.updateHash(hash, context);
  920. }
  921. /**
  922. * Invalidates the cached state associated with this value.
  923. * @returns {void}
  924. */
  925. invalidateBuild() {
  926. // should be overridden to support this feature
  927. }
  928. /* istanbul ignore next */
  929. /**
  930. * Returns the unique identifier used to reference this module.
  931. * @abstract
  932. * @returns {string} a unique identifier of the module
  933. */
  934. identifier() {
  935. const AbstractMethodError = require("./errors/AbstractMethodError");
  936. throw new AbstractMethodError();
  937. }
  938. /* istanbul ignore next */
  939. /**
  940. * Returns a human-readable identifier for this module.
  941. * @abstract
  942. * @param {RequestShortener} requestShortener the request shortener
  943. * @returns {string} a user readable identifier of the module
  944. */
  945. readableIdentifier(requestShortener) {
  946. const AbstractMethodError = require("./errors/AbstractMethodError");
  947. throw new AbstractMethodError();
  948. }
  949. /* istanbul ignore next */
  950. /**
  951. * Builds the module using the provided compilation context.
  952. * @abstract
  953. * @param {WebpackOptions} options webpack options
  954. * @param {Compilation} compilation the compilation
  955. * @param {ResolverWithOptions} resolver the resolver
  956. * @param {InputFileSystem} fs the file system
  957. * @param {BuildCallback} callback callback function
  958. * @returns {void}
  959. */
  960. build(options, compilation, resolver, fs, callback) {
  961. const AbstractMethodError = require("./errors/AbstractMethodError");
  962. throw new AbstractMethodError();
  963. }
  964. /**
  965. * Returns the source types this module can generate.
  966. * @abstract
  967. * @returns {SourceTypes} types available (do not mutate)
  968. */
  969. getSourceTypes() {
  970. // Better override this method to return the correct types
  971. if (this.source === Module.prototype.source) {
  972. return DEFAULT_TYPES_UNKNOWN;
  973. }
  974. return JAVASCRIPT_TYPES;
  975. }
  976. /**
  977. * Freshly recomputed source types when they depend on incoming connections, for chunk-graph cache invalidation; undefined otherwise. #20800
  978. * @returns {SourceTypes | undefined} source types or undefined
  979. */
  980. getReferencedSourceTypes() {
  981. return undefined;
  982. }
  983. /**
  984. * Basic source types are high-level categories like javascript, css, webassembly, etc.
  985. * We only have built-in knowledge about the javascript basic type here; other basic types may be
  986. * added or changed over time by generators and do not need to be handled or detected here.
  987. *
  988. * Some modules, e.g. RemoteModule, may return non-basic source types like "remote" and "share-init"
  989. * from getSourceTypes(), but their generated output is still JavaScript, i.e. their basic type is JS.
  990. * @returns {BasicSourceTypes} types available (do not mutate)
  991. */
  992. getSourceBasicTypes() {
  993. return this.getSourceTypes();
  994. }
  995. /**
  996. * Returns generated source.
  997. * @abstract
  998. * @deprecated Use codeGeneration() instead
  999. * @param {DependencyTemplates} dependencyTemplates the dependency templates
  1000. * @param {RuntimeTemplate} runtimeTemplate the runtime template
  1001. * @param {SourceType=} type the type of source that should be generated
  1002. * @returns {Source} generated source
  1003. */
  1004. source(dependencyTemplates, runtimeTemplate, type = JAVASCRIPT_TYPE) {
  1005. if (this.codeGeneration === Module.prototype.codeGeneration) {
  1006. const AbstractMethodError = require("./errors/AbstractMethodError");
  1007. throw new AbstractMethodError();
  1008. }
  1009. const chunkGraph = ChunkGraph.getChunkGraphForModule(
  1010. this,
  1011. "Module.source() is deprecated. Use Compilation.codeGenerationResults.getSource(module, runtime, type) instead",
  1012. "DEP_WEBPACK_MODULE_SOURCE"
  1013. );
  1014. /** @type {CodeGenerationContext} */
  1015. const codeGenContext = {
  1016. dependencyTemplates,
  1017. runtimeTemplate,
  1018. moduleGraph: chunkGraph.moduleGraph,
  1019. chunkGraph,
  1020. runtime: undefined,
  1021. runtimes: [],
  1022. codeGenerationResults: undefined
  1023. };
  1024. const sources = this.codeGeneration(codeGenContext).sources;
  1025. return /** @type {Source} */ (
  1026. type
  1027. ? sources.get(type)
  1028. : sources.get(/** @type {SourceType} */ (first(this.getSourceTypes())))
  1029. );
  1030. }
  1031. /* istanbul ignore next */
  1032. /**
  1033. * Returns the estimated size for the requested source type.
  1034. * @abstract
  1035. * @param {string=} type the source type for which the size should be estimated
  1036. * @returns {number} the estimated size of the module (must be non-zero)
  1037. */
  1038. size(type) {
  1039. const AbstractMethodError = require("./errors/AbstractMethodError");
  1040. throw new AbstractMethodError();
  1041. }
  1042. /**
  1043. * Gets the library identifier.
  1044. * @param {LibIdentOptions} options options
  1045. * @returns {LibIdent | null} an identifier for library inclusion
  1046. */
  1047. libIdent(options) {
  1048. return null;
  1049. }
  1050. /**
  1051. * Returns the path used when matching this module against rule conditions.
  1052. * @returns {NameForCondition | null} absolute path which should be used for condition matching (usually the resource path)
  1053. */
  1054. nameForCondition() {
  1055. return null;
  1056. }
  1057. /**
  1058. * Returns the reason this module cannot be concatenated, when one exists.
  1059. * @param {ConcatenationBailoutReasonContext} context context
  1060. * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
  1061. */
  1062. getConcatenationBailoutReason(context) {
  1063. return `Module Concatenation is not implemented for ${this.constructor.name}`;
  1064. }
  1065. /**
  1066. * Gets side effects connection state.
  1067. * @param {ModuleGraph} moduleGraph the module graph
  1068. * @returns {ConnectionState} how this module should be connected to referencing modules when consumed for side-effects only
  1069. */
  1070. getSideEffectsConnectionState(moduleGraph) {
  1071. return true;
  1072. }
  1073. /**
  1074. * Generates code and runtime requirements for this module.
  1075. * @param {CodeGenerationContext} context context for code generation
  1076. * @returns {CodeGenerationResult} result
  1077. */
  1078. codeGeneration(context) {
  1079. // Best override this method
  1080. /** @type {Sources} */
  1081. const sources = new Map();
  1082. for (const type of this.getSourceTypes()) {
  1083. if (type !== UNKNOWN_TYPE) {
  1084. sources.set(
  1085. type,
  1086. this.source(
  1087. context.dependencyTemplates,
  1088. context.runtimeTemplate,
  1089. type
  1090. )
  1091. );
  1092. }
  1093. }
  1094. return {
  1095. sources,
  1096. runtimeRequirements: new Set([
  1097. RuntimeGlobals.module,
  1098. RuntimeGlobals.exports,
  1099. RuntimeGlobals.require
  1100. ])
  1101. };
  1102. }
  1103. /**
  1104. * Returns true if the module can be placed in the chunk.
  1105. * @param {Chunk} chunk the chunk which condition should be checked
  1106. * @param {Compilation} compilation the compilation
  1107. * @returns {boolean} true if the module can be placed in the chunk
  1108. */
  1109. chunkCondition(chunk, compilation) {
  1110. return true;
  1111. }
  1112. hasChunkCondition() {
  1113. return this.chunkCondition !== Module.prototype.chunkCondition;
  1114. }
  1115. /**
  1116. * Assuming this module is in the cache. Update the (cached) module with
  1117. * the fresh module from the factory. Usually updates internal references
  1118. * and properties.
  1119. * @param {Module} module fresh module
  1120. * @returns {void}
  1121. */
  1122. updateCacheModule(module) {
  1123. this.type = module.type;
  1124. this.layer = module.layer;
  1125. this.context = module.context;
  1126. this.factoryMeta = module.factoryMeta;
  1127. this.resolveOptions = module.resolveOptions;
  1128. }
  1129. /**
  1130. * Module should be unsafe cached. Get data that's needed for that.
  1131. * This data will be passed to restoreFromUnsafeCache later.
  1132. * @returns {UnsafeCacheData} cached data
  1133. */
  1134. getUnsafeCacheData() {
  1135. return {
  1136. factoryMeta: this.factoryMeta,
  1137. resolveOptions: this.resolveOptions
  1138. };
  1139. }
  1140. /**
  1141. * restore unsafe cache data
  1142. * @param {UnsafeCacheData} unsafeCacheData data from getUnsafeCacheData
  1143. * @param {NormalModuleFactory} normalModuleFactory the normal module factory handling the unsafe caching
  1144. */
  1145. _restoreFromUnsafeCache(unsafeCacheData, normalModuleFactory) {
  1146. this.factoryMeta = unsafeCacheData.factoryMeta;
  1147. this.resolveOptions = unsafeCacheData.resolveOptions;
  1148. }
  1149. /**
  1150. * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
  1151. */
  1152. cleanupForCache() {
  1153. this.factoryMeta = undefined;
  1154. this.resolveOptions = undefined;
  1155. }
  1156. /**
  1157. * Re-acquire factory-derived helpers (parser/generator) for an incremental
  1158. * rebuild so this compilation's plugin hooks apply.
  1159. * @param {NormalModuleFactory} normalModuleFactory the normal module factory
  1160. * @returns {void}
  1161. */
  1162. _restoreParserAndGenerator(normalModuleFactory) {}
  1163. /**
  1164. * Releases the cached parser between incremental rebuilds so it doesn't keep
  1165. * the previous compilation alive.
  1166. * @returns {void}
  1167. */
  1168. _cleanupParserForCache() {}
  1169. /**
  1170. * Gets the original source.
  1171. * @returns {Source | null} the original source for the module before webpack transformation
  1172. */
  1173. originalSource() {
  1174. return null;
  1175. }
  1176. /**
  1177. * Adds the provided file dependencies to the module.
  1178. * @param {FileSystemDependencies} fileDependencies set where file dependencies are added to
  1179. * @param {FileSystemDependencies} contextDependencies set where context dependencies are added to
  1180. * @param {FileSystemDependencies} missingDependencies set where missing dependencies are added to
  1181. * @param {FileSystemDependencies} buildDependencies set where build dependencies are added to
  1182. */
  1183. addCacheDependencies(
  1184. fileDependencies,
  1185. contextDependencies,
  1186. missingDependencies,
  1187. buildDependencies
  1188. ) {}
  1189. /**
  1190. * Serializes this instance into the provided serializer context.
  1191. * @param {ObjectSerializerContext} context context
  1192. */
  1193. serialize(context) {
  1194. const { write } = context;
  1195. write(this.type);
  1196. write(this.layer);
  1197. write(this.context);
  1198. write(this.resolveOptions);
  1199. write(this.factoryMeta);
  1200. write(this.useSourceMap);
  1201. write(this.useSimpleSourceMap);
  1202. write(
  1203. this._warnings !== undefined && this._warnings.length === 0
  1204. ? undefined
  1205. : this._warnings
  1206. );
  1207. write(
  1208. this._errors !== undefined && this._errors.length === 0
  1209. ? undefined
  1210. : this._errors
  1211. );
  1212. write(this.buildMeta);
  1213. write(this.buildInfo);
  1214. write(this.presentationalDependencies);
  1215. write(this.codeGenerationDependencies);
  1216. super.serialize(context);
  1217. }
  1218. /**
  1219. * Restores this instance from the provided deserializer context.
  1220. * @param {ObjectDeserializerContext} context context
  1221. */
  1222. deserialize(context) {
  1223. const { read } = context;
  1224. this.type = read();
  1225. this.layer = read();
  1226. this.context = read();
  1227. this.resolveOptions = read();
  1228. this.factoryMeta = read();
  1229. this.useSourceMap = read();
  1230. this.useSimpleSourceMap = read();
  1231. this._warnings = read();
  1232. this._errors = read();
  1233. this.buildMeta = read();
  1234. this.buildInfo = read();
  1235. this.presentationalDependencies = read();
  1236. this.codeGenerationDependencies = read();
  1237. super.deserialize(context);
  1238. }
  1239. // TODO remove in webpack 6
  1240. /**
  1241. * Gets source basic types.
  1242. * @deprecated In webpack 6, call getSourceBasicTypes() directly on the module instance instead of using this static method.
  1243. * @param {Module} module the module
  1244. * @returns {ReturnType<Module["getSourceBasicTypes"]>} the source types of the module
  1245. */
  1246. static getSourceBasicTypes(module) {
  1247. if (!(module instanceof Module)) {
  1248. // https://github.com/webpack/webpack/issues/20597
  1249. // fallback to javascript
  1250. return JAVASCRIPT_TYPES;
  1251. }
  1252. return module.getSourceBasicTypes();
  1253. }
  1254. }
  1255. makeSerializable(Module, "webpack/lib/Module");
  1256. // TODO remove in webpack 6
  1257. Object.defineProperty(Module.prototype, "hasEqualsChunks", {
  1258. /**
  1259. * Gets has equals chunks.
  1260. * @deprecated
  1261. * @returns {EXPECTED_ANY} throw an error
  1262. */
  1263. get() {
  1264. throw new Error(
  1265. "Module.hasEqualsChunks was renamed (use hasEqualChunks instead)"
  1266. );
  1267. }
  1268. });
  1269. // TODO remove in webpack 6
  1270. Object.defineProperty(Module.prototype, "isUsed", {
  1271. /**
  1272. * Returns throw an error.
  1273. * @deprecated
  1274. * @returns {EXPECTED_ANY} throw an error
  1275. */
  1276. get() {
  1277. throw new Error(
  1278. "Module.isUsed was renamed (use getUsedName, isExportUsed or isModuleUsed instead)"
  1279. );
  1280. }
  1281. });
  1282. // TODO remove in webpack 6
  1283. Object.defineProperty(Module.prototype, "errors", {
  1284. /**
  1285. * Returns errors.
  1286. * @deprecated
  1287. * @returns {Error[]} errors
  1288. */
  1289. get: util.deprecate(
  1290. /**
  1291. * Returns errors.
  1292. * @this {Module}
  1293. * @returns {Error[]} errors
  1294. */
  1295. function errors() {
  1296. if (this._errors === undefined) {
  1297. this._errors = [];
  1298. }
  1299. return this._errors;
  1300. },
  1301. "Module.errors was removed (use getErrors instead)",
  1302. "DEP_WEBPACK_MODULE_ERRORS"
  1303. )
  1304. });
  1305. // TODO remove in webpack 6
  1306. Object.defineProperty(Module.prototype, "warnings", {
  1307. /**
  1308. * Returns warnings.
  1309. * @deprecated
  1310. * @returns {Error[]} warnings
  1311. */
  1312. get: util.deprecate(
  1313. /**
  1314. * Returns warnings.
  1315. * @this {Module}
  1316. * @returns {Error[]} warnings
  1317. */
  1318. function warnings() {
  1319. if (this._warnings === undefined) {
  1320. this._warnings = [];
  1321. }
  1322. return this._warnings;
  1323. },
  1324. "Module.warnings was removed (use getWarnings instead)",
  1325. "DEP_WEBPACK_MODULE_WARNINGS"
  1326. )
  1327. });
  1328. // TODO remove in webpack 6
  1329. Object.defineProperty(Module.prototype, "used", {
  1330. /**
  1331. * Returns throw an error.
  1332. * @deprecated
  1333. * @returns {EXPECTED_ANY} throw an error
  1334. */
  1335. get() {
  1336. throw new Error(
  1337. "Module.used was refactored (use ModuleGraph.getUsedExports instead)"
  1338. );
  1339. },
  1340. /**
  1341. * Updates used using the provided value.
  1342. * @param {EXPECTED_ANY} value value
  1343. */
  1344. set(value) {
  1345. throw new Error(
  1346. "Module.used was refactored (use ModuleGraph.setUsedExports instead)"
  1347. );
  1348. }
  1349. });
  1350. module.exports = Module;