RemoveParentModulesPlugin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 Queue = require("../util/Queue");
  8. const { intersect } = require("../util/SetHelpers");
  9. /** @typedef {import("../Chunk")} Chunk */
  10. /** @typedef {import("../ChunkGroup")} ChunkGroup */
  11. /** @typedef {import("../Compiler")} Compiler */
  12. class RemoveParentModulesPlugin {
  13. /**
  14. * @param {Compiler} compiler the compiler
  15. * @returns {void}
  16. */
  17. apply(compiler) {
  18. compiler.hooks.compilation.tap("RemoveParentModulesPlugin", compilation => {
  19. /**
  20. * @param {Iterable<Chunk>} chunks the chunks
  21. * @param {ChunkGroup[]} chunkGroups the chunk groups
  22. */
  23. const handler = (chunks, chunkGroups) => {
  24. const chunkGraph = compilation.chunkGraph;
  25. const queue = new Queue();
  26. const availableModulesMap = new WeakMap();
  27. for (const chunkGroup of compilation.entrypoints.values()) {
  28. // initialize available modules for chunks without parents
  29. availableModulesMap.set(chunkGroup, new Set());
  30. for (const child of chunkGroup.childrenIterable) {
  31. queue.enqueue(child);
  32. }
  33. }
  34. for (const chunkGroup of compilation.asyncEntrypoints) {
  35. // initialize available modules for chunks without parents
  36. availableModulesMap.set(chunkGroup, new Set());
  37. for (const child of chunkGroup.childrenIterable) {
  38. queue.enqueue(child);
  39. }
  40. }
  41. while (queue.length > 0) {
  42. const chunkGroup = queue.dequeue();
  43. let availableModules = availableModulesMap.get(chunkGroup);
  44. let changed = false;
  45. for (const parent of chunkGroup.parentsIterable) {
  46. const availableModulesInParent = availableModulesMap.get(parent);
  47. if (availableModulesInParent !== undefined) {
  48. // If we know the available modules in parent: process these
  49. if (availableModules === undefined) {
  50. // if we have not own info yet: create new entry
  51. availableModules = new Set(availableModulesInParent);
  52. for (const chunk of parent.chunks) {
  53. for (const m of chunkGraph.getChunkModulesIterable(chunk)) {
  54. availableModules.add(m);
  55. }
  56. }
  57. availableModulesMap.set(chunkGroup, availableModules);
  58. changed = true;
  59. } else {
  60. for (const m of availableModules) {
  61. if (
  62. !chunkGraph.isModuleInChunkGroup(m, parent) &&
  63. !availableModulesInParent.has(m)
  64. ) {
  65. availableModules.delete(m);
  66. changed = true;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. if (changed) {
  73. // if something changed: enqueue our children
  74. for (const child of chunkGroup.childrenIterable) {
  75. queue.enqueue(child);
  76. }
  77. }
  78. }
  79. // now we have available modules for every chunk
  80. for (const chunk of chunks) {
  81. const availableModulesSets = Array.from(
  82. chunk.groupsIterable,
  83. chunkGroup => availableModulesMap.get(chunkGroup)
  84. );
  85. if (availableModulesSets.some(s => s === undefined)) continue; // No info about this chunk group
  86. const availableModules =
  87. availableModulesSets.length === 1
  88. ? availableModulesSets[0]
  89. : intersect(availableModulesSets);
  90. const numberOfModules = chunkGraph.getNumberOfChunkModules(chunk);
  91. const toRemove = new Set();
  92. if (numberOfModules < availableModules.size) {
  93. for (const m of chunkGraph.getChunkModulesIterable(chunk)) {
  94. if (availableModules.has(m)) {
  95. toRemove.add(m);
  96. }
  97. }
  98. } else {
  99. for (const m of availableModules) {
  100. if (chunkGraph.isModuleInChunk(m, chunk)) {
  101. toRemove.add(m);
  102. }
  103. }
  104. }
  105. for (const module of toRemove) {
  106. chunkGraph.disconnectChunkAndModule(chunk, module);
  107. }
  108. }
  109. };
  110. compilation.hooks.optimizeChunks.tap(
  111. {
  112. name: "RemoveParentModulesPlugin",
  113. stage: STAGE_BASIC
  114. },
  115. handler
  116. );
  117. });
  118. }
  119. }
  120. module.exports = RemoveParentModulesPlugin;