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";
|
|
|
|
|
2017-11-25 00:23:33 +08:00
|
|
|
const Queue = require("../util/Queue");
|
2018-01-10 05:39:16 +08:00
|
|
|
const ChunkGroup = require("../ChunkGroup");
|
2017-11-25 00:23:33 +08:00
|
|
|
|
2018-01-06 04:01:00 +08:00
|
|
|
const getParentChunksWithModule = (currentChunk, module) => {
|
|
|
|
const chunks = [];
|
|
|
|
const stack = new Set(currentChunk.parentsIterable);
|
2017-01-26 03:39:24 +08:00
|
|
|
|
2018-01-06 04:01:00 +08:00
|
|
|
for(const chunk of stack) {
|
|
|
|
if(chunk.containsModule(module)) {
|
|
|
|
chunks.push(chunk);
|
|
|
|
} else {
|
|
|
|
for(const parent of chunk.parentsIterable) {
|
|
|
|
stack.add(parent);
|
|
|
|
}
|
2017-01-26 03:39:24 +08:00
|
|
|
}
|
2016-04-11 05:38:41 +08:00
|
|
|
}
|
2018-01-06 04:01:00 +08:00
|
|
|
|
2016-04-11 05:38:41 +08:00
|
|
|
return chunks;
|
2017-11-08 18:32:05 +08:00
|
|
|
};
|
2014-09-08 04:54:38 +08:00
|
|
|
|
2017-01-05 02:44:01 +08:00
|
|
|
class RemoveParentModulesPlugin {
|
|
|
|
apply(compiler) {
|
2017-12-06 22:01:25 +08:00
|
|
|
compiler.hooks.compilation.tap("RemoveParentModulesPlugin", (compilation) => {
|
2017-12-14 04:35:39 +08:00
|
|
|
const handler = (chunks) => {
|
2018-01-10 05:39:16 +08:00
|
|
|
const queue = new Queue();
|
2017-11-25 00:23:33 +08:00
|
|
|
const availableModulesMap = new Map();
|
2017-01-24 02:52:47 +08:00
|
|
|
|
2017-11-25 00:23:33 +08:00
|
|
|
for(const chunk of chunks) {
|
|
|
|
// initialize available modules for chunks without parents
|
2018-01-10 05:39:16 +08:00
|
|
|
if(chunk.getNumberOfParents() === 0) {
|
2017-11-25 00:23:33 +08:00
|
|
|
availableModulesMap.set(chunk, new Set());
|
2018-01-10 05:39:16 +08:00
|
|
|
for(const child of chunk.chunksIterable)
|
|
|
|
queue.enqueue(child);
|
|
|
|
}
|
2017-11-25 00:23:33 +08:00
|
|
|
}
|
2017-01-24 02:52:47 +08:00
|
|
|
|
2017-11-25 00:23:33 +08:00
|
|
|
while(queue.length > 0) {
|
|
|
|
const chunk = queue.dequeue();
|
|
|
|
let availableModules = availableModulesMap.get(chunk);
|
|
|
|
let changed = false;
|
2018-01-10 05:39:16 +08:00
|
|
|
if(chunk instanceof ChunkGroup) {
|
|
|
|
let allParentAvailableModules = new Set();
|
|
|
|
for(const parent of chunk.parentsIterable) {
|
|
|
|
const availableModulesInParent = availableModulesMap.get(parent);
|
|
|
|
if(availableModulesInParent === undefined) {
|
|
|
|
allParentAvailableModules = undefined;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(const module of availableModulesInParent)
|
|
|
|
allParentAvailableModules.add(module);
|
|
|
|
}
|
|
|
|
if(availableModules === undefined) {
|
|
|
|
// if we have not own info yet: create new entry
|
|
|
|
availableModules = allParentAvailableModules;
|
|
|
|
availableModulesMap.set(chunk, availableModules);
|
|
|
|
changed = true;
|
|
|
|
} else {
|
|
|
|
for(const m of availableModules) {
|
|
|
|
if(!allParentAvailableModules.has(m)) {
|
|
|
|
availableModules.delete(m);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(const parent of chunk.parentsIterable) {
|
|
|
|
const availableModulesInParent = availableModulesMap.get(parent);
|
|
|
|
if(availableModulesInParent !== undefined) {
|
|
|
|
// If we know the available modules in parent: process these
|
|
|
|
if(availableModules === undefined) {
|
|
|
|
// if we have not own info yet: create new entry
|
|
|
|
availableModules = new Set(availableModulesInParent);
|
|
|
|
if(!(parent instanceof ChunkGroup)) {
|
|
|
|
for(const m of parent.modulesIterable)
|
|
|
|
availableModules.add(m);
|
|
|
|
}
|
|
|
|
availableModulesMap.set(chunk, availableModules);
|
|
|
|
changed = true;
|
|
|
|
} else {
|
|
|
|
if(parent instanceof ChunkGroup) {
|
|
|
|
for(const m of availableModules) {
|
|
|
|
if(!availableModulesInParent.has(m)) {
|
|
|
|
availableModules.delete(m);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for(const m of availableModules) {
|
|
|
|
if(!parent.containsModule(m) && !availableModulesInParent.has(m)) {
|
|
|
|
availableModules.delete(m);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 00:23:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-05 02:44:01 +08:00
|
|
|
}
|
2017-01-24 02:52:47 +08:00
|
|
|
}
|
2017-11-25 00:23:33 +08:00
|
|
|
if(changed) {
|
|
|
|
// if something changed: enqueue our children
|
|
|
|
for(const child of chunk.chunksIterable)
|
|
|
|
queue.enqueue(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we have available modules for every chunk
|
|
|
|
|
|
|
|
for(const chunk of chunks) {
|
|
|
|
// remove modules from chunk if they are already available
|
|
|
|
const availableModules = availableModulesMap.get(chunk);
|
|
|
|
const modules = new Set(chunk.modulesIterable);
|
|
|
|
const toRemove = new Set();
|
|
|
|
if(modules.size < availableModules.size) {
|
|
|
|
for(const m of modules)
|
|
|
|
if(availableModules.has(m))
|
|
|
|
toRemove.add(m);
|
|
|
|
} else {
|
|
|
|
for(const m of availableModules)
|
|
|
|
if(modules.has(m))
|
|
|
|
toRemove.add(m);
|
|
|
|
}
|
|
|
|
for(const module of toRemove) {
|
2018-01-06 04:01:00 +08:00
|
|
|
module.rewriteChunkInReasons(chunk, getParentChunksWithModule(chunk, module));
|
2017-11-25 00:23:33 +08:00
|
|
|
chunk.removeModule(module);
|
|
|
|
}
|
2017-01-24 02:52:47 +08:00
|
|
|
}
|
2017-12-14 04:35:39 +08:00
|
|
|
};
|
|
|
|
compilation.hooks.optimizeChunksBasic.tap("RemoveParentModulesPlugin", handler);
|
|
|
|
compilation.hooks.optimizeExtractedChunksBasic.tap("RemoveParentModulesPlugin", handler);
|
2013-01-31 01:49:25 +08:00
|
|
|
});
|
2017-01-05 02:44:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = RemoveParentModulesPlugin;
|