提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1264 lines
36 KiB

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