NaturalChunkIdsPlugin.js 984 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { compareChunksNatural } = require("../util/comparators");
  7. const { assignAscendingChunkIds } = require("./IdHelpers");
  8. /** @typedef {import("../Chunk")} Chunk */
  9. /** @typedef {import("../Compiler")} Compiler */
  10. /** @typedef {import("../Module")} Module */
  11. class NaturalChunkIdsPlugin {
  12. /**
  13. * Apply the plugin
  14. * @param {Compiler} compiler the compiler instance
  15. * @returns {void}
  16. */
  17. apply(compiler) {
  18. compiler.hooks.compilation.tap("NaturalChunkIdsPlugin", compilation => {
  19. compilation.hooks.chunkIds.tap("NaturalChunkIdsPlugin", chunks => {
  20. const chunkGraph = compilation.chunkGraph;
  21. const compareNatural = compareChunksNatural(chunkGraph);
  22. const chunksInNaturalOrder = Array.from(chunks).sort(compareNatural);
  23. assignAscendingChunkIds(chunksInNaturalOrder, compilation);
  24. });
  25. });
  26. }
  27. }
  28. module.exports = NaturalChunkIdsPlugin;