Module.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const DependenciesBlock = require("./DependenciesBlock");
  7. const ModuleReason = require("./ModuleReason");
  8. const Template = require("./Template");
  9. function addToSet(set, items) {
  10. for(let item of items) {
  11. if(set.indexOf(item) < 0)
  12. set.push(item);
  13. }
  14. }
  15. function byId(a, b) {
  16. return a.id - b.id;
  17. }
  18. let debugId = 1000;
  19. class Module extends DependenciesBlock {
  20. constructor() {
  21. super();
  22. this.context = null;
  23. this.reasons = [];
  24. this.debugId = debugId++;
  25. this.lastId = -1;
  26. this.id = null;
  27. this.portableId = null;
  28. this.index = null;
  29. this.index2 = null;
  30. this.depth = null;
  31. this.used = null;
  32. this.usedExports = null;
  33. this.providedExports = null;
  34. this.chunks = [];
  35. this.warnings = [];
  36. this.dependenciesWarnings = [];
  37. this.errors = [];
  38. this.dependenciesErrors = [];
  39. this.strict = false;
  40. this.meta = {};
  41. }
  42. disconnect() {
  43. this.reasons.length = 0;
  44. this.lastId = this.id;
  45. this.id = null;
  46. this.index = null;
  47. this.index2 = null;
  48. this.depth = null;
  49. this.used = null;
  50. this.usedExports = null;
  51. this.providedExports = null;
  52. this.chunks.length = 0;
  53. super.disconnect();
  54. }
  55. unseal() {
  56. this.lastId = this.id;
  57. this.id = null;
  58. this.index = null;
  59. this.index2 = null;
  60. this.depth = null;
  61. this.chunks.length = 0;
  62. super.unseal();
  63. }
  64. addChunk(chunk) {
  65. let idx = this.chunks.indexOf(chunk);
  66. if(idx < 0)
  67. this.chunks.push(chunk);
  68. }
  69. removeChunk(chunk) {
  70. let idx = this.chunks.indexOf(chunk);
  71. if(idx >= 0) {
  72. this.chunks.splice(idx, 1);
  73. chunk.removeModule(this);
  74. return true;
  75. }
  76. return false;
  77. }
  78. addReason(module, dependency) {
  79. this.reasons.push(new ModuleReason(module, dependency));
  80. }
  81. removeReason(module, dependency) {
  82. for(let i = 0; i < this.reasons.length; i++) {
  83. let r = this.reasons[i];
  84. if(r.module === module && r.dependency === dependency) {
  85. this.reasons.splice(i, 1);
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. hasReasonForChunk(chunk) {
  92. for(let r of this.reasons) {
  93. if(r.chunks) {
  94. if(r.chunks.indexOf(chunk) >= 0)
  95. return true;
  96. } else if(r.module.chunks.indexOf(chunk) >= 0)
  97. return true;
  98. }
  99. return false;
  100. }
  101. rewriteChunkInReasons(oldChunk, newChunks) {
  102. this.reasons.forEach(r => {
  103. if(!r.chunks) {
  104. if(r.module.chunks.indexOf(oldChunk) < 0)
  105. return;
  106. r.chunks = r.module.chunks;
  107. }
  108. r.chunks = r.chunks.reduce((arr, c) => {
  109. addToSet(arr, c !== oldChunk ? [c] : newChunks);
  110. return arr;
  111. }, []);
  112. });
  113. }
  114. isUsed(exportName) {
  115. if(this.used === null) return exportName;
  116. if(!exportName) return !!this.used;
  117. if(!this.used) return false;
  118. if(!this.usedExports) return false;
  119. if(this.usedExports === true) return exportName;
  120. let idx = this.usedExports.indexOf(exportName);
  121. if(idx < 0) return false;
  122. if(this.isProvided(exportName))
  123. return Template.numberToIdentifer(idx);
  124. return exportName;
  125. }
  126. isProvided(exportName) {
  127. if(!Array.isArray(this.providedExports))
  128. return null;
  129. return this.providedExports.indexOf(exportName) >= 0;
  130. }
  131. toString() {
  132. return `Module[${this.id || this.debugId}]`;
  133. }
  134. needRebuild(fileTimestamps, contextTimestamps) {
  135. return true;
  136. }
  137. updateHash(hash) {
  138. hash.update(this.id + "" + this.used);
  139. hash.update(JSON.stringify(this.usedExports));
  140. super.updateHash(hash);
  141. }
  142. sortItems() {
  143. super.sortItems();
  144. this.chunks.sort(byId);
  145. this.reasons.sort((a, b) => byId(a.module, b.module));
  146. }
  147. unbuild() {
  148. this.disconnect();
  149. }
  150. }
  151. Object.defineProperty(Module.prototype, "entry", {
  152. configurable: false,
  153. get() {
  154. throw new Error("Module.entry was removed. Use Chunk.entryModule");
  155. },
  156. set() {
  157. throw new Error("Module.entry was removed. Use Chunk.entryModule");
  158. }
  159. });
  160. Module.prototype.identifier = null;
  161. Module.prototype.readableIdentifier = null;
  162. Module.prototype.build = null;
  163. Module.prototype.source = null;
  164. Module.prototype.size = null;
  165. Module.prototype.nameForCondition = null;
  166. module.exports = Module;