Chunk.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const compareLocations = require("./compareLocations");
  7. let debugId = 1000;
  8. const byId = (a, b) => {
  9. if(a.id < b.id) return -1;
  10. if(b.id < a.id) return 1;
  11. return 0;
  12. };
  13. class Chunk {
  14. constructor(name, module, loc) {
  15. this.id = null;
  16. this.ids = null;
  17. this.debugId = debugId++;
  18. this.name = name;
  19. this.modules = [];
  20. this.entrypoints = [];
  21. this.chunks = [];
  22. this.parents = [];
  23. this.blocks = [];
  24. this.origins = [];
  25. this.files = [];
  26. this.rendered = false;
  27. if(module) {
  28. this.origins.push({
  29. module,
  30. loc,
  31. name
  32. });
  33. }
  34. }
  35. get entry() {
  36. throw new Error("Chunk.entry was removed. Use hasRuntime()");
  37. }
  38. set entry(data) {
  39. throw new Error("Chunk.entry was removed. Use hasRuntime()");
  40. }
  41. get initial() {
  42. throw new Error("Chunk.initial was removed. Use isInitial()");
  43. }
  44. set initial(data) {
  45. throw new Error("Chunk.initial was removed. Use isInitial()");
  46. }
  47. hasRuntime() {
  48. if(this.entrypoints.length === 0) return false;
  49. return this.entrypoints[0].chunks[0] === this;
  50. }
  51. isInitial() {
  52. return this.entrypoints.length > 0;
  53. }
  54. hasEntryModule() {
  55. return !!this.entryModule;
  56. }
  57. addToCollection(collection, item) {
  58. if(item === this) {
  59. return false;
  60. }
  61. if(collection.indexOf(item) > -1) {
  62. return false;
  63. }
  64. collection.push(item);
  65. return true;
  66. }
  67. addChunk(chunk) {
  68. return this.addToCollection(this.chunks, chunk);
  69. }
  70. addParent(parentChunk) {
  71. return this.addToCollection(this.parents, parentChunk);
  72. }
  73. addModule(module) {
  74. return this.addToCollection(this.modules, module);
  75. }
  76. addBlock(block) {
  77. return this.addToCollection(this.blocks, block);
  78. }
  79. removeModule(module) {
  80. const idx = this.modules.indexOf(module);
  81. if(idx >= 0) {
  82. this.modules.splice(idx, 1);
  83. module.removeChunk(this);
  84. return true;
  85. }
  86. return false;
  87. }
  88. removeChunk(chunk) {
  89. const idx = this.chunks.indexOf(chunk);
  90. if(idx >= 0) {
  91. this.chunks.splice(idx, 1);
  92. chunk.removeParent(this);
  93. return true;
  94. }
  95. return false;
  96. }
  97. removeParent(chunk) {
  98. const idx = this.parents.indexOf(chunk);
  99. if(idx >= 0) {
  100. this.parents.splice(idx, 1);
  101. chunk.removeChunk(this);
  102. return true;
  103. }
  104. return false;
  105. }
  106. addOrigin(module, loc) {
  107. this.origins.push({
  108. module,
  109. loc,
  110. name: this.name
  111. });
  112. }
  113. remove(reason) {
  114. // cleanup modules
  115. this.modules.slice().forEach(module => {
  116. module.removeChunk(this);
  117. });
  118. // cleanup parents
  119. this.parents.forEach(parentChunk => {
  120. // remove this chunk from its parents
  121. const idx = parentChunk.chunks.indexOf(this);
  122. if(idx >= 0) {
  123. parentChunk.chunks.splice(idx, 1);
  124. }
  125. // cleanup "sub chunks"
  126. this.chunks.forEach(chunk => {
  127. /**
  128. * remove this chunk as "intermediary" and connect
  129. * it "sub chunks" and parents directly
  130. */
  131. // add parent to each "sub chunk"
  132. chunk.addParent(parentChunk);
  133. // add "sub chunk" to parent
  134. parentChunk.addChunk(chunk);
  135. });
  136. });
  137. /**
  138. * we need to iterate again over the chunks
  139. * to remove this from the chunks parents.
  140. * This can not be done in the above loop
  141. * as it is not garuanteed that `this.parents` contains anything.
  142. */
  143. this.chunks.forEach(chunk => {
  144. // remove this as parent of every "sub chunk"
  145. const idx = chunk.parents.indexOf(this);
  146. if(idx >= 0) {
  147. chunk.parents.splice(idx, 1);
  148. }
  149. });
  150. // cleanup blocks
  151. this.blocks.forEach(block => {
  152. const idx = block.chunks.indexOf(this);
  153. if(idx >= 0) {
  154. block.chunks.splice(idx, 1);
  155. if(block.chunks.length === 0) {
  156. block.chunks = null;
  157. block.chunkReason = reason;
  158. }
  159. }
  160. });
  161. }
  162. moveModule(module, otherChunk) {
  163. module.removeChunk(this);
  164. module.addChunk(otherChunk);
  165. otherChunk.addModule(module);
  166. module.rewriteChunkInReasons(this, [otherChunk]);
  167. }
  168. replaceChunk(oldChunk, newChunk) {
  169. const idx = this.chunks.indexOf(oldChunk);
  170. if(idx >= 0) {
  171. this.chunks.splice(idx, 1);
  172. }
  173. if(this !== newChunk && newChunk.addParent(this)) {
  174. this.addChunk(newChunk);
  175. }
  176. }
  177. replaceParentChunk(oldParentChunk, newParentChunk) {
  178. const idx = this.parents.indexOf(oldParentChunk);
  179. if(idx >= 0) {
  180. this.parents.splice(idx, 1);
  181. }
  182. if(this !== newParentChunk && newParentChunk.addChunk(this)) {
  183. this.addParent(newParentChunk);
  184. }
  185. }
  186. integrate(otherChunk, reason) {
  187. if(!this.canBeIntegrated(otherChunk)) {
  188. return false;
  189. }
  190. const otherChunkModules = otherChunk.modules.slice();
  191. otherChunkModules.forEach(module => otherChunk.moveModule(module, this));
  192. otherChunk.modules.length = 0;
  193. otherChunk.parents.forEach(parentChunk => parentChunk.replaceChunk(otherChunk, this));
  194. otherChunk.parents.length = 0;
  195. otherChunk.chunks.forEach(chunk => chunk.replaceParentChunk(otherChunk, this));
  196. otherChunk.chunks.length = 0;
  197. otherChunk.blocks.forEach(b => {
  198. b.chunks = b.chunks ? b.chunks.map(c => {
  199. return c === otherChunk ? this : c;
  200. }) : [this];
  201. b.chunkReason = reason;
  202. this.addBlock(b);
  203. });
  204. otherChunk.blocks.length = 0;
  205. otherChunk.origins.forEach(origin => {
  206. this.origins.push(origin);
  207. });
  208. this.origins.forEach(origin => {
  209. if(!origin.reasons) {
  210. origin.reasons = [reason];
  211. } else if(origin.reasons[0] !== reason) {
  212. origin.reasons.unshift(reason);
  213. }
  214. });
  215. this.chunks = this.chunks.filter(chunk => {
  216. return chunk !== otherChunk && chunk !== this;
  217. });
  218. this.parents = this.parents.filter(parentChunk => {
  219. return parentChunk !== otherChunk && parentChunk !== this;
  220. });
  221. return true;
  222. }
  223. split(newChunk) {
  224. this.blocks.forEach(block => {
  225. newChunk.blocks.push(block);
  226. block.chunks.push(newChunk);
  227. });
  228. this.chunks.forEach(chunk => {
  229. newChunk.chunks.push(chunk);
  230. chunk.parents.push(newChunk);
  231. });
  232. this.parents.forEach(parentChunk => {
  233. parentChunk.chunks.push(newChunk);
  234. newChunk.parents.push(parentChunk);
  235. });
  236. this.entrypoints.forEach(entrypoint => {
  237. entrypoint.insertChunk(newChunk, this);
  238. });
  239. }
  240. isEmpty() {
  241. return this.modules.length === 0;
  242. }
  243. updateHash(hash) {
  244. hash.update(`${this.id} `);
  245. hash.update(this.ids ? this.ids.join(",") : "");
  246. hash.update(`${this.name || ""} `);
  247. this.modules.forEach(m => m.updateHash(hash));
  248. }
  249. canBeIntegrated(otherChunk) {
  250. if(otherChunk.isInitial()) {
  251. return false;
  252. }
  253. if(this.isInitial()) {
  254. if(otherChunk.parents.length !== 1 || otherChunk.parents[0] !== this) {
  255. return false;
  256. }
  257. }
  258. return true;
  259. }
  260. addMultiplierAndOverhead(size, options) {
  261. const overhead = typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
  262. const multiplicator = this.isInitial() ? (options.entryChunkMultiplicator || 10) : 1;
  263. return size * multiplicator + overhead;
  264. }
  265. modulesSize() {
  266. let count = 0;
  267. for(let i = 0; i < this.modules.length; i++) {
  268. count += this.modules[i].size();
  269. }
  270. return count;
  271. }
  272. size(options) {
  273. return this.addMultiplierAndOverhead(this.modulesSize(), options);
  274. }
  275. integratedSize(otherChunk, options) {
  276. // Chunk if it's possible to integrate this chunk
  277. if(!this.canBeIntegrated(otherChunk)) {
  278. return false;
  279. }
  280. let integratedModulesSize = this.modulesSize();
  281. // only count modules that do not exist in this chunk!
  282. for(let i = 0; i < otherChunk.modules.length; i++) {
  283. const otherModule = otherChunk.modules[i];
  284. if(this.modules.indexOf(otherModule) === -1) {
  285. integratedModulesSize += otherModule.size();
  286. }
  287. }
  288. return this.addMultiplierAndOverhead(integratedModulesSize, options);
  289. }
  290. getChunkMaps(includeEntries, realHash) {
  291. const chunksProcessed = [];
  292. const chunkHashMap = {};
  293. const chunkNameMap = {};
  294. (function addChunk(chunk) {
  295. if(chunksProcessed.indexOf(chunk) >= 0) return;
  296. chunksProcessed.push(chunk);
  297. if(!chunk.hasRuntime() || includeEntries) {
  298. chunkHashMap[chunk.id] = realHash ? chunk.hash : chunk.renderedHash;
  299. if(chunk.name)
  300. chunkNameMap[chunk.id] = chunk.name;
  301. }
  302. chunk.chunks.forEach(addChunk);
  303. }(this));
  304. return {
  305. hash: chunkHashMap,
  306. name: chunkNameMap
  307. };
  308. }
  309. sortItems() {
  310. this.modules.sort(byId);
  311. this.origins.sort((a, b) => {
  312. const aIdent = a.module.identifier();
  313. const bIdent = b.module.identifier();
  314. if(aIdent < bIdent) return -1;
  315. if(aIdent > bIdent) return 1;
  316. return compareLocations(a.loc, b.loc);
  317. });
  318. this.origins.forEach(origin => {
  319. if(origin.reasons)
  320. origin.reasons.sort();
  321. });
  322. this.parents.sort(byId);
  323. this.chunks.sort(byId);
  324. }
  325. toString() {
  326. return `Chunk[${this.modules.join()}]`;
  327. }
  328. checkConstraints() {
  329. const chunk = this;
  330. chunk.chunks.forEach((child, idx) => {
  331. if(chunk.chunks.indexOf(child) !== idx)
  332. throw new Error(`checkConstraints: duplicate child in chunk ${chunk.debugId} ${child.debugId}`);
  333. if(child.parents.indexOf(chunk) < 0)
  334. throw new Error(`checkConstraints: child missing parent ${chunk.debugId} -> ${child.debugId}`);
  335. });
  336. chunk.parents.forEach((parentChunk, idx) => {
  337. if(chunk.parents.indexOf(parentChunk) !== idx)
  338. throw new Error(`checkConstraints: duplicate parent in chunk ${chunk.debugId} ${parentChunk.debugId}`);
  339. if(parentChunk.chunks.indexOf(chunk) < 0)
  340. throw new Error(`checkConstraints: parent missing child ${parentChunk.debugId} <- ${chunk.debugId}`);
  341. });
  342. }
  343. }
  344. module.exports = Chunk;