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 02:44:01 +08:00
|
|
|
"use strict";
|
|
|
|
|
2016-04-11 05:55:37 +08:00
|
|
|
function chunkContainsModule(chunk, module) {
|
2017-01-05 02:44:01 +08:00
|
|
|
const chunks = module.chunks;
|
|
|
|
const modules = chunk.modules;
|
2016-04-11 06:38:18 +08:00
|
|
|
if(chunks.length < modules.length) {
|
2016-04-11 05:55:37 +08:00
|
|
|
return chunks.indexOf(chunk) >= 0;
|
|
|
|
} else {
|
|
|
|
return modules.indexOf(module) >= 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 05:38:41 +08:00
|
|
|
function hasModule(chunk, module, checkedChunks) {
|
2016-04-11 05:55:37 +08:00
|
|
|
if(chunkContainsModule(chunk, module)) return [chunk];
|
2016-07-13 17:03:14 +08:00
|
|
|
if(chunk.parents.length === 0) return false;
|
2017-01-05 02:44:01 +08:00
|
|
|
return allHaveModule(chunk.parents.filter((c) => {
|
2016-04-11 05:38:41 +08:00
|
|
|
return checkedChunks.indexOf(c) < 0;
|
|
|
|
}), module, checkedChunks);
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
|
2016-04-11 05:38:41 +08:00
|
|
|
function allHaveModule(someChunks, module, checkedChunks) {
|
|
|
|
if(!checkedChunks) checkedChunks = [];
|
2017-01-05 02:44:01 +08:00
|
|
|
let chunks = [];
|
2016-04-11 05:38:41 +08:00
|
|
|
for(var i = 0; i < someChunks.length; i++) {
|
|
|
|
checkedChunks.push(someChunks[i]);
|
2017-01-05 02:44:01 +08:00
|
|
|
const subChunks = hasModule(someChunks[i], module, checkedChunks);
|
2016-04-11 05:38:41 +08:00
|
|
|
if(!subChunks) return false;
|
|
|
|
addToSet(chunks, subChunks);
|
|
|
|
}
|
|
|
|
return chunks;
|
2014-09-08 04:54:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function addToSet(set, items) {
|
2017-01-24 02:52:47 +08:00
|
|
|
for(var index = 0, lenItems = items.length; index < lenItems; index++) {
|
|
|
|
var item = items[index];
|
|
|
|
|
|
|
|
if(set.indexOf(item) < 0) {
|
2014-09-08 04:54:38 +08:00
|
|
|
set.push(item);
|
2017-01-24 02:52:47 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
|
2016-04-11 05:55:37 +08:00
|
|
|
function debugIds(chunks) {
|
2017-01-05 02:44:01 +08:00
|
|
|
const list = chunks.map((chunk) => {
|
2016-04-11 05:55:37 +08:00
|
|
|
return chunk.debugId;
|
|
|
|
});
|
2017-01-05 02:44:01 +08:00
|
|
|
const debugIdMissing = list.some((dId) => {
|
2016-06-20 14:21:00 +08:00
|
|
|
return typeof dId !== "number";
|
2016-06-21 03:46:27 +08:00
|
|
|
});
|
|
|
|
if(debugIdMissing)
|
|
|
|
return "no";
|
2016-04-11 05:55:37 +08:00
|
|
|
list.sort();
|
|
|
|
return list.join(",");
|
|
|
|
}
|
|
|
|
|
2017-01-05 02:44:01 +08:00
|
|
|
class RemoveParentModulesPlugin {
|
|
|
|
apply(compiler) {
|
|
|
|
compiler.plugin("compilation", (compilation) => {
|
|
|
|
compilation.plugin(["optimize-chunks-basic", "optimize-extracted-chunks-basic"], (chunks) => {
|
2017-01-24 02:52:47 +08:00
|
|
|
for(var index = 0, lenChunks = chunks.length; index < lenChunks; index++) {
|
|
|
|
var chunk = chunks[index];
|
|
|
|
|
|
|
|
var cache = {};
|
|
|
|
var modules = chunk.modules.slice();
|
|
|
|
for(var i = 0, lenChunk = modules.length; i < lenChunk; i++) {
|
|
|
|
var module = modules[i];
|
|
|
|
|
|
|
|
if(chunk.parents.length === 0) continue;
|
|
|
|
var dId = "$" + debugIds(module.chunks);
|
|
|
|
var parentChunksWithModule;
|
2017-01-05 02:44:01 +08:00
|
|
|
if((dId in cache) && dId !== "$no") {
|
|
|
|
parentChunksWithModule = cache[dId];
|
|
|
|
} else {
|
|
|
|
parentChunksWithModule = cache[dId] = allHaveModule(chunk.parents, module);
|
|
|
|
}
|
|
|
|
if(parentChunksWithModule) {
|
|
|
|
module.rewriteChunkInReasons(chunk, parentChunksWithModule);
|
|
|
|
chunk.removeModule(module);
|
|
|
|
}
|
2017-01-24 02:52:47 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
|
|
|
});
|
2017-01-05 02:44:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = RemoveParentModulesPlugin;
|