Module.js 43 KB

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