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:38:41 +08:00
|
|
|
function hasModule(chunk, module, checkedChunks) {
|
2017-04-19 04:30:18 +08:00
|
|
|
if(module.isInChunk(chunk)) 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-26 03:39:24 +08:00
|
|
|
var chunks = [];
|
2016-04-11 05:38:41 +08:00
|
|
|
for(var i = 0; i < someChunks.length; i++) {
|
|
|
|
checkedChunks.push(someChunks[i]);
|
2017-01-26 03:39:24 +08:00
|
|
|
var subChunks = hasModule(someChunks[i], module, checkedChunks);
|
2016-04-11 05:38:41 +08:00
|
|
|
if(!subChunks) return false;
|
2017-01-26 03:39:24 +08:00
|
|
|
|
|
|
|
for(var index = 0; index < subChunks.length; index++) {
|
|
|
|
var item = subChunks[index];
|
|
|
|
|
|
|
|
if(!chunks.length || chunks.indexOf(item) < 0) {
|
|
|
|
chunks.push(item);
|
|
|
|
}
|
|
|
|
}
|
2016-04-11 05:38:41 +08:00
|
|
|
}
|
|
|
|
return chunks;
|
2014-09-08 04:54:38 +08:00
|
|
|
}
|
|
|
|
|
2016-04-11 05:55:37 +08:00
|
|
|
|
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-25 03:39:38 +08:00
|
|
|
for(var index = 0; index < chunks.length; index++) {
|
2017-01-24 02:52:47 +08:00
|
|
|
var chunk = chunks[index];
|
2017-02-23 20:31:05 +08:00
|
|
|
if(chunk.parents.length === 0) continue;
|
2017-01-24 02:52:47 +08:00
|
|
|
|
2017-01-26 03:47:11 +08:00
|
|
|
// TODO consider Map when performance has improved https://gist.github.com/sokra/b36098368da7b8f6792fd7c85fca6311
|
2017-01-25 03:39:38 +08:00
|
|
|
var cache = Object.create(null);
|
2017-01-24 02:52:47 +08:00
|
|
|
var modules = chunk.modules.slice();
|
2017-01-25 03:39:38 +08:00
|
|
|
for(var i = 0; i < modules.length; i++) {
|
2017-01-24 02:52:47 +08:00
|
|
|
var module = modules[i];
|
|
|
|
|
2017-04-19 04:30:18 +08:00
|
|
|
var dId = module.getChunkIdsIdent();
|
2017-01-24 02:52:47 +08:00
|
|
|
var parentChunksWithModule;
|
2017-04-19 04:30:18 +08:00
|
|
|
if(dId !== null && (dId in cache)) {
|
2017-01-05 02:44:01 +08:00
|
|
|
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;
|