#![feature(box_patterns)]
use farmfe_core::{
config::{Config, Mode},
plugin::Plugin,
};
use crate::remove_hot_update::remove_useless_hot_update_stmts;
pub mod init_tree_shake_module_map;
pub mod mark_initial_side_effects;
pub mod module;
pub mod remove_hot_update;
pub mod statement_graph;
pub mod tree_shake_modules;
pub struct FarmPluginTreeShake;
impl FarmPluginTreeShake {
pub fn new(_: &Config) -> Self {
Self {}
}
}
impl Plugin for FarmPluginTreeShake {
fn name(&self) -> &'static str {
"FarmPluginTreeShake"
}
fn optimize_module_graph(
&self,
module_graph: &mut farmfe_core::module::module_graph::ModuleGraph,
context: &std::sync::Arc<farmfe_core::context::CompilationContext>,
) -> farmfe_core::error::Result<Option<()>> {
let mut tree_shake_modules_map =
init_tree_shake_module_map::init_tree_shake_module_map(module_graph, context);
let entry_module_ids = mark_initial_side_effects::mark_initial_side_effects(
module_graph,
&mut tree_shake_modules_map,
);
let modules_to_remove = tree_shake_modules::tree_shake_modules(
entry_module_ids,
module_graph,
&mut tree_shake_modules_map,
);
for module in module_graph.modules_mut() {
if let Some(tree_shake_module) = tree_shake_modules_map.get(&module.id) {
let mut used_exports = tree_shake_module.handled_used_exports.to_string_vec();
used_exports.sort();
module.used_exports = used_exports;
}
}
for module_id in modules_to_remove {
module_graph.remove_module(&module_id);
}
if matches!(context.config.mode, Mode::Production) {
remove_useless_hot_update_stmts(module_graph);
}
tree_shake_modules::remove_export_idents::remove_export_idents(module_graph);
Ok(Some(()))
}
}