@@ -33,7 +33,7 @@ pub struct OptimizeSharedConfig {
3333#[ derive( Debug , Clone ) ]
3434pub struct OptimizeDependencyReferencedExportsPluginOptions {
3535 pub shared : Vec < OptimizeSharedConfig > ,
36- pub ignored_runtime : Vec < String > ,
36+ pub inject_used_exports : bool ,
3737}
3838
3939#[ derive( Debug , Clone ) ]
@@ -45,13 +45,14 @@ struct SharedEntryData {
4545#[ derive( Debug , Clone ) ]
4646pub struct OptimizeDependencyReferencedExportsPlugin {
4747 shared_map : FxHashMap < String , SharedEntryData > ,
48- ignored_runtime : FxHashSet < String > ,
4948 shared_referenced_exports : Arc < RwLock < FxHashMap < String , FxHashMap < String , FxHashSet < String > > > > > ,
49+ inject_used_exports : bool ,
5050}
5151
5252impl OptimizeDependencyReferencedExportsPlugin {
5353 pub fn new ( options : OptimizeDependencyReferencedExportsPluginOptions ) -> Self {
5454 let mut shared_map = FxHashMap :: default ( ) ;
55+ let inject_used_exports = options. inject_used_exports . clone ( ) ;
5556 for config in options. shared . into_iter ( ) . filter ( |c| c. treeshake ) {
5657 let atoms = config
5758 . used_exports
@@ -66,13 +67,12 @@ impl OptimizeDependencyReferencedExportsPlugin {
6667 ) ;
6768 }
6869
69- let ignored_runtime = options. ignored_runtime . into_iter ( ) . collect ( ) ;
7070 let shared_referenced_exports = Arc :: new ( RwLock :: new ( FxHashMap :: <
7171 String ,
7272 FxHashMap < String , FxHashSet < String > > ,
7373 > :: default ( ) ) ) ;
7474
75- Self :: new_inner ( shared_map, ignored_runtime , shared_referenced_exports )
75+ Self :: new_inner ( shared_map, shared_referenced_exports , inject_used_exports )
7676 }
7777
7878 fn apply_custom_exports ( & self ) {
@@ -267,11 +267,11 @@ async fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result<O
267267 let exports_info = module_graph_mut. get_exports_info ( module_id) ;
268268 let exports_info_data = exports_info. as_data_mut ( & mut module_graph_mut) ;
269269
270- for ( runtime , exports) in runtime_reference_exports. iter ( ) {
270+ for ( _runtime , exports) in runtime_reference_exports. iter ( ) {
271271 for export_name in exports {
272272 let export_atom = Atom :: from ( export_name. as_str ( ) ) ;
273273 if let Some ( export_info) = exports_info_data. named_exports_mut ( & export_atom) {
274- let runtime_spec = RuntimeSpec :: from_iter ( [ runtime. clone ( ) . into ( ) ] ) ;
274+ // let runtime_spec = RuntimeSpec::from_iter([runtime.clone().into()]);
275275 // let status =
276276 // export_info.set_used(rspack_core::UsageState::Used, Some(&runtime_spec));
277277 // set used by runtime when set_owned_used_in_unknown_way remove
@@ -304,29 +304,23 @@ async fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result<O
304304 if can_update_module_used_stage {
305305 // mark used exports for dependencies and blocks dependencies
306306 // create a helper closure to update dependency's export info
307- runtime_reference_exports
308- . iter ( )
309- . for_each ( |( runtime_name, _) | {
310- let runtime_spec = RuntimeSpec :: from_iter ( [ runtime_name. clone ( ) . into ( ) ] ) ;
311- for export_info in exports_info_data. exports_mut ( ) . values_mut ( ) {
312- export_info. set_used_conditionally (
313- Box :: new ( |used| * used == rspack_core:: UsageState :: Unknown ) ,
314- rspack_core:: UsageState :: Unused ,
315- None ,
316- ) ;
317- let export_used_status: rspack_core:: UsageState =
318- export_info. get_used ( Some ( & runtime_spec) ) ;
319- export_info. set_can_mangle_provide ( Some ( false ) ) ;
320- export_info. set_can_mangle_use ( Some ( false ) ) ;
321- }
322- exports_info_data
323- . other_exports_info_mut ( )
324- . set_used_conditionally (
325- Box :: new ( |used| * used == rspack_core:: UsageState :: Unknown ) ,
326- rspack_core:: UsageState :: Unused ,
327- None ,
328- ) ;
329- } ) ;
307+ // set used by runtime when set_owned_used_in_unknown_way remove
308+ for export_info in exports_info_data. exports_mut ( ) . values_mut ( ) {
309+ export_info. set_used_conditionally (
310+ Box :: new ( |used| * used == rspack_core:: UsageState :: Unknown ) ,
311+ rspack_core:: UsageState :: Unused ,
312+ None ,
313+ ) ;
314+ export_info. set_can_mangle_provide ( Some ( false ) ) ;
315+ export_info. set_can_mangle_use ( Some ( false ) ) ;
316+ }
317+ exports_info_data
318+ . other_exports_info_mut ( )
319+ . set_used_conditionally (
320+ Box :: new ( |used| * used == rspack_core:: UsageState :: Unknown ) ,
321+ rspack_core:: UsageState :: Unused ,
322+ None ,
323+ ) ;
330324 }
331325 }
332326 }
@@ -492,10 +486,12 @@ impl Plugin for OptimizeDependencyReferencedExportsPlugin {
492486 . compilation_hooks
493487 . optimize_dependencies
494488 . tap ( optimize_dependencies:: new ( self ) ) ;
495- ctx
496- . compilation_hooks
497- . additional_tree_runtime_requirements
498- . tap ( additional_tree_runtime_requirements:: new ( self ) ) ;
489+ if self . inject_used_exports {
490+ ctx
491+ . compilation_hooks
492+ . additional_tree_runtime_requirements
493+ . tap ( additional_tree_runtime_requirements:: new ( self ) ) ;
494+ }
499495 Ok ( ( ) )
500496 }
501497}
0 commit comments