Module.js 33 KB

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