OccurrenceChunkIdsPlugin.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { compareChunksNatural } = require("../util/comparators");
  7. const { assignAscendingChunkIds } = require("./IdHelpers");
  8. /** @typedef {import("../../declarations/plugins/ids/OccurrenceChunkIdsPlugin").OccurrenceChunkIdsPluginOptions} OccurrenceChunkIdsPluginOptions */
  9. /** @typedef {import("../Chunk")} Chunk */
  10. /** @typedef {import("../Compiler")} Compiler */
  11. const PLUGIN_NAME = "OccurrenceChunkIdsPlugin";
  12. class OccurrenceChunkIdsPlugin {
  13. /**
  14. * Creates an instance of OccurrenceChunkIdsPlugin.
  15. * @param {OccurrenceChunkIdsPluginOptions=} options options object
  16. */
  17. constructor(options = {}) {
  18. /** @type {OccurrenceChunkIdsPluginOptions} */
  19. this.options = options;
  20. }
  21. /**
  22. * Applies the plugin by registering its hooks on the compiler.
  23. * @param {Compiler} compiler the compiler instance
  24. * @returns {void}
  25. */
  26. apply(compiler) {
  27. compiler.hooks.validate.tap(PLUGIN_NAME, () => {
  28. compiler.validate(
  29. () =>
  30. require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.json"),
  31. this.options,
  32. {
  33. name: "Occurrence Order Chunk Ids Plugin",
  34. baseDataPath: "options"
  35. },
  36. (options) =>
  37. require("../../schemas/plugins/ids/OccurrenceChunkIdsPlugin.check")(
  38. options
  39. )
  40. );
  41. });
  42. compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
  43. compilation.hooks.chunkIds.tap(PLUGIN_NAME, (chunks) => {
  44. const chunkGraph = compilation.chunkGraph;
  45. /** @type {Map<Chunk, number>} */
  46. const occursInInitialChunksMap = new Map();
  47. const compareNatural = compareChunksNatural(chunkGraph);
  48. for (const c of chunks) {
  49. let occurs = 0;
  50. for (const chunkGroup of c.groupsIterable) {
  51. for (const parent of chunkGroup.parentsIterable) {
  52. if (parent.isInitial()) occurs++;
  53. }
  54. }
  55. occursInInitialChunksMap.set(c, occurs);
  56. }
  57. /** @type {Chunk[]} */
  58. const chunksInOccurrenceOrder = [...chunks].sort((a, b) => {
  59. if (this.options.prioritiseInitial) {
  60. const aEntryOccurs =
  61. /** @type {number} */
  62. (occursInInitialChunksMap.get(a));
  63. const bEntryOccurs =
  64. /** @type {number} */
  65. (occursInInitialChunksMap.get(b));
  66. if (aEntryOccurs > bEntryOccurs) return -1;
  67. if (aEntryOccurs < bEntryOccurs) return 1;
  68. }
  69. const aOccurs = a.getNumberOfGroups();
  70. const bOccurs = b.getNumberOfGroups();
  71. if (aOccurs > bOccurs) return -1;
  72. if (aOccurs < bOccurs) return 1;
  73. return compareNatural(a, b);
  74. });
  75. assignAscendingChunkIds(chunksInOccurrenceOrder, compilation);
  76. });
  77. });
  78. }
  79. }
  80. module.exports = OccurrenceChunkIdsPlugin;