chunksorter.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // @ts-check
  2. /** @typedef {import("webpack/lib/Compilation.js")} WebpackCompilation */
  3. 'use strict';
  4. /**
  5. * @type {{[sortmode: string] : (entryPointNames: Array<string>, compilation, htmlWebpackPluginOptions) => 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} chunks the chunks to sort
  12. * @return {Array} 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 {WebpackCompilation} compilation the webpack compilation
  19. * @param htmlWebpackPluginOptions the plugin options
  20. * @return {string[]} The sorted chunks
  21. */
  22. module.exports.manual = (entryPointNames, compilation, htmlWebpackPluginOptions) => {
  23. const chunks = htmlWebpackPluginOptions.chunks;
  24. if (!Array.isArray(chunks)) {
  25. return entryPointNames;
  26. }
  27. // Remove none existing entries from
  28. // htmlWebpackPluginOptions.chunks
  29. return chunks.filter((entryPointName) => {
  30. return compilation.entrypoints.has(entryPointName);
  31. });
  32. };
  33. /**
  34. * Defines the default sorter.
  35. */
  36. module.exports.auto = module.exports.none;