10
0

chunksorter.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // @ts-check
  2. "use strict";
  3. /** @typedef {import("webpack").Compilation} Compilation */
  4. /**
  5. * @type {{[sortmode: string] : (entryPointNames: Array<string>, compilation: Compilation, htmlWebpackPluginOptions: any) => Array<string> }}
  6. * This file contains different sort methods for the entry chunks names
  7. */
  8. module.exports = {};
  9. /**
  10. * Performs identity mapping (no-sort).
  11. * @param {Array<string>} chunks the chunks to sort
  12. * @return {Array<string>} The sorted chunks
  13. */
  14. module.exports.none = (chunks) => chunks;
  15. /**
  16. * Sort manually by the chunks
  17. * @param {string[]} entryPointNames the chunks to sort
  18. * @param {Compilation} compilation the webpack compilation
  19. * @param {any} htmlWebpackPluginOptions the plugin options
  20. * @return {string[]} The sorted chunks
  21. */
  22. module.exports.manual = (
  23. entryPointNames,
  24. compilation,
  25. htmlWebpackPluginOptions,
  26. ) => {
  27. const chunks = htmlWebpackPluginOptions.chunks;
  28. if (!Array.isArray(chunks)) {
  29. return entryPointNames;
  30. }
  31. // Remove none existing entries from
  32. // htmlWebpackPluginOptions.chunks
  33. return chunks.filter((entryPointName) => {
  34. return compilation.entrypoints.has(entryPointName);
  35. });
  36. };
  37. /**
  38. * Defines the default sorter.
  39. */
  40. module.exports.auto = module.exports.none;