farmfe_plugin_tree_shake 2.2.0

Tree shake plugin of farm.
use farmfe_core::{
  HashMap,
  module::{ModuleId, module_graph::ModuleGraph},
  parking_lot::Mutex,
  rayon::iter::{IntoParallelIterator, ParallelIterator},
  swc_common::GLOBALS,
};

use crate::module::TreeShakeModule;

pub fn init_tree_shake_module_map(
  module_graph: &mut ModuleGraph,
  context: &std::sync::Arc<farmfe_core::context::CompilationContext>,
) -> HashMap<ModuleId, TreeShakeModule> {
  let tree_shake_modules_map = Mutex::new(HashMap::<ModuleId, TreeShakeModule>::default());
  module_graph
    .modules_mut()
    .into_par_iter()
    .for_each(|module| {
      if !module.module_type.is_script() || module.external {
        return;
      }
      let globals = context.meta.get_globals(&module.id);

      GLOBALS.set(globals.value(), || {
        let tree_shake_module = TreeShakeModule::new(module);
        tree_shake_modules_map
          .lock()
          .insert(module.id.clone(), tree_shake_module);
      });
    });
  tree_shake_modules_map.into_inner()
}