chunksorter.js 1.4 KB

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