MergeDuplicateChunksPlugin.js 845 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. function getChunkIdentifier(chunk) {
  7. return chunk.modules.map((m) => {
  8. return m.identifier();
  9. }).sort().join(", ");
  10. }
  11. class MergeDuplicateChunksPlugin {
  12. apply(compiler) {
  13. compiler.plugin("compilation", (compilation) => {
  14. compilation.plugin("optimize-chunks-basic", (chunks) => {
  15. const map = {};
  16. chunks.slice().forEach((chunk) => {
  17. if(chunk.hasRuntime() || chunk.hasEntryModule()) return;
  18. const ident = getChunkIdentifier(chunk);
  19. if(map[ident]) {
  20. if(map[ident].integrate(chunk, "duplicate"))
  21. chunks.splice(chunks.indexOf(chunk), 1);
  22. return;
  23. }
  24. map[ident] = chunk;
  25. });
  26. });
  27. });
  28. }
  29. }
  30. module.exports = MergeDuplicateChunksPlugin;