FlagIncludedChunksPlugin.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { compareIds } = require("../util/comparators");
  7. /** @typedef {import("../Chunk")} Chunk */
  8. /** @typedef {import("../Chunk").ChunkId} ChunkId */
  9. /** @typedef {import("../Compiler")} Compiler */
  10. /** @typedef {import("../Module")} Module */
  11. const PLUGIN_NAME = "FlagIncludedChunksPlugin";
  12. class FlagIncludedChunksPlugin {
  13. /**
  14. * Applies the plugin by registering its hooks on the compiler.
  15. * @param {Compiler} compiler the compiler instance
  16. * @returns {void}
  17. */
  18. apply(compiler) {
  19. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  20. compilation.hooks.optimizeChunkIds.tap(PLUGIN_NAME, (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 / (1 / modulesCount) ** (1 / 31);
  38. /** @type {number[]} */
  39. const modulos = Array.from(
  40. { length: 31 },
  41. /**
  42. * Handles the callback logic for this hook.
  43. * @param {number} x x
  44. * @param {number} i i
  45. * @returns {number} result
  46. */
  47. (x, i) => (modulo ** i) | 0
  48. );
  49. // iterate all modules to generate bit values
  50. let i = 0;
  51. for (const module of compilation.modules) {
  52. let bit = 30;
  53. while (i % modulos[bit] !== 0) {
  54. bit--;
  55. }
  56. moduleBits.set(module, 1 << bit);
  57. i++;
  58. }
  59. // iterate all chunks to generate bitmaps
  60. /** @type {WeakMap<Chunk, number>} */
  61. const chunkModulesHash = new WeakMap();
  62. for (const chunk of chunks) {
  63. let hash = 0;
  64. for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
  65. hash |= /** @type {number} */ (moduleBits.get(module));
  66. }
  67. chunkModulesHash.set(chunk, hash);
  68. }
  69. for (const chunkA of chunks) {
  70. const chunkAHash =
  71. /** @type {number} */
  72. (chunkModulesHash.get(chunkA));
  73. const chunkAModulesCount = chunkGraph.getNumberOfChunkModules(chunkA);
  74. if (chunkAModulesCount === 0) continue;
  75. /** @type {undefined | Module} */
  76. let bestModule;
  77. for (const module of chunkGraph.getChunkModulesIterable(chunkA)) {
  78. if (
  79. bestModule === undefined ||
  80. chunkGraph.getNumberOfModuleChunks(bestModule) >
  81. chunkGraph.getNumberOfModuleChunks(module)
  82. ) {
  83. bestModule = module;
  84. }
  85. }
  86. loopB: for (const chunkB of chunkGraph.getModuleChunksIterable(
  87. /** @type {Module} */ (bestModule)
  88. )) {
  89. // as we iterate the same iterables twice
  90. // skip if we find ourselves
  91. if (chunkA === chunkB) continue;
  92. const chunkBModulesCount =
  93. chunkGraph.getNumberOfChunkModules(chunkB);
  94. // ids for empty chunks are not included
  95. if (chunkBModulesCount === 0) continue;
  96. // instead of swapping A and B just bail
  97. // as we loop twice the current A will be B and B then A
  98. if (chunkAModulesCount > chunkBModulesCount) continue;
  99. // is chunkA in chunkB?
  100. // we do a cheap check for the hash value
  101. const chunkBHash =
  102. /** @type {number} */
  103. (chunkModulesHash.get(chunkB));
  104. if ((chunkBHash & chunkAHash) !== chunkAHash) continue;
  105. // compare all modules
  106. for (const m of chunkGraph.getChunkModulesIterable(chunkA)) {
  107. if (!chunkGraph.isModuleInChunk(m, chunkB)) continue loopB;
  108. }
  109. /** @type {ChunkId[]} */
  110. (chunkB.ids).push(/** @type {ChunkId} */ (chunkA.id));
  111. // https://github.com/webpack/webpack/issues/18837
  112. /** @type {ChunkId[]} */
  113. (chunkB.ids).sort(compareIds);
  114. }
  115. }
  116. });
  117. });
  118. }
  119. }
  120. module.exports = FlagIncludedChunksPlugin;