wallet-core/node_modules/webpack/lib/optimize/RemoveParentModulesPlugin.js

66 lines
2.0 KiB
JavaScript
Raw Normal View History

2017-05-03 15:35:00 +02:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
function hasModule(chunk, module, checkedChunks) {
2017-08-14 05:01:11 +02:00
if(chunk.containsModule(module)) return [chunk];
2017-05-03 15:35:00 +02:00
if(chunk.parents.length === 0) return false;
return allHaveModule(chunk.parents.filter((c) => {
2017-08-14 05:01:11 +02:00
return !checkedChunks.has(c);
2017-05-03 15:35:00 +02:00
}), module, checkedChunks);
}
function allHaveModule(someChunks, module, checkedChunks) {
2017-08-14 05:01:11 +02:00
if(!checkedChunks) checkedChunks = new Set();
var chunks = new Set();
2017-05-03 15:35:00 +02:00
for(var i = 0; i < someChunks.length; i++) {
2017-08-14 05:01:11 +02:00
checkedChunks.add(someChunks[i]);
2017-05-03 15:35:00 +02:00
var subChunks = hasModule(someChunks[i], module, checkedChunks);
if(!subChunks) return false;
for(var index = 0; index < subChunks.length; index++) {
var item = subChunks[index];
2017-08-14 05:01:11 +02:00
chunks.add(item);
2017-05-03 15:35:00 +02:00
}
}
return chunks;
}
class RemoveParentModulesPlugin {
apply(compiler) {
compiler.plugin("compilation", (compilation) => {
compilation.plugin(["optimize-chunks-basic", "optimize-extracted-chunks-basic"], (chunks) => {
for(var index = 0; index < chunks.length; index++) {
var chunk = chunks[index];
if(chunk.parents.length === 0) continue;
// TODO consider Map when performance has improved https://gist.github.com/sokra/b36098368da7b8f6792fd7c85fca6311
var cache = Object.create(null);
2017-08-14 05:01:11 +02:00
var modules = chunk.getModules();
2017-05-03 15:35:00 +02:00
for(var i = 0; i < modules.length; i++) {
var module = modules[i];
2017-08-14 05:01:11 +02:00
var dId = module.getChunkIdsIdent();
2017-05-03 15:35:00 +02:00
var parentChunksWithModule;
2017-08-14 05:01:11 +02:00
if(dId === null) {
parentChunksWithModule = allHaveModule(chunk.parents, module);
} else if(dId in cache) {
2017-05-03 15:35:00 +02:00
parentChunksWithModule = cache[dId];
} else {
parentChunksWithModule = cache[dId] = allHaveModule(chunk.parents, module);
}
if(parentChunksWithModule) {
2017-08-14 05:01:11 +02:00
module.rewriteChunkInReasons(chunk, Array.from(parentChunksWithModule));
2017-05-03 15:35:00 +02:00
chunk.removeModule(module);
}
}
}
});
});
}
}
module.exports = RemoveParentModulesPlugin;