a zip code crypto-currency system good for red ONLY

AggressiveSplittingPlugin.js 7.1KB

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