Module.js 32 KB

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