|
| 1 | +use itertools::Itertools; |
| 2 | +use rayon::prelude::*; |
| 3 | +use rspack_core::{ |
| 4 | + Compilation, CompilationOptimizeDependencies, ExportProvided, ExportsInfo, ExportsInfoGetter, |
| 5 | + Plugin, PrefetchExportsInfoMode, UsageState, UsedNameItem, incremental::IncrementalPasses, |
| 6 | +}; |
| 7 | +use rspack_error::Result; |
| 8 | +use rspack_hook::{plugin, plugin_hook}; |
| 9 | +use rustc_hash::{FxHashMap, FxHashSet}; |
| 10 | + |
| 11 | +#[plugin] |
| 12 | +#[derive(Debug, Default)] |
| 13 | +pub struct InlineExportsPlugin; |
| 14 | + |
| 15 | +// We put it to optimize_dependencies hook instead of optimize_code_generation hook like MangleExportsPlugin |
| 16 | +// because inline can affect side effects optimization (not the SideEffectsFlagPlugin does, the buildChunkGraph |
| 17 | +// does), buildChunkGraph can use dependency condition to determine if a dependency still active, if the dependency |
| 18 | +// imported export is inlined, then the dependency is inactive and will not be processed by buildChunkGraph, if a |
| 19 | +// module's all exports are all being inlined, then the module can be eliminated by buildChunkGraph |
| 20 | +#[plugin_hook(CompilationOptimizeDependencies for InlineExportsPlugin, stage = 100)] |
| 21 | +async fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result<Option<bool>> { |
| 22 | + if let Some(diagnostic) = compilation.incremental.disable_passes( |
| 23 | + IncrementalPasses::MODULES_HASHES, |
| 24 | + "InlineExportsPlugin (optimization.inlineExports = true)", |
| 25 | + "it requires calculating the export names of all the modules, which is a global effect", |
| 26 | + ) { |
| 27 | + if let Some(diagnostic) = diagnostic { |
| 28 | + compilation.push_diagnostic(diagnostic); |
| 29 | + } |
| 30 | + compilation.cgm_hash_artifact.clear(); |
| 31 | + } |
| 32 | + |
| 33 | + let mut mg = compilation.get_module_graph_mut(); |
| 34 | + let modules = mg.modules(); |
| 35 | + |
| 36 | + let mut visited: FxHashSet<ExportsInfo> = FxHashSet::default(); |
| 37 | + |
| 38 | + let mut q = modules |
| 39 | + .keys() |
| 40 | + .filter_map(|mid| { |
| 41 | + let mgm = mg.module_graph_module_by_identifier(mid)?; |
| 42 | + Some(mgm.exports) |
| 43 | + }) |
| 44 | + .collect_vec(); |
| 45 | + |
| 46 | + while !q.is_empty() { |
| 47 | + let items = std::mem::take(&mut q); |
| 48 | + let batch = items |
| 49 | + .par_iter() |
| 50 | + .filter_map(|exports_info| { |
| 51 | + let exports_info_data = |
| 52 | + ExportsInfoGetter::prefetch(exports_info, &mg, PrefetchExportsInfoMode::Default); |
| 53 | + let export_list = { |
| 54 | + // If there are other usage (e.g. `import { Kind } from './enum'; Kind;`) in any runtime, |
| 55 | + // then we cannot inline this export. |
| 56 | + if exports_info_data.other_exports_info().get_used(None) != UsageState::Unused { |
| 57 | + return None; |
| 58 | + } |
| 59 | + exports_info_data |
| 60 | + .exports() |
| 61 | + .map(|(_, export_info_data)| { |
| 62 | + let do_inline = !export_info_data.has_used_name() |
| 63 | + && export_info_data.can_inline() == Some(true) |
| 64 | + && matches!(export_info_data.provided(), Some(ExportProvided::Provided)); |
| 65 | + |
| 66 | + let nested_exports_info = if export_info_data.exports_info_owned() { |
| 67 | + let used = export_info_data.get_used(None); |
| 68 | + if used == UsageState::OnlyPropertiesUsed || used == UsageState::Unused { |
| 69 | + export_info_data.exports_info() |
| 70 | + } else { |
| 71 | + None |
| 72 | + } |
| 73 | + } else { |
| 74 | + None |
| 75 | + }; |
| 76 | + (export_info_data.id(), nested_exports_info, do_inline) |
| 77 | + }) |
| 78 | + .collect::<Vec<_>>() |
| 79 | + }; |
| 80 | + |
| 81 | + Some((*exports_info, export_list)) |
| 82 | + }) |
| 83 | + .collect::<FxHashMap<_, _>>(); |
| 84 | + |
| 85 | + visited.extend(batch.keys()); |
| 86 | + for (_, export_list) in batch { |
| 87 | + q.extend( |
| 88 | + export_list |
| 89 | + .into_iter() |
| 90 | + .filter_map(|(export_info, nested_exports_info, do_inline)| { |
| 91 | + if do_inline { |
| 92 | + let data = export_info.as_data_mut(&mut mg); |
| 93 | + data.set_used_name(UsedNameItem::Inlined( |
| 94 | + data |
| 95 | + .can_inline_provide() |
| 96 | + .expect("should have provided inline value") |
| 97 | + .clone(), |
| 98 | + )); |
| 99 | + } |
| 100 | + nested_exports_info |
| 101 | + }) |
| 102 | + .filter(|e| !visited.contains(e)), |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + Ok(None) |
| 108 | +} |
| 109 | + |
| 110 | +impl Plugin for InlineExportsPlugin { |
| 111 | + fn apply(&self, ctx: &mut rspack_core::ApplyContext<'_>) -> Result<()> { |
| 112 | + ctx |
| 113 | + .compilation_hooks |
| 114 | + .optimize_dependencies |
| 115 | + .tap(optimize_dependencies::new(self)); |
| 116 | + Ok(()) |
| 117 | + } |
| 118 | +} |
0 commit comments