FlagIncludedChunksPlugin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("../Chunk")} Chunk */
  7. /** @typedef {import("../Chunk").ChunkId} ChunkId */
  8. /** @typedef {import("../Compiler")} Compiler */
  9. /** @typedef {import("../Module")} Module */
  10. class FlagIncludedChunksPlugin {
  11. /**
  12. * Apply the plugin
  13. * @param {Compiler} compiler the compiler instance
  14. * @returns {void}
  15. */
  16. apply(compiler) {
  17. compiler.hooks.compilation.tap("FlagIncludedChunksPlugin", compilation => {
  18. compilation.hooks.optimizeChunkIds.tap(
  19. "FlagIncludedChunksPlugin",
  20. chunks => {
  21. const chunkGraph = compilation.chunkGraph;
  22. // prepare two bit integers for each module
  23. // 2^31 is the max number represented as SMI in v8
  24. // we want the bits distributed this way:
  25. // the bit 2^31 is pretty rar and only one module should get it
  26. // so it has a probability of 1 / modulesCount
  27. // the first bit (2^0) is the easiest and every module could get it
  28. // if it doesn't get a better bit
  29. // from bit 2^n to 2^(n+1) there is a probability of p
  30. // so 1 / modulesCount == p^31
  31. // <=> p = sqrt31(1 / modulesCount)
  32. // so we use a modulo of 1 / sqrt31(1 / modulesCount)
  33. /** @type {WeakMap<Module, number>} */
  34. const moduleBits = new WeakMap();
  35. const modulesCount = compilation.modules.size;
  36. // precalculate the modulo values for each bit
  37. const modulo = 1 / Math.pow(1 / modulesCount, 1 / 31);
  38. const modulos = Array.from(
  39. { length: 31 },
  40. (x, i) => Math.pow(modulo, i) | 0
  41. );
  42. // iterate all modules to generate bit values
  43. let i = 0;
  44. for (const module of compilation.modules) {
  45. let bit = 30;
  46. while (i % modulos[bit] !== 0) {
  47. bit--;
  48. }
  49. moduleBits.set(module, 1 << bit);
  50. i++;
  51. }
  52. // iterate all chunks to generate bitmaps
  53. /** @type {WeakMap<Chunk, number>} */
  54. const chunkModulesHash = new WeakMap();
  55. for (const chunk of chunks) {
  56. let hash = 0;
  57. for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
  58. hash |= /** @type {number} */ (moduleBits.get(module));
  59. }
  60. chunkModulesHash.set(chunk, hash);
  61. }
  62. for (const chunkA of chunks) {
  63. const chunkAHash =
  64. /** @type {number} */
  65. (chunkModulesHash.get(chunkA));
  66. const chunkAModulesCount =
  67. chunkGraph.getNumberOfChunkModules(chunkA);
  68. if (chunkAModulesCount === 0) continue;
  69. let bestModule = undefined;
  70. for (const module of chunkGraph.getChunkModulesIterable(chunkA)) {
  71. if (
  72. bestModule === undefined ||
  73. chunkGraph.getNumberOfModuleChunks(bestModule) >
  74. chunkGraph.getNumberOfModuleChunks(module)
  75. )
  76. bestModule = module;
  77. }
  78. loopB: for (const chunkB of chunkGraph.getModuleChunksIterable(
  79. /** @type {Module} */ (bestModule)
  80. )) {
  81. // as we iterate the same iterables twice
  82. // skip if we find ourselves
  83. if (chunkA === chunkB) continue;
  84. const chunkBModulesCount =
  85. chunkGraph.getNumberOfChunkModules(chunkB);
  86. // ids for empty chunks are not included
  87. if (chunkBModulesCount === 0) continue;
  88. // instead of swapping A and B just bail
  89. // as we loop twice the current A will be B and B then A
  90. if (chunkAModulesCount > chunkBModulesCount) continue;
  91. // is chunkA in chunkB?
  92. // we do a cheap check for the hash value
  93. const chunkBHash =
  94. /** @type {number} */
  95. (chunkModulesHash.get(chunkB));
  96. if ((chunkBHash & chunkAHash) !== chunkAHash) continue;
  97. // compare all modules
  98. for (const m of chunkGraph.getChunkModulesIterable(chunkA)) {
  99. if (!chunkGraph.isModuleInChunk(m, chunkB)) continue loopB;
  100. }
  101. /** @type {ChunkId[]} */
  102. (chunkB.ids).push(/** @type {ChunkId} */ (chunkA.id));
  103. }
  104. }
  105. }
  106. );
  107. });
  108. }
  109. }
  110. module.exports = FlagIncludedChunksPlugin;