2016-05-05 16:13:50 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2017-01-05 06:21:38 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
class EnsureChunkConditionsPlugin {
|
2016-05-05 16:13:50 +08:00
|
|
|
|
2017-01-05 06:21:38 +08:00
|
|
|
apply(compiler) {
|
2017-12-06 22:01:25 +08:00
|
|
|
compiler.hooks.compilation.tap("EnsureChunkConditionsPlugin", (compilation) => {
|
2017-06-01 22:10:27 +08:00
|
|
|
const triesMap = new Map();
|
2017-12-14 04:35:39 +08:00
|
|
|
const handler = (chunks) => {
|
2017-01-05 06:21:38 +08:00
|
|
|
let changed = false;
|
|
|
|
chunks.forEach((chunk) => {
|
2018-01-04 19:48:09 +08:00
|
|
|
for(const module of chunk.modulesIterable) {
|
2018-01-18 18:31:38 +08:00
|
|
|
if(!module.chunkCondition) continue;
|
2017-01-05 06:21:38 +08:00
|
|
|
if(!module.chunkCondition(chunk)) {
|
2017-06-01 22:10:27 +08:00
|
|
|
let usedChunks = triesMap.get(module);
|
|
|
|
if(!usedChunks) triesMap.set(module, usedChunks = new Set());
|
|
|
|
usedChunks.add(chunk);
|
2017-02-05 07:40:10 +08:00
|
|
|
const newChunks = [];
|
2017-09-22 22:38:47 +08:00
|
|
|
for(const parent of chunk.parentsIterable) {
|
2017-06-01 22:10:27 +08:00
|
|
|
if(!usedChunks.has(parent)) {
|
2017-01-05 06:21:38 +08:00
|
|
|
parent.addModule(module);
|
2017-08-16 20:05:06 +08:00
|
|
|
module.addChunk(parent);
|
2017-01-05 06:21:38 +08:00
|
|
|
newChunks.push(parent);
|
|
|
|
}
|
2017-09-22 22:38:47 +08:00
|
|
|
}
|
2017-01-05 06:21:38 +08:00
|
|
|
module.rewriteChunkInReasons(chunk, newChunks);
|
|
|
|
chunk.removeModule(module);
|
|
|
|
changed = true;
|
|
|
|
}
|
2018-01-04 19:48:09 +08:00
|
|
|
}
|
2016-05-05 16:13:50 +08:00
|
|
|
});
|
2017-01-05 06:21:38 +08:00
|
|
|
if(changed) return true;
|
2017-12-14 04:35:39 +08:00
|
|
|
};
|
|
|
|
compilation.hooks.optimizeChunksBasic.tap("EnsureChunkConditionsPlugin", handler);
|
|
|
|
compilation.hooks.optimizeExtractedChunksBasic.tap("EnsureChunkConditionsPlugin", handler);
|
2016-05-05 16:13:50 +08:00
|
|
|
});
|
2017-01-05 06:21:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = EnsureChunkConditionsPlugin;
|