Skip to content

Commit 7a15344

Browse files
committed
reverted new algorithm for removing modules from parent chunks
1 parent 5d981fe commit 7a15344

1 file changed

Lines changed: 18 additions & 101 deletions

File tree

lib/optimize/RemoveParentModulesPlugin.js

Lines changed: 18 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,24 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
function listToSet(list, chunk) {
6-
var set = {};
7-
list.forEach(function(module) {
8-
set[module._RemoveParentModulesPlugin_index] = {
9-
module: module,
10-
chunks: [chunk]
11-
};
12-
});
13-
return set;
14-
}
15-
16-
function mergeSets(a, b) {
17-
var newSet = {};
18-
Object.keys(a).forEach(function(key) {
19-
var item = a[key];
20-
newSet[key] = {
21-
module: item.module,
22-
chunks: item.chunks
23-
};
24-
});
25-
Object.keys(b).forEach(function(key) {
26-
var item = b[key];
27-
newSet[key] = {
28-
module: item.module,
29-
chunks: item.chunks
30-
};
31-
});
32-
return newSet;
33-
}
34-
35-
function intersectSets(a, b) {
36-
var newSet = {};
37-
Object.keys(a).forEach(function(key) {
38-
var aItem = a[key];
39-
var bItem = b[key];
40-
if(bItem) {
41-
newSet[key] = {
42-
module: aItem.module,
43-
chunks: aItem.chunks.concat(bItem.chunks)
44-
};
45-
}
46-
});
47-
return newSet;
5+
function hasModule(chunk, module, checkedChunks) {
6+
if(chunk.modules.indexOf(module) >= 0) return [chunk];
7+
if(chunk.entry) return false;
8+
return allHaveModule(chunk.parents.filter(function(c) {
9+
return checkedChunks.indexOf(c) < 0;
10+
}), module, checkedChunks);
4811
}
4912

50-
function intersectAll(map) {
51-
var keys = Object.keys(map);
52-
if(keys.length === 0)
53-
return null;
54-
return keys.map(function(key) {
55-
return map[key];
56-
}).reduce(intersectSets);
13+
function allHaveModule(someChunks, module, checkedChunks) {
14+
if(!checkedChunks) checkedChunks = [];
15+
var chunks = [];
16+
for(var i = 0; i < someChunks.length; i++) {
17+
checkedChunks.push(someChunks[i]);
18+
var subChunks = hasModule(someChunks[i], module, checkedChunks);
19+
if(!subChunks) return false;
20+
addToSet(chunks, subChunks);
21+
}
22+
return chunks;
5723
}
5824

5925
function addToSet(set, items) {
@@ -63,65 +29,16 @@ function addToSet(set, items) {
6329
});
6430
}
6531

66-
function toStr(set) {
67-
return Object.keys(set).map(function(key) {
68-
return set[key].module.request.substr(-12);
69-
}).join(", ");
70-
}
71-
7232
function RemoveParentModulesPlugin() {}
7333
module.exports = RemoveParentModulesPlugin;
7434

7535
RemoveParentModulesPlugin.prototype.apply = function(compiler) {
7636
compiler.plugin("compilation", function(compilation) {
7737
compilation.plugin(["optimize-chunks-basic", "optimize-extracted-chunks-basic"], function(chunks) {
78-
this.modules.forEach(function(module, idx) {
79-
module._RemoveParentModulesPlugin_index = idx;
80-
})
81-
var todo = chunks.slice();
82-
todo.forEach(function(chunk, idx) {
83-
chunk._RemoveParentModulesPlugin_processed = false;
84-
chunk._RemoveParentModulesPlugin_availableModulesByChunk = {};
85-
chunk._RemoveParentModulesPlugin_index = idx;
86-
})
87-
for(var i = 0; i < todo.length; i++) {
88-
var chunk = todo[i];
89-
var index = chunk._RemoveParentModulesPlugin_index;
90-
var availableModules = chunk._RemoveParentModulesPlugin_availableModules = intersectAll(chunk._RemoveParentModulesPlugin_availableModulesByChunk);
91-
if(chunk.chunks.length === 0) {
92-
chunk._RemoveParentModulesPlugin_processed = true;
93-
continue;
94-
}
95-
var set = listToSet(chunk.modules, chunk);
96-
if(availableModules)
97-
set = mergeSets(set, availableModules);
98-
chunk.chunks.forEach(function(child) {
99-
var availableModules = child._RemoveParentModulesPlugin_availableModulesByChunk[index];
100-
child._RemoveParentModulesPlugin_availableModulesByChunk[index] = set;
101-
if(!availableModules || Object.keys(availableModules).length !== Object.keys(set).length) {
102-
if(child._RemoveParentModulesPlugin_processed) {
103-
todo.push(child);
104-
child._RemoveParentModulesPlugin_processed = false;
105-
}
106-
}
107-
});
108-
chunk._RemoveParentModulesPlugin_processed = true;
109-
}
11038
chunks.forEach(function(chunk) {
111-
var availableModules = chunk._RemoveParentModulesPlugin_availableModules;
112-
delete chunk._RemoveParentModulesPlugin_availableModulesByChunk;
113-
delete chunk._RemoveParentModulesPlugin_availableModules;
114-
delete chunk._RemoveParentModulesPlugin_processed;
115-
delete chunk._RemoveParentModulesPlugin_index;
116-
if(chunk.entry) return;
117-
if(!availableModules) return;
11839
chunk.modules.slice().forEach(function(module) {
119-
var info = availableModules[module._RemoveParentModulesPlugin_index];
120-
if(!info) return;
121-
var parentChunksWithModule = info.chunks;
122-
parentChunksWithModule = parentChunksWithModule.filter(function(chunk, idx) {
123-
return parentChunksWithModule.indexOf(chunk) === idx;
124-
});
40+
if(chunk.entry) return;
41+
var parentChunksWithModule = allHaveModule(chunk.parents, module);
12542
if(parentChunksWithModule) {
12643
module.rewriteChunkInReasons(chunk, parentChunksWithModule);
12744
chunk.removeModule(module);

0 commit comments

Comments
 (0)