2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-01-05 13:42:36 +08:00
|
|
|
"use strict";
|
2013-01-31 01:49:25 +08:00
|
|
|
|
|
|
|
function getChunkIdentifier(chunk) {
|
2017-04-21 16:05:56 +08:00
|
|
|
return chunk.mapModules((m) => {
|
2013-01-31 01:49:25 +08:00
|
|
|
return m.identifier();
|
2014-02-15 18:29:15 +08:00
|
|
|
}).sort().join(", ");
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
|
2017-01-05 13:42:36 +08:00
|
|
|
class MergeDuplicateChunksPlugin {
|
|
|
|
|
|
|
|
apply(compiler) {
|
|
|
|
compiler.plugin("compilation", (compilation) => {
|
|
|
|
compilation.plugin("optimize-chunks-basic", (chunks) => {
|
|
|
|
const map = {};
|
|
|
|
chunks.slice().forEach((chunk) => {
|
|
|
|
if(chunk.hasRuntime() || chunk.hasEntryModule()) return;
|
|
|
|
const ident = getChunkIdentifier(chunk);
|
|
|
|
if(map[ident]) {
|
|
|
|
if(map[ident].integrate(chunk, "duplicate"))
|
|
|
|
chunks.splice(chunks.indexOf(chunk), 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
map[ident] = chunk;
|
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
|
|
|
});
|
2017-01-05 13:42:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = MergeDuplicateChunksPlugin;
|