NaturalChunkIdsPlugin.js 1006 B

1234567891011121314151617181920212223242526272829303132333435
  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. const PLUGIN_NAME = "NaturalChunkIdsPlugin";
  12. class NaturalChunkIdsPlugin {
  13. /**
  14. * Apply the plugin
  15. * @param {Compiler} compiler the compiler instance
  16. * @returns {void}
  17. */
  18. apply(compiler) {
  19. compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
  20. compilation.hooks.chunkIds.tap(PLUGIN_NAME, chunks => {
  21. const chunkGraph = compilation.chunkGraph;
  22. const compareNatural = compareChunksNatural(chunkGraph);
  23. const chunksInNaturalOrder = Array.from(chunks).sort(compareNatural);
  24. assignAscendingChunkIds(chunksInNaturalOrder, compilation);
  25. });
  26. });
  27. }
  28. }
  29. module.exports = NaturalChunkIdsPlugin;