webpack/lib/optimize/RemoveEmptyChunksPlugin.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2013-01-31 01:49:25 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
const { STAGE_BASIC, STAGE_ADVANCED } = require("../OptimizationStages");
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
class RemoveEmptyChunksPlugin {
/**
2020-04-23 16:48:36 +08:00
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
2018-02-25 09:00:20 +08:00
compiler.hooks.compilation.tap("RemoveEmptyChunksPlugin", compilation => {
/**
2018-09-06 22:59:11 +08:00
* @param {Iterable<Chunk>} chunks the chunks array
* @returns {void}
*/
2018-02-25 09:00:20 +08:00
const handler = chunks => {
const chunkGraph = compilation.chunkGraph;
2018-09-06 22:59:11 +08:00
for (const chunk of chunks) {
2018-05-13 06:15:39 +08:00
if (
chunkGraph.getNumberOfChunkModules(chunk) === 0 &&
2018-05-13 06:15:39 +08:00
!chunk.hasRuntime() &&
chunkGraph.getNumberOfEntryModules(chunk) === 0
2018-05-13 06:15:39 +08:00
) {
compilation.chunkGraph.disconnectChunk(chunk);
2018-09-06 22:59:11 +08:00
compilation.chunks.delete(chunk);
2018-05-13 06:15:39 +08:00
}
}
2017-12-14 04:35:39 +08:00
};
// TODO do it once
compilation.hooks.optimizeChunks.tap(
2018-12-09 19:54:17 +08:00
{
name: "RemoveEmptyChunksPlugin",
stage: STAGE_BASIC
2018-12-09 19:54:17 +08:00
},
2018-02-25 09:00:20 +08:00
handler
);
compilation.hooks.optimizeChunks.tap(
2018-12-09 19:54:17 +08:00
{
name: "RemoveEmptyChunksPlugin",
stage: STAGE_ADVANCED
2018-12-09 19:54:17 +08:00
},
handler
);
2013-01-31 01:49:25 +08:00
});
}
}
module.exports = RemoveEmptyChunksPlugin;