You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NaturalChunkOrderPlugin.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("../Compiler")} Compiler */
  7. class NaturalChunkOrderPlugin {
  8. /**
  9. * @param {Compiler} compiler webpack compiler
  10. * @returns {void}
  11. */
  12. apply(compiler) {
  13. compiler.hooks.compilation.tap("NaturalChunkOrderPlugin", compilation => {
  14. compilation.hooks.optimizeChunkOrder.tap(
  15. "NaturalChunkOrderPlugin",
  16. chunks => {
  17. chunks.sort((chunkA, chunkB) => {
  18. const a = chunkA.modulesIterable[Symbol.iterator]();
  19. const b = chunkB.modulesIterable[Symbol.iterator]();
  20. // eslint-disable-next-line no-constant-condition
  21. while (true) {
  22. const aItem = a.next();
  23. const bItem = b.next();
  24. if (aItem.done && bItem.done) return 0;
  25. if (aItem.done) return -1;
  26. if (bItem.done) return 1;
  27. const aModuleId = aItem.value.id;
  28. const bModuleId = bItem.value.id;
  29. if (aModuleId < bModuleId) return -1;
  30. if (aModuleId > bModuleId) return 1;
  31. }
  32. });
  33. }
  34. );
  35. });
  36. }
  37. }
  38. module.exports = NaturalChunkOrderPlugin;