AggressiveSplittingPlugin.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const identifierUtils = require("../util/identifier");
  7. function toIndexOf(list) {
  8. return function(item) {
  9. return list.indexOf(item);
  10. };
  11. }
  12. function toChunkModuleIndices(modules) {
  13. return function(idx) {
  14. return modules[idx];
  15. };
  16. }
  17. function moveModuleBetween(oldChunk, newChunk) {
  18. return function(module) {
  19. oldChunk.moveModule(module, newChunk);
  20. };
  21. }
  22. function isNotAEntryModule(entryModule) {
  23. return function(module) {
  24. return entryModule !== module;
  25. };
  26. }
  27. function copyWithReason(obj) {
  28. const newObj = {};
  29. Object.keys(obj).forEach((key) => {
  30. newObj[key] = obj[key];
  31. });
  32. if(!newObj.reasons || newObj.reasons.indexOf("aggressive-splitted") < 0)
  33. newObj.reasons = (newObj.reasons || []).concat("aggressive-splitted");
  34. return newObj;
  35. }
  36. class AggressiveSplittingPlugin {
  37. constructor(options) {
  38. this.options = options || {};
  39. if(typeof this.options.minSize !== "number") this.options.minSize = 30 * 1024;
  40. if(typeof this.options.maxSize !== "number") this.options.maxSize = 50 * 1024;
  41. if(typeof this.options.chunkOverhead !== "number") this.options.chunkOverhead = 0;
  42. if(typeof this.options.entryChunkMultiplicator !== "number") this.options.entryChunkMultiplicator = 1;
  43. }
  44. apply(compiler) {
  45. compiler.plugin("compilation", (compilation) => {
  46. compilation.plugin("optimize-chunks-advanced", (chunks) => {
  47. const savedSplits = compilation.records && compilation.records.aggressiveSplits || [];
  48. const usedSplits = compilation._aggressiveSplittingSplits ?
  49. savedSplits.concat(compilation._aggressiveSplittingSplits) : savedSplits;
  50. const minSize = this.options.minSize;
  51. const maxSize = this.options.maxSize;
  52. // 1. try to restore to recorded splitting
  53. for(let j = 0; j < usedSplits.length; j++) {
  54. const splitData = usedSplits[j];
  55. for(let i = 0; i < chunks.length; i++) {
  56. const chunk = chunks[i];
  57. const chunkModuleNames = chunk.modules.map(m => identifierUtils.makePathsRelative(compiler.context, m.identifier()));
  58. if(chunkModuleNames.length < splitData.modules.length)
  59. continue;
  60. const moduleIndicies = splitData.modules.map(toIndexOf(chunkModuleNames));
  61. const hasAllModules = moduleIndicies.every((idx) => {
  62. return idx >= 0;
  63. });
  64. if(hasAllModules) {
  65. if(chunkModuleNames.length > splitData.modules.length) {
  66. const selectedModules = moduleIndicies.map(toChunkModuleIndices(chunk.modules));
  67. const newChunk = compilation.addChunk();
  68. selectedModules.forEach(moveModuleBetween(chunk, newChunk));
  69. chunk.split(newChunk);
  70. chunk.name = null;
  71. newChunk._fromAggressiveSplitting = true;
  72. if(j < savedSplits.length)
  73. newChunk._fromAggressiveSplittingIndex = j;
  74. if(splitData.id !== null && splitData.id !== undefined) {
  75. newChunk.id = splitData.id;
  76. }
  77. newChunk.origins = chunk.origins.map(copyWithReason);
  78. chunk.origins = chunk.origins.map(copyWithReason);
  79. return true;
  80. } else {
  81. if(j < savedSplits.length)
  82. chunk._fromAggressiveSplittingIndex = j;
  83. chunk.name = null;
  84. if(splitData.id !== null && splitData.id !== undefined) {
  85. chunk.id = splitData.id;
  86. }
  87. }
  88. }
  89. }
  90. }
  91. // 2. for any other chunk which isn't splitted yet, split it
  92. for(let i = 0; i < chunks.length; i++) {
  93. const chunk = chunks[i];
  94. const size = chunk.size(this.options);
  95. if(size > maxSize && chunk.modules.length > 1) {
  96. const newChunk = compilation.addChunk();
  97. const modules = chunk.modules
  98. .filter(isNotAEntryModule(chunk.entryModule))
  99. .sort((a, b) => {
  100. a = a.identifier();
  101. b = b.identifier();
  102. if(a > b) return 1;
  103. if(a < b) return -1;
  104. return 0;
  105. });
  106. for(let k = 0; k < modules.length; k++) {
  107. chunk.moveModule(modules[k], newChunk);
  108. const newSize = newChunk.size(this.options);
  109. const chunkSize = chunk.size(this.options);
  110. // break early if it's fine
  111. if(chunkSize < maxSize && newSize < maxSize && newSize >= minSize && chunkSize >= minSize)
  112. break;
  113. if(newSize > maxSize && k === 0) {
  114. // break if there is a single module which is bigger than maxSize
  115. break;
  116. }
  117. if(newSize > maxSize || chunkSize < minSize) {
  118. // move it back
  119. newChunk.moveModule(modules[k], chunk);
  120. // check if it's fine now
  121. if(newSize < maxSize && newSize >= minSize && chunkSize >= minSize)
  122. break;
  123. }
  124. }
  125. if(newChunk.modules.length > 0) {
  126. chunk.split(newChunk);
  127. chunk.name = null;
  128. newChunk.origins = chunk.origins.map(copyWithReason);
  129. chunk.origins = chunk.origins.map(copyWithReason);
  130. compilation._aggressiveSplittingSplits = (compilation._aggressiveSplittingSplits || []).concat({
  131. modules: newChunk.modules.map(m => identifierUtils.makePathsRelative(compiler.context, m.identifier()))
  132. });
  133. return true;
  134. } else {
  135. chunks.splice(chunks.indexOf(newChunk), 1);
  136. }
  137. }
  138. }
  139. });
  140. compilation.plugin("record-hash", (records) => {
  141. // 3. save to made splittings to records
  142. const minSize = this.options.minSize;
  143. if(!records.aggressiveSplits) records.aggressiveSplits = [];
  144. compilation.chunks.forEach((chunk) => {
  145. if(chunk.hasEntryModule()) return;
  146. const size = chunk.size(this.options);
  147. const incorrectSize = size < minSize;
  148. const modules = chunk.modules.map(m => identifierUtils.makePathsRelative(compiler.context, m.identifier()));
  149. if(typeof chunk._fromAggressiveSplittingIndex === "undefined") {
  150. if(incorrectSize) return;
  151. chunk.recorded = true;
  152. records.aggressiveSplits.push({
  153. modules: modules,
  154. hash: chunk.hash,
  155. id: chunk.id
  156. });
  157. } else {
  158. const splitData = records.aggressiveSplits[chunk._fromAggressiveSplittingIndex];
  159. if(splitData.hash !== chunk.hash || incorrectSize) {
  160. if(chunk._fromAggressiveSplitting) {
  161. chunk._aggressiveSplittingInvalid = true;
  162. splitData.invalid = true;
  163. } else {
  164. splitData.hash = chunk.hash;
  165. }
  166. }
  167. }
  168. });
  169. records.aggressiveSplits = records.aggressiveSplits.filter((splitData) => {
  170. return !splitData.invalid;
  171. });
  172. });
  173. compilation.plugin("need-additional-seal", (callback) => {
  174. const invalid = compilation.chunks.some((chunk) => {
  175. return chunk._aggressiveSplittingInvalid;
  176. });
  177. if(invalid)
  178. return true;
  179. });
  180. });
  181. }
  182. }
  183. module.exports = AggressiveSplittingPlugin;