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.

Module.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 DependenciesBlock = require("./DependenciesBlock");
  8. const ModuleReason = require("./ModuleReason");
  9. const SortableSet = require("./util/SortableSet");
  10. const Template = require("./Template");
  11. /** @typedef {import("./Chunk")} Chunk */
  12. /** @typedef {import("./RequestShortener")} RequestShortener */
  13. /** @typedef {import("./WebpackError")} WebpackError */
  14. /** @typedef {import("./util/createHash").Hash} Hash */
  15. const EMPTY_RESOLVE_OPTIONS = {};
  16. let debugId = 1000;
  17. const sortById = (a, b) => {
  18. return a.id - b.id;
  19. };
  20. const sortByDebugId = (a, b) => {
  21. return a.debugId - b.debugId;
  22. };
  23. /** @typedef {(requestShortener: RequestShortener) => string} OptimizationBailoutFunction */
  24. class Module extends DependenciesBlock {
  25. constructor(type, context = null) {
  26. super();
  27. /** @type {string} */
  28. this.type = type;
  29. /** @type {string} */
  30. this.context = context;
  31. // Unique Id
  32. /** @type {number} */
  33. this.debugId = debugId++;
  34. // Hash
  35. /** @type {string} */
  36. this.hash = undefined;
  37. /** @type {string} */
  38. this.renderedHash = undefined;
  39. // Info from Factory
  40. /** @type {TODO} */
  41. this.resolveOptions = EMPTY_RESOLVE_OPTIONS;
  42. /** @type {object} */
  43. this.factoryMeta = {};
  44. // Info from Build
  45. /** @type {WebpackError[]} */
  46. this.warnings = [];
  47. /** @type {WebpackError[]} */
  48. this.errors = [];
  49. /** @type {object} */
  50. this.buildMeta = undefined;
  51. /** @type {object} */
  52. this.buildInfo = undefined;
  53. // Graph (per Compilation)
  54. /** @type {ModuleReason[]} */
  55. this.reasons = [];
  56. /** @type {SortableSet<Chunk>} */
  57. this._chunks = new SortableSet(undefined, sortById);
  58. // Info from Compilation (per Compilation)
  59. /** @type {number|string} */
  60. this.id = null;
  61. /** @type {number} */
  62. this.index = null;
  63. /** @type {number} */
  64. this.index2 = null;
  65. /** @type {number} */
  66. this.depth = null;
  67. /** @type {Module} */
  68. this.issuer = null;
  69. /** @type {undefined | object} */
  70. this.profile = undefined;
  71. /** @type {boolean} */
  72. this.prefetched = false;
  73. /** @type {boolean} */
  74. this.built = false;
  75. // Info from Optimization (per Compilation)
  76. /** @type {null | boolean} */
  77. this.used = null;
  78. /** @type {false | true | string[]} */
  79. this.usedExports = null;
  80. /** @type {(string | OptimizationBailoutFunction)[]} */
  81. this.optimizationBailout = [];
  82. // delayed operations
  83. /** @type {undefined | {oldChunk: Chunk, newChunks: Chunk[]}[] } */
  84. this._rewriteChunkInReasons = undefined;
  85. /** @type {boolean} */
  86. this.useSourceMap = false;
  87. // info from build
  88. this._source = null;
  89. }
  90. get exportsArgument() {
  91. return (this.buildInfo && this.buildInfo.exportsArgument) || "exports";
  92. }
  93. get moduleArgument() {
  94. return (this.buildInfo && this.buildInfo.moduleArgument) || "module";
  95. }
  96. disconnect() {
  97. this.hash = undefined;
  98. this.renderedHash = undefined;
  99. this.reasons.length = 0;
  100. this._rewriteChunkInReasons = undefined;
  101. this._chunks.clear();
  102. this.id = null;
  103. this.index = null;
  104. this.index2 = null;
  105. this.depth = null;
  106. this.issuer = null;
  107. this.profile = undefined;
  108. this.prefetched = false;
  109. this.built = false;
  110. this.used = null;
  111. this.usedExports = null;
  112. this.optimizationBailout.length = 0;
  113. super.disconnect();
  114. }
  115. unseal() {
  116. this.id = null;
  117. this.index = null;
  118. this.index2 = null;
  119. this.depth = null;
  120. this._chunks.clear();
  121. super.unseal();
  122. }
  123. setChunks(chunks) {
  124. this._chunks = new SortableSet(chunks, sortById);
  125. }
  126. addChunk(chunk) {
  127. if (this._chunks.has(chunk)) return false;
  128. this._chunks.add(chunk);
  129. return true;
  130. }
  131. removeChunk(chunk) {
  132. if (this._chunks.delete(chunk)) {
  133. chunk.removeModule(this);
  134. return true;
  135. }
  136. return false;
  137. }
  138. isInChunk(chunk) {
  139. return this._chunks.has(chunk);
  140. }
  141. isEntryModule() {
  142. for (const chunk of this._chunks) {
  143. if (chunk.entryModule === this) return true;
  144. }
  145. return false;
  146. }
  147. get optional() {
  148. return (
  149. this.reasons.length > 0 &&
  150. this.reasons.every(r => r.dependency && r.dependency.optional)
  151. );
  152. }
  153. /**
  154. * @returns {Chunk[]} all chunks which contain the module
  155. */
  156. getChunks() {
  157. return Array.from(this._chunks);
  158. }
  159. getNumberOfChunks() {
  160. return this._chunks.size;
  161. }
  162. get chunksIterable() {
  163. return this._chunks;
  164. }
  165. hasEqualsChunks(otherModule) {
  166. if (this._chunks.size !== otherModule._chunks.size) return false;
  167. this._chunks.sortWith(sortByDebugId);
  168. otherModule._chunks.sortWith(sortByDebugId);
  169. const a = this._chunks[Symbol.iterator]();
  170. const b = otherModule._chunks[Symbol.iterator]();
  171. // eslint-disable-next-line no-constant-condition
  172. while (true) {
  173. const aItem = a.next();
  174. const bItem = b.next();
  175. if (aItem.done) return true;
  176. if (aItem.value !== bItem.value) return false;
  177. }
  178. }
  179. addReason(module, dependency, explanation) {
  180. this.reasons.push(new ModuleReason(module, dependency, explanation));
  181. }
  182. removeReason(module, dependency) {
  183. for (let i = 0; i < this.reasons.length; i++) {
  184. let r = this.reasons[i];
  185. if (r.module === module && r.dependency === dependency) {
  186. this.reasons.splice(i, 1);
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. hasReasonForChunk(chunk) {
  193. if (this._rewriteChunkInReasons) {
  194. for (const operation of this._rewriteChunkInReasons) {
  195. this._doRewriteChunkInReasons(operation.oldChunk, operation.newChunks);
  196. }
  197. this._rewriteChunkInReasons = undefined;
  198. }
  199. for (let i = 0; i < this.reasons.length; i++) {
  200. if (this.reasons[i].hasChunk(chunk)) return true;
  201. }
  202. return false;
  203. }
  204. hasReasons() {
  205. return this.reasons.length > 0;
  206. }
  207. rewriteChunkInReasons(oldChunk, newChunks) {
  208. // This is expensive. Delay operation until we really need the data
  209. if (this._rewriteChunkInReasons === undefined) {
  210. this._rewriteChunkInReasons = [];
  211. }
  212. this._rewriteChunkInReasons.push({
  213. oldChunk,
  214. newChunks
  215. });
  216. }
  217. _doRewriteChunkInReasons(oldChunk, newChunks) {
  218. for (let i = 0; i < this.reasons.length; i++) {
  219. this.reasons[i].rewriteChunks(oldChunk, newChunks);
  220. }
  221. }
  222. /**
  223. * @param {string=} exportName the name of the export
  224. * @returns {boolean|string} false if the export isn't used, true if no exportName is provided and the module is used, or the name to access it if the export is used
  225. */
  226. isUsed(exportName) {
  227. if (!exportName) return this.used !== false;
  228. if (this.used === null || this.usedExports === null) return exportName;
  229. if (!this.used) return false;
  230. if (!this.usedExports) return false;
  231. if (this.usedExports === true) return exportName;
  232. let idx = this.usedExports.indexOf(exportName);
  233. if (idx < 0) return false;
  234. // Mangle export name if possible
  235. if (this.isProvided(exportName)) {
  236. if (this.buildMeta.exportsType === "namespace") {
  237. return Template.numberToIdentifer(idx);
  238. }
  239. if (
  240. this.buildMeta.exportsType === "named" &&
  241. !this.usedExports.includes("default")
  242. ) {
  243. return Template.numberToIdentifer(idx);
  244. }
  245. }
  246. return exportName;
  247. }
  248. isProvided(exportName) {
  249. if (!Array.isArray(this.buildMeta.providedExports)) return null;
  250. return this.buildMeta.providedExports.includes(exportName);
  251. }
  252. toString() {
  253. return `Module[${this.id || this.debugId}]`;
  254. }
  255. needRebuild(fileTimestamps, contextTimestamps) {
  256. return true;
  257. }
  258. /**
  259. * @param {Hash} hash the hash used to track dependencies
  260. * @returns {void}
  261. */
  262. updateHash(hash) {
  263. hash.update(`${this.id}`);
  264. hash.update(JSON.stringify(this.usedExports));
  265. super.updateHash(hash);
  266. }
  267. sortItems(sortChunks) {
  268. super.sortItems();
  269. if (sortChunks) this._chunks.sort();
  270. this.reasons.sort((a, b) => {
  271. if (a.module === b.module) return 0;
  272. if (!a.module) return -1;
  273. if (!b.module) return 1;
  274. return sortById(a.module, b.module);
  275. });
  276. if (Array.isArray(this.usedExports)) {
  277. this.usedExports.sort();
  278. }
  279. }
  280. unbuild() {
  281. this.dependencies.length = 0;
  282. this.blocks.length = 0;
  283. this.variables.length = 0;
  284. this.buildMeta = undefined;
  285. this.buildInfo = undefined;
  286. this.disconnect();
  287. }
  288. get arguments() {
  289. throw new Error("Module.arguments was removed, there is no replacement.");
  290. }
  291. set arguments(value) {
  292. throw new Error("Module.arguments was removed, there is no replacement.");
  293. }
  294. }
  295. // TODO remove in webpack 5
  296. Object.defineProperty(Module.prototype, "forEachChunk", {
  297. configurable: false,
  298. value: util.deprecate(
  299. /**
  300. * @deprecated
  301. * @param {function(any, any, Set<any>): void} fn callback function
  302. * @returns {void}
  303. * @this {Module}
  304. */
  305. function(fn) {
  306. this._chunks.forEach(fn);
  307. },
  308. "Module.forEachChunk: Use for(const chunk of module.chunksIterable) instead"
  309. )
  310. });
  311. // TODO remove in webpack 5
  312. Object.defineProperty(Module.prototype, "mapChunks", {
  313. configurable: false,
  314. value: util.deprecate(
  315. /**
  316. * @deprecated
  317. * @param {function(any, any): void} fn Mapper function
  318. * @returns {Array<TODO>} Array of chunks mapped
  319. * @this {Module}
  320. */
  321. function(fn) {
  322. return Array.from(this._chunks, fn);
  323. },
  324. "Module.mapChunks: Use Array.from(module.chunksIterable, fn) instead"
  325. )
  326. });
  327. // TODO remove in webpack 5
  328. Object.defineProperty(Module.prototype, "entry", {
  329. configurable: false,
  330. get() {
  331. throw new Error("Module.entry was removed. Use Chunk.entryModule");
  332. },
  333. set() {
  334. throw new Error("Module.entry was removed. Use Chunk.entryModule");
  335. }
  336. });
  337. // TODO remove in webpack 5
  338. Object.defineProperty(Module.prototype, "meta", {
  339. configurable: false,
  340. get: util.deprecate(
  341. /**
  342. * @deprecated
  343. * @returns {void}
  344. * @this {Module}
  345. */
  346. function() {
  347. return this.buildMeta;
  348. },
  349. "Module.meta was renamed to Module.buildMeta"
  350. ),
  351. set: util.deprecate(
  352. /**
  353. * @deprecated
  354. * @param {TODO} value Value
  355. * @returns {void}
  356. * @this {Module}
  357. */
  358. function(value) {
  359. this.buildMeta = value;
  360. },
  361. "Module.meta was renamed to Module.buildMeta"
  362. )
  363. });
  364. /** @type {function(): string} */
  365. Module.prototype.identifier = null;
  366. /** @type {function(RequestShortener): string} */
  367. Module.prototype.readableIdentifier = null;
  368. Module.prototype.build = null;
  369. Module.prototype.source = null;
  370. Module.prototype.size = null;
  371. Module.prototype.nameForCondition = null;
  372. /** @type {null | function(Chunk): boolean} */
  373. Module.prototype.chunkCondition = null;
  374. Module.prototype.updateCacheModule = null;
  375. module.exports = Module;