webpack/lib/optimize/RemoveEmptyChunksPlugin.js

61 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";
2025-07-03 17:06:45 +08:00
const { STAGE_ADVANCED, STAGE_BASIC } = require("../OptimizationStages");
/** @typedef {import("../Chunk")} Chunk */
/** @typedef {import("../Compiler")} Compiler */
2025-04-23 20:03:37 +08:00
const PLUGIN_NAME = "RemoveEmptyChunksPlugin";
class RemoveEmptyChunksPlugin {
/**
2020-04-23 16:48:36 +08:00
* Apply the plugin
* @param {Compiler} compiler the compiler instance
* @returns {void}
*/
apply(compiler) {
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
/**
2018-09-06 22:59:11 +08:00
* @param {Iterable<Chunk>} chunks the chunks array
* @returns {void}
*/
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
{
2025-04-23 20:03:37 +08:00
name: PLUGIN_NAME,
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
{
2025-04-23 20:03:37 +08:00
name: PLUGIN_NAME,
stage: STAGE_ADVANCED
2018-12-09 19:54:17 +08:00
},
handler
);
2013-01-31 01:49:25 +08:00
});
}
}
module.exports = RemoveEmptyChunksPlugin;