Module.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. let debugId = 1000;
  12. const sortById = (a, b) => {
  13. return a.id - b.id;
  14. };
  15. const sortByDebugId = (a, b) => {
  16. return a.debugId - b.debugId;
  17. };
  18. class Module extends DependenciesBlock {
  19. constructor() {
  20. super();
  21. this.context = null;
  22. this.reasons = [];
  23. this.debugId = debugId++;
  24. this.id = null;
  25. this.portableId = null;
  26. this.index = null;
  27. this.index2 = null;
  28. this.depth = null;
  29. this.used = null;
  30. this.usedExports = null;
  31. this.providedExports = null;
  32. this._chunks = new SortableSet(undefined, sortById);
  33. this._chunksDebugIdent = undefined;
  34. this.warnings = [];
  35. this.dependenciesWarnings = [];
  36. this.errors = [];
  37. this.dependenciesErrors = [];
  38. this.strict = false;
  39. this.meta = {};
  40. this.optimizationBailout = [];
  41. }
  42. disconnect() {
  43. this.reasons.length = 0;
  44. this.id = null;
  45. this.index = null;
  46. this.index2 = null;
  47. this.depth = null;
  48. this.used = null;
  49. this.usedExports = null;
  50. this.providedExports = null;
  51. this._chunks.clear();
  52. this._chunksDebugIdent = undefined;
  53. super.disconnect();
  54. }
  55. unseal() {
  56. this.id = null;
  57. this.index = null;
  58. this.index2 = null;
  59. this.depth = null;
  60. this._chunks.clear();
  61. this._chunksDebugIdent = undefined;
  62. super.unseal();
  63. }
  64. setChunks(chunks) {
  65. this._chunks = new SortableSet(chunks, sortById);
  66. this._chunksDebugIdent = undefined;
  67. }
  68. addChunk(chunk) {
  69. this._chunks.add(chunk);
  70. this._chunksDebugIdent = undefined;
  71. }
  72. removeChunk(chunk) {
  73. if(this._chunks.delete(chunk)) {
  74. this._chunksDebugIdent = undefined;
  75. chunk.removeModule(this);
  76. return true;
  77. }
  78. return false;
  79. }
  80. isInChunk(chunk) {
  81. return this._chunks.has(chunk);
  82. }
  83. getChunkIdsIdent() {
  84. if(this._chunksDebugIdent !== undefined) return this._chunksDebugIdent;
  85. this._chunks.sortWith(sortByDebugId);
  86. const chunks = this._chunks;
  87. const list = [];
  88. for(const chunk of chunks) {
  89. const debugId = chunk.debugId;
  90. if(typeof debugId !== "number") {
  91. return this._chunksDebugIdent = null;
  92. }
  93. list.push(debugId);
  94. }
  95. return this._chunksDebugIdent = list.join(",");
  96. }
  97. forEachChunk(fn) {
  98. this._chunks.forEach(fn);
  99. }
  100. mapChunks(fn) {
  101. return Array.from(this._chunks, fn);
  102. }
  103. getChunks() {
  104. return Array.from(this._chunks);
  105. }
  106. getNumberOfChunks() {
  107. return this._chunks.size;
  108. }
  109. hasEqualsChunks(otherModule) {
  110. if(this._chunks.size !== otherModule._chunks.size) return false;
  111. this._chunks.sortWith(sortByDebugId);
  112. otherModule._chunks.sortWith(sortByDebugId);
  113. const a = this._chunks[Symbol.iterator]();
  114. const b = otherModule._chunks[Symbol.iterator]();
  115. while(true) { // eslint-disable-line
  116. const aItem = a.next();
  117. const bItem = b.next();
  118. if(aItem.done) return true;
  119. if(aItem.value !== bItem.value) return false;
  120. }
  121. }
  122. addReason(module, dependency) {
  123. this.reasons.push(new ModuleReason(module, dependency));
  124. }
  125. removeReason(module, dependency) {
  126. for(let i = 0; i < this.reasons.length; i++) {
  127. let r = this.reasons[i];
  128. if(r.module === module && r.dependency === dependency) {
  129. this.reasons.splice(i, 1);
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. hasReasonForChunk(chunk) {
  136. for(let i = 0; i < this.reasons.length; i++) {
  137. if(this.reasons[i].hasChunk(chunk))
  138. return true;
  139. }
  140. return false;
  141. }
  142. rewriteChunkInReasons(oldChunk, newChunks) {
  143. for(let i = 0; i < this.reasons.length; i++) {
  144. this.reasons[i].rewriteChunks(oldChunk, newChunks);
  145. }
  146. }
  147. isUsed(exportName) {
  148. if(this.used === null) return exportName;
  149. if(!exportName) return !!this.used;
  150. if(!this.used) return false;
  151. if(!this.usedExports) return false;
  152. if(this.usedExports === true) return exportName;
  153. let idx = this.usedExports.indexOf(exportName);
  154. if(idx < 0) return false;
  155. if(this.isProvided(exportName))
  156. return Template.numberToIdentifer(idx);
  157. return exportName;
  158. }
  159. isProvided(exportName) {
  160. if(!Array.isArray(this.providedExports))
  161. return null;
  162. return this.providedExports.indexOf(exportName) >= 0;
  163. }
  164. toString() {
  165. return `Module[${this.id || this.debugId}]`;
  166. }
  167. needRebuild(fileTimestamps, contextTimestamps) {
  168. return true;
  169. }
  170. updateHash(hash) {
  171. hash.update(this.id + "" + this.used);
  172. hash.update(JSON.stringify(this.usedExports));
  173. super.updateHash(hash);
  174. }
  175. sortItems(sortChunks) {
  176. super.sortItems();
  177. if(sortChunks)
  178. this._chunks.sort();
  179. this.reasons.sort((a, b) => sortById(a.module, b.module));
  180. if(Array.isArray(this.usedExports)) {
  181. this.usedExports.sort();
  182. }
  183. }
  184. unbuild() {
  185. this.disconnect();
  186. }
  187. }
  188. Object.defineProperty(Module.prototype, "entry", {
  189. configurable: false,
  190. get() {
  191. throw new Error("Module.entry was removed. Use Chunk.entryModule");
  192. },
  193. set() {
  194. throw new Error("Module.entry was removed. Use Chunk.entryModule");
  195. }
  196. });
  197. Object.defineProperty(Module.prototype, "chunks", {
  198. configurable: false,
  199. get: util.deprecate(function() {
  200. return Array.from(this._chunks);
  201. }, "Module.chunks: Use Module.forEachChunk/mapChunks/getNumberOfChunks/isInChunk/addChunk/removeChunk instead"),
  202. set() {
  203. throw new Error("Readonly. Use Module.addChunk/removeChunk to modify chunks.");
  204. }
  205. });
  206. Module.prototype.identifier = null;
  207. Module.prototype.readableIdentifier = null;
  208. Module.prototype.build = null;
  209. Module.prototype.source = null;
  210. Module.prototype.size = null;
  211. Module.prototype.nameForCondition = null;
  212. module.exports = Module;