OccurrenceOrderPlugin.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class OccurrenceOrderPlugin {
  7. constructor(preferEntry) {
  8. if(preferEntry !== undefined && typeof preferEntry !== "boolean") {
  9. throw new Error("Argument should be a boolean.\nFor more info on this plugin, see https://webpack.js.org/plugins/");
  10. }
  11. this.preferEntry = preferEntry;
  12. }
  13. apply(compiler) {
  14. const preferEntry = this.preferEntry;
  15. compiler.plugin("compilation", (compilation) => {
  16. compilation.plugin("optimize-module-order", (modules) => {
  17. const occursInInitialChunksMap = new Map();
  18. const occursInAllChunksMap = new Map();
  19. const initialChunkChunkMap = new Map();
  20. const entryCountMap = new Map();
  21. modules.forEach(m => {
  22. let initial = 0;
  23. let entry = 0;
  24. m.forEachChunk(c => {
  25. if(c.isInitial()) initial++;
  26. if(c.entryModule === m) entry++;
  27. });
  28. initialChunkChunkMap.set(m, initial);
  29. entryCountMap.set(m, entry);
  30. });
  31. const countOccursInEntry = (sum, r) => {
  32. if(!r.module) return sum;
  33. return sum + initialChunkChunkMap.get(r.module);
  34. };
  35. const countOccurs = (sum, r) => {
  36. if(!r.module) return sum;
  37. return sum + r.module.getNumberOfChunks();
  38. };
  39. if(preferEntry) {
  40. modules.forEach(m => {
  41. const result = m.reasons.reduce(countOccursInEntry, 0) + initialChunkChunkMap.get(m) + entryCountMap.get(m);
  42. occursInInitialChunksMap.set(m, result);
  43. });
  44. }
  45. modules.forEach(m => {
  46. const result = m.reasons.reduce(countOccurs, 0) + m.getNumberOfChunks() + entryCountMap.get(m);
  47. occursInAllChunksMap.set(m, result);
  48. });
  49. modules.sort((a, b) => {
  50. if(preferEntry) {
  51. const aEntryOccurs = occursInInitialChunksMap.get(a);
  52. const bEntryOccurs = occursInInitialChunksMap.get(b);
  53. if(aEntryOccurs > bEntryOccurs) return -1;
  54. if(aEntryOccurs < bEntryOccurs) return 1;
  55. }
  56. const aOccurs = occursInAllChunksMap.get(a);
  57. const bOccurs = occursInAllChunksMap.get(b);
  58. if(aOccurs > bOccurs) return -1;
  59. if(aOccurs < bOccurs) return 1;
  60. if(a.index > b.index) return 1;
  61. if(a.index < b.index) return -1;
  62. return 0;
  63. });
  64. });
  65. compilation.plugin("optimize-chunk-order", (chunks) => {
  66. const occursInInitialChunksMap = new Map();
  67. chunks.forEach(c => {
  68. const result = c.parents.reduce((sum, p) => {
  69. if(p.isInitial()) return sum + 1;
  70. return sum;
  71. }, 0);
  72. return occursInInitialChunksMap.set(c, result);
  73. });
  74. function occurs(c) {
  75. return c.blocks.length;
  76. }
  77. chunks.sort((a, b) => {
  78. const aEntryOccurs = occursInInitialChunksMap.get(a);
  79. const bEntryOccurs = occursInInitialChunksMap.get(b);
  80. if(aEntryOccurs > bEntryOccurs) return -1;
  81. if(aEntryOccurs < bEntryOccurs) return 1;
  82. const aOccurs = occurs(a);
  83. const bOccurs = occurs(b);
  84. if(aOccurs > bOccurs) return -1;
  85. if(aOccurs < bOccurs) return 1;
  86. return a.compareTo(b);
  87. });
  88. });
  89. });
  90. }
  91. }
  92. module.exports = OccurrenceOrderPlugin;