chunksorter.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 = (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;