ChunkModuleIdRangePlugin.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { find } = require("../util/SetHelpers");
  7. const {
  8. compareModulesByPostOrderIndexOrIdentifier,
  9. compareModulesByPreOrderIndexOrIdentifier
  10. } = require("../util/comparators");
  11. /** @typedef {import("../Compiler")} Compiler */
  12. /** @typedef {import("../Module")} Module */
  13. /**
  14. * @typedef {object} ChunkModuleIdRangePluginOptions
  15. * @property {string} name the chunk name
  16. * @property {("index" | "index2" | "preOrderIndex" | "postOrderIndex")=} order order
  17. * @property {number=} start start id
  18. * @property {number=} end end id
  19. */
  20. const PLUGIN_NAME = "ChunkModuleIdRangePlugin";
  21. class ChunkModuleIdRangePlugin {
  22. /**
  23. * @param {ChunkModuleIdRangePluginOptions} options options object
  24. */
  25. constructor(options) {
  26. this.options = options;
  27. }
  28. /**
  29. * Apply the plugin
  30. * @param {Compiler} compiler the compiler instance
  31. * @returns {void}
  32. */
  33. apply(compiler) {
  34. const options = this.options;
  35. compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
  36. const moduleGraph = compilation.moduleGraph;
  37. compilation.hooks.moduleIds.tap(PLUGIN_NAME, modules => {
  38. const chunkGraph = compilation.chunkGraph;
  39. const chunk = find(
  40. compilation.chunks,
  41. chunk => chunk.name === options.name
  42. );
  43. if (!chunk) {
  44. throw new Error(
  45. `${PLUGIN_NAME}: Chunk with name '${options.name}"' was not found`
  46. );
  47. }
  48. /** @type {Module[]} */
  49. let chunkModules;
  50. if (options.order) {
  51. let cmpFn;
  52. switch (options.order) {
  53. case "index":
  54. case "preOrderIndex":
  55. cmpFn = compareModulesByPreOrderIndexOrIdentifier(moduleGraph);
  56. break;
  57. case "index2":
  58. case "postOrderIndex":
  59. cmpFn = compareModulesByPostOrderIndexOrIdentifier(moduleGraph);
  60. break;
  61. default:
  62. throw new Error(`${PLUGIN_NAME}: unexpected value of order`);
  63. }
  64. chunkModules = chunkGraph.getOrderedChunkModules(chunk, cmpFn);
  65. } else {
  66. chunkModules = [...modules]
  67. .filter(m => chunkGraph.isModuleInChunk(m, chunk))
  68. .sort(compareModulesByPreOrderIndexOrIdentifier(moduleGraph));
  69. }
  70. let currentId = options.start || 0;
  71. for (let i = 0; i < chunkModules.length; i++) {
  72. const m = chunkModules[i];
  73. if (m.needId && chunkGraph.getModuleId(m) === null) {
  74. chunkGraph.setModuleId(m, currentId++);
  75. }
  76. if (options.end && currentId > options.end) break;
  77. }
  78. });
  79. });
  80. }
  81. }
  82. module.exports = ChunkModuleIdRangePlugin;