10
0

Module.js 33 KB

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