2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2016-04-11 05:55:37 +08:00
|
|
|
function chunkContainsModule(chunk, module) {
|
|
|
|
var chunks = module.chunks;
|
|
|
|
var 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;
|
2016-04-11 05:38:41 +08:00
|
|
|
return allHaveModule(chunk.parents.filter(function(c) {
|
|
|
|
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 = [];
|
|
|
|
var chunks = [];
|
|
|
|
for(var i = 0; i < someChunks.length; i++) {
|
|
|
|
checkedChunks.push(someChunks[i]);
|
|
|
|
var subChunks = hasModule(someChunks[i], module, checkedChunks);
|
|
|
|
if(!subChunks) return false;
|
|
|
|
addToSet(chunks, subChunks);
|
|
|
|
}
|
|
|
|
return chunks;
|
2014-09-08 04:54:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function addToSet(set, items) {
|
|
|
|
items.forEach(function(item) {
|
|
|
|
if(set.indexOf(item) < 0)
|
|
|
|
set.push(item);
|
|
|
|
});
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
|
2016-04-11 05:55:37 +08:00
|
|
|
function debugIds(chunks) {
|
|
|
|
var list = chunks.map(function(chunk) {
|
|
|
|
return chunk.debugId;
|
|
|
|
});
|
2016-06-21 03:46:27 +08:00
|
|
|
var debugIdMissing = list.some(function(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(",");
|
|
|
|
}
|
|
|
|
|
2015-07-13 06:20:09 +08:00
|
|
|
function RemoveParentModulesPlugin() {}
|
2013-01-31 01:49:25 +08:00
|
|
|
module.exports = RemoveParentModulesPlugin;
|
|
|
|
|
|
|
|
RemoveParentModulesPlugin.prototype.apply = function(compiler) {
|
|
|
|
compiler.plugin("compilation", function(compilation) {
|
2015-07-07 06:11:13 +08:00
|
|
|
compilation.plugin(["optimize-chunks-basic", "optimize-extracted-chunks-basic"], function(chunks) {
|
2013-01-31 01:49:25 +08:00
|
|
|
chunks.forEach(function(chunk) {
|
2016-04-11 05:55:37 +08:00
|
|
|
var cache = {};
|
2013-01-31 01:49:25 +08:00
|
|
|
chunk.modules.slice().forEach(function(module) {
|
2016-07-13 17:03:14 +08:00
|
|
|
if(chunk.parents.length === 0) return;
|
2016-04-11 05:55:37 +08:00
|
|
|
var dId = "$" + debugIds(module.chunks);
|
|
|
|
var parentChunksWithModule;
|
2016-06-16 06:52:50 +08:00
|
|
|
if((dId in cache) && dId !== "$no") {
|
2016-04-11 05:55:37 +08:00
|
|
|
parentChunksWithModule = cache[dId];
|
|
|
|
} else {
|
|
|
|
parentChunksWithModule = cache[dId] = allHaveModule(chunk.parents, module);
|
|
|
|
}
|
2014-09-08 04:54:38 +08:00
|
|
|
if(parentChunksWithModule) {
|
|
|
|
module.rewriteChunkInReasons(chunk, parentChunksWithModule);
|
2013-01-31 01:49:25 +08:00
|
|
|
chunk.removeModule(module);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|