MergeDuplicateChunksPlugin.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { STAGE_BASIC } = require("../OptimizationStages");
  7. const createSchemaValidation = require("../util/create-schema-validation");
  8. const { runtimeEqual } = require("../util/runtime");
  9. /** @typedef {import("../../declarations/plugins/optimize/MergeDuplicateChunksPlugin").MergeDuplicateChunksPluginOptions} MergeDuplicateChunksPluginOptions */
  10. /** @typedef {import("../Compiler")} Compiler */
  11. const validate = createSchemaValidation(
  12. require("../../schemas/plugins/optimize/MergeDuplicateChunksPlugin.check.js"),
  13. () =>
  14. require("../../schemas/plugins/optimize/MergeDuplicateChunksPlugin.json"),
  15. {
  16. name: "Merge Duplicate Chunks Plugin",
  17. baseDataPath: "options"
  18. }
  19. );
  20. class MergeDuplicateChunksPlugin {
  21. /**
  22. * @param {MergeDuplicateChunksPluginOptions} options options object
  23. */
  24. constructor(options = { stage: STAGE_BASIC }) {
  25. validate(options);
  26. this.options = options;
  27. }
  28. /**
  29. * @param {Compiler} compiler the compiler
  30. * @returns {void}
  31. */
  32. apply(compiler) {
  33. compiler.hooks.compilation.tap(
  34. "MergeDuplicateChunksPlugin",
  35. compilation => {
  36. compilation.hooks.optimizeChunks.tap(
  37. {
  38. name: "MergeDuplicateChunksPlugin",
  39. stage: this.options.stage
  40. },
  41. chunks => {
  42. const { chunkGraph, moduleGraph } = compilation;
  43. // remember already tested chunks for performance
  44. const notDuplicates = new Set();
  45. // for each chunk
  46. for (const chunk of chunks) {
  47. // track a Set of all chunk that could be duplicates
  48. let possibleDuplicates;
  49. for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
  50. if (possibleDuplicates === undefined) {
  51. // when possibleDuplicates is not yet set,
  52. // create a new Set from chunks of the current module
  53. // including only chunks with the same number of modules
  54. for (const dup of chunkGraph.getModuleChunksIterable(
  55. module
  56. )) {
  57. if (
  58. dup !== chunk &&
  59. chunkGraph.getNumberOfChunkModules(chunk) ===
  60. chunkGraph.getNumberOfChunkModules(dup) &&
  61. !notDuplicates.has(dup)
  62. ) {
  63. // delay allocating the new Set until here, reduce memory pressure
  64. if (possibleDuplicates === undefined) {
  65. possibleDuplicates = new Set();
  66. }
  67. possibleDuplicates.add(dup);
  68. }
  69. }
  70. // when no chunk is possible we can break here
  71. if (possibleDuplicates === undefined) break;
  72. } else {
  73. // validate existing possible duplicates
  74. for (const dup of possibleDuplicates) {
  75. // remove possible duplicate when module is not contained
  76. if (!chunkGraph.isModuleInChunk(module, dup)) {
  77. possibleDuplicates.delete(dup);
  78. }
  79. }
  80. // when all chunks has been removed we can break here
  81. if (possibleDuplicates.size === 0) break;
  82. }
  83. }
  84. // when we found duplicates
  85. if (
  86. possibleDuplicates !== undefined &&
  87. possibleDuplicates.size > 0
  88. ) {
  89. outer: for (const otherChunk of possibleDuplicates) {
  90. if (otherChunk.hasRuntime() !== chunk.hasRuntime()) continue;
  91. if (chunkGraph.getNumberOfEntryModules(chunk) > 0) continue;
  92. if (chunkGraph.getNumberOfEntryModules(otherChunk) > 0)
  93. continue;
  94. if (!runtimeEqual(chunk.runtime, otherChunk.runtime)) {
  95. for (const module of chunkGraph.getChunkModulesIterable(
  96. chunk
  97. )) {
  98. const exportsInfo = moduleGraph.getExportsInfo(module);
  99. if (
  100. !exportsInfo.isEquallyUsed(
  101. chunk.runtime,
  102. otherChunk.runtime
  103. )
  104. ) {
  105. continue outer;
  106. }
  107. }
  108. }
  109. // merge them
  110. if (chunkGraph.canChunksBeIntegrated(chunk, otherChunk)) {
  111. chunkGraph.integrateChunks(chunk, otherChunk);
  112. compilation.chunks.delete(otherChunk);
  113. }
  114. }
  115. }
  116. // don't check already processed chunks twice
  117. notDuplicates.add(chunk);
  118. }
  119. }
  120. );
  121. }
  122. );
  123. }
  124. }
  125. module.exports = MergeDuplicateChunksPlugin;