RemoveParentModulesPlugin.js 3.7 KB

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