ChunkModuleIdRangePlugin.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.find((chunk) => chunk.name === options.name);
  15. if(!chunk) throw new Error("ChunkModuleIdRangePlugin: Chunk with name '" + options.name + "' was not found");
  16. let currentId = options.start;
  17. let chunkModules;
  18. if(options.order) {
  19. chunkModules = chunk.modules.slice();
  20. switch(options.order) {
  21. case "index":
  22. chunkModules.sort((a, b) => {
  23. return a.index - b.index;
  24. });
  25. break;
  26. case "index2":
  27. chunkModules.sort((a, b) => {
  28. return a.index2 - b.index2;
  29. });
  30. break;
  31. default:
  32. throw new Error("ChunkModuleIdRangePlugin: unexpected value of order");
  33. }
  34. } else {
  35. chunkModules = modules.filter((m) => {
  36. return m.chunks.indexOf(chunk) >= 0;
  37. });
  38. }
  39. for(let i = 0; i < chunkModules.length; i++) {
  40. const m = chunkModules[i];
  41. if(m.id === null) {
  42. m.id = currentId++;
  43. }
  44. if(options.end && currentId > options.end)
  45. break;
  46. }
  47. });
  48. });
  49. }
  50. }
  51. module.exports = ChunkModuleIdRangePlugin;