2017-05-03 15:35:00 +02:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
class OccurrenceOrderPlugin {
|
|
|
|
constructor(preferEntry) {
|
|
|
|
if(preferEntry !== undefined && typeof preferEntry !== "boolean") {
|
|
|
|
throw new Error("Argument should be a boolean.\nFor more info on this plugin, see https://webpack.js.org/plugins/");
|
|
|
|
}
|
|
|
|
this.preferEntry = preferEntry;
|
|
|
|
}
|
|
|
|
apply(compiler) {
|
|
|
|
const preferEntry = this.preferEntry;
|
|
|
|
compiler.plugin("compilation", (compilation) => {
|
|
|
|
compilation.plugin("optimize-module-order", (modules) => {
|
2017-08-14 05:01:11 +02:00
|
|
|
const occursInInitialChunksMap = new Map();
|
|
|
|
const occursInAllChunksMap = new Map();
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
const initialChunkChunkMap = new Map();
|
|
|
|
const entryCountMap = new Map();
|
|
|
|
modules.forEach(m => {
|
|
|
|
let initial = 0;
|
|
|
|
let entry = 0;
|
|
|
|
m.forEachChunk(c => {
|
|
|
|
if(c.isInitial()) initial++;
|
|
|
|
if(c.entryModule === m) entry++;
|
|
|
|
});
|
|
|
|
initialChunkChunkMap.set(m, initial);
|
|
|
|
entryCountMap.set(m, entry);
|
|
|
|
});
|
|
|
|
|
|
|
|
const countOccursInEntry = (sum, r) => {
|
|
|
|
if(!r.module) return sum;
|
|
|
|
return sum + initialChunkChunkMap.get(r.module);
|
|
|
|
};
|
|
|
|
const countOccurs = (sum, r) => {
|
|
|
|
if(!r.module) return sum;
|
|
|
|
return sum + r.module.getNumberOfChunks();
|
|
|
|
};
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
if(preferEntry) {
|
|
|
|
modules.forEach(m => {
|
|
|
|
const result = m.reasons.reduce(countOccursInEntry, 0) + initialChunkChunkMap.get(m) + entryCountMap.get(m);
|
|
|
|
occursInInitialChunksMap.set(m, result);
|
|
|
|
});
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
|
|
|
modules.forEach(m => {
|
|
|
|
const result = m.reasons.reduce(countOccurs, 0) + m.getNumberOfChunks() + entryCountMap.get(m);
|
|
|
|
occursInAllChunksMap.set(m, result);
|
|
|
|
});
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
modules.sort((a, b) => {
|
|
|
|
if(preferEntry) {
|
2017-08-14 05:01:11 +02:00
|
|
|
const aEntryOccurs = occursInInitialChunksMap.get(a);
|
|
|
|
const bEntryOccurs = occursInInitialChunksMap.get(b);
|
2017-05-03 15:35:00 +02:00
|
|
|
if(aEntryOccurs > bEntryOccurs) return -1;
|
|
|
|
if(aEntryOccurs < bEntryOccurs) return 1;
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
const aOccurs = occursInAllChunksMap.get(a);
|
|
|
|
const bOccurs = occursInAllChunksMap.get(b);
|
2017-05-03 15:35:00 +02:00
|
|
|
if(aOccurs > bOccurs) return -1;
|
|
|
|
if(aOccurs < bOccurs) return 1;
|
2017-08-14 05:01:11 +02:00
|
|
|
if(a.index > b.index) return 1;
|
|
|
|
if(a.index < b.index) return -1;
|
2017-05-03 15:35:00 +02:00
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
compilation.plugin("optimize-chunk-order", (chunks) => {
|
2017-08-14 05:01:11 +02:00
|
|
|
const occursInInitialChunksMap = new Map();
|
|
|
|
|
|
|
|
chunks.forEach(c => {
|
|
|
|
const result = c.parents.reduce((sum, p) => {
|
|
|
|
if(p.isInitial()) return sum + 1;
|
|
|
|
return sum;
|
|
|
|
}, 0);
|
|
|
|
return occursInInitialChunksMap.set(c, result);
|
|
|
|
});
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function occurs(c) {
|
|
|
|
return c.blocks.length;
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
chunks.sort((a, b) => {
|
2017-08-14 05:01:11 +02:00
|
|
|
const aEntryOccurs = occursInInitialChunksMap.get(a);
|
|
|
|
const bEntryOccurs = occursInInitialChunksMap.get(b);
|
2017-05-03 15:35:00 +02:00
|
|
|
if(aEntryOccurs > bEntryOccurs) return -1;
|
|
|
|
if(aEntryOccurs < bEntryOccurs) return 1;
|
|
|
|
const aOccurs = occurs(a);
|
|
|
|
const bOccurs = occurs(b);
|
|
|
|
if(aOccurs > bOccurs) return -1;
|
|
|
|
if(aOccurs < bOccurs) return 1;
|
2017-08-14 05:01:11 +02:00
|
|
|
return a.compareTo(b);
|
2017-05-03 15:35:00 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = OccurrenceOrderPlugin;
|