Module.js 30 KB

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