Module.js 31 KB

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