ChunkModuleIdRangePlugin.js 1.5 KB

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