NaturalModuleIdsPlugin.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Florent Cailhol @ooflorent
  4. */
  5. "use strict";
  6. const {
  7. compareModulesByPreOrderIndexOrIdentifier
  8. } = require("../util/comparators");
  9. const {
  10. assignAscendingModuleIds,
  11. getUsedModuleIdsAndModules
  12. } = require("./IdHelpers");
  13. /** @typedef {import("../Compiler")} Compiler */
  14. /** @typedef {import("../Module")} Module */
  15. class NaturalModuleIdsPlugin {
  16. /**
  17. * Apply the plugin
  18. * @param {Compiler} compiler the compiler instance
  19. * @returns {void}
  20. */
  21. apply(compiler) {
  22. compiler.hooks.compilation.tap("NaturalModuleIdsPlugin", compilation => {
  23. compilation.hooks.moduleIds.tap("NaturalModuleIdsPlugin", modules => {
  24. const [usedIds, modulesInNaturalOrder] =
  25. getUsedModuleIdsAndModules(compilation);
  26. modulesInNaturalOrder.sort(
  27. compareModulesByPreOrderIndexOrIdentifier(compilation.moduleGraph)
  28. );
  29. assignAscendingModuleIds(usedIds, modulesInNaturalOrder, compilation);
  30. });
  31. });
  32. }
  33. }
  34. module.exports = NaturalModuleIdsPlugin;