ChunkModuleIdRangePlugin.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const sortByIndex = (a, b) => {
  7. return a.index - b.index;
  8. };
  9. const sortByIndex2 = (a, b) => {
  10. return a.index2 - b.index2;
  11. };
  12. class ChunkModuleIdRangePlugin {
  13. constructor(options) {
  14. this.options = options;
  15. }
  16. apply(compiler) {
  17. const options = this.options;
  18. compiler.hooks.compilation.tap("ChunkModuleIdRangePlugin", compilation => {
  19. compilation.hooks.moduleIds.tap("ChunkModuleIdRangePlugin", modules => {
  20. const chunk = compilation.chunks.find(
  21. chunk => chunk.name === options.name
  22. );
  23. if (!chunk) {
  24. throw new Error(
  25. `ChunkModuleIdRangePlugin: Chunk with name '${
  26. options.name
  27. }"' was not found`
  28. );
  29. }
  30. let chunkModules;
  31. if (options.order) {
  32. chunkModules = Array.from(chunk.modulesIterable);
  33. switch (options.order) {
  34. case "index":
  35. chunkModules.sort(sortByIndex);
  36. break;
  37. case "index2":
  38. chunkModules.sort(sortByIndex2);
  39. break;
  40. default:
  41. throw new Error(
  42. "ChunkModuleIdRangePlugin: unexpected value of order"
  43. );
  44. }
  45. } else {
  46. chunkModules = modules.filter(m => {
  47. return m.chunksIterable.has(chunk);
  48. });
  49. }
  50. let currentId = options.start || 0;
  51. for (let i = 0; i < chunkModules.length; i++) {
  52. const m = chunkModules[i];
  53. if (m.id === null) {
  54. m.id = currentId++;
  55. }
  56. if (options.end && currentId > options.end) break;
  57. }
  58. });
  59. });
  60. }
  61. }
  62. module.exports = ChunkModuleIdRangePlugin;