RemoveEmptyChunksPlugin.js 990 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class RemoveEmptyChunksPlugin {
  7. apply(compiler) {
  8. compiler.hooks.compilation.tap("RemoveEmptyChunksPlugin", compilation => {
  9. const handler = chunks => {
  10. for (let i = chunks.length - 1; i >= 0; i--) {
  11. const chunk = chunks[i];
  12. if (
  13. chunk.isEmpty() &&
  14. !chunk.hasRuntime() &&
  15. !chunk.hasEntryModule()
  16. ) {
  17. chunk.remove("empty");
  18. chunks.splice(i, 1);
  19. }
  20. }
  21. };
  22. compilation.hooks.optimizeChunksBasic.tap(
  23. "RemoveEmptyChunksPlugin",
  24. handler
  25. );
  26. compilation.hooks.optimizeChunksAdvanced.tap(
  27. "RemoveEmptyChunksPlugin",
  28. handler
  29. );
  30. compilation.hooks.optimizeExtractedChunksBasic.tap(
  31. "RemoveEmptyChunksPlugin",
  32. handler
  33. );
  34. compilation.hooks.optimizeExtractedChunksAdvanced.tap(
  35. "RemoveEmptyChunksPlugin",
  36. handler
  37. );
  38. });
  39. }
  40. }
  41. module.exports = RemoveEmptyChunksPlugin;