Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RemoveParentModulesPlugin.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Queue = require("../util/Queue");
  7. const { intersect } = require("../util/SetHelpers");
  8. const getParentChunksWithModule = (currentChunk, module) => {
  9. const chunks = [];
  10. const stack = new Set(currentChunk.parentsIterable);
  11. for (const chunk of stack) {
  12. if (chunk.containsModule(module)) {
  13. chunks.push(chunk);
  14. } else {
  15. for (const parent of chunk.parentsIterable) {
  16. stack.add(parent);
  17. }
  18. }
  19. }
  20. return chunks;
  21. };
  22. class RemoveParentModulesPlugin {
  23. apply(compiler) {
  24. compiler.hooks.compilation.tap("RemoveParentModulesPlugin", compilation => {
  25. const handler = (chunks, chunkGroups) => {
  26. const queue = new Queue();
  27. const availableModulesMap = new WeakMap();
  28. for (const chunkGroup of compilation.entrypoints.values()) {
  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 chunk.modulesIterable) {
  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. !parent.containsModule(m) &&
  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 = chunk.getNumberOfModules();
  85. const toRemove = new Set();
  86. if (numberOfModules < availableModules.size) {
  87. for (const m of chunk.modulesIterable) {
  88. if (availableModules.has(m)) {
  89. toRemove.add(m);
  90. }
  91. }
  92. } else {
  93. for (const m of availableModules) {
  94. if (chunk.containsModule(m)) {
  95. toRemove.add(m);
  96. }
  97. }
  98. }
  99. for (const module of toRemove) {
  100. module.rewriteChunkInReasons(
  101. chunk,
  102. getParentChunksWithModule(chunk, module)
  103. );
  104. chunk.removeModule(module);
  105. }
  106. }
  107. };
  108. compilation.hooks.optimizeChunksBasic.tap(
  109. "RemoveParentModulesPlugin",
  110. handler
  111. );
  112. compilation.hooks.optimizeExtractedChunksBasic.tap(
  113. "RemoveParentModulesPlugin",
  114. handler
  115. );
  116. });
  117. }
  118. }
  119. module.exports = RemoveParentModulesPlugin;