@@ -84,46 +84,40 @@ impl CollectSharedEntryPlugin {
8484 }
8585 }
8686
87- // 2) Fallback: walk to node_modules/<pkg>[/...] and read package.json
87+ // 2) Fallback: read version from the deepest node_modules/<pkg>/ package.json
8888 let path = Path :: new ( request) ;
89- let mut package_json_path = PathBuf :: new ( ) ;
90- let mut found_node_modules = false ;
91- let mut need_two_segments = false ;
92- let mut captured = false ;
93-
94- for component in path. components ( ) {
95- let comp_str = component. as_os_str ( ) . to_string_lossy ( ) ;
96- package_json_path. push ( comp_str. as_ref ( ) ) ;
97- if !found_node_modules && comp_str == "node_modules" {
98- found_node_modules = true ;
99- continue ;
100- }
101- if found_node_modules && !captured {
102- if comp_str. starts_with ( '@' ) {
103- // scoped package: need scope + name
104- need_two_segments = true ;
105- continue ;
106- } else {
107- if need_two_segments {
108- // this is the name after scope
109- package_json_path. push ( "package.json" ) ;
110- captured = true ;
111- break ;
112- } else {
113- // unscoped package name is this segment
114- package_json_path. push ( "package.json" ) ;
115- captured = true ;
116- break ;
89+ let comps: Vec < String > = path
90+ . components ( )
91+ . map ( |c| c. as_os_str ( ) . to_string_lossy ( ) . to_string ( ) )
92+ . collect ( ) ;
93+ if let Some ( idx) = comps. iter ( ) . rposition ( |c| c == "node_modules" ) {
94+ let mut pkg_parts: Vec < & str > = Vec :: new ( ) ;
95+ if let Some ( next) = comps. get ( idx + 1 ) {
96+ if next. starts_with ( '@' ) {
97+ if let Some ( next2) = comps. get ( idx + 2 ) {
98+ pkg_parts. push ( next. as_str ( ) ) ;
99+ pkg_parts. push ( next2. as_str ( ) ) ;
117100 }
101+ } else {
102+ pkg_parts. push ( next. as_str ( ) ) ;
118103 }
119104 }
120- }
121-
122- if captured && package_json_path. exists ( ) {
123- if let Ok ( content) = std:: fs:: read_to_string ( & package_json_path) {
124- if let Ok ( json) = serde_json:: from_str :: < serde_json:: Value > ( & content) {
125- if let Some ( version) = json. get ( "version" ) . and_then ( |v| v. as_str ( ) ) {
126- return Some ( version. to_string ( ) ) ;
105+ if !pkg_parts. is_empty ( ) {
106+ let mut package_json_path = PathBuf :: new ( ) ;
107+ for c in comps. iter ( ) . take ( idx + 1 ) {
108+ package_json_path. push ( c) ;
109+ }
110+ for p in & pkg_parts {
111+ package_json_path. push ( p) ;
112+ }
113+ package_json_path. push ( "package.json" ) ;
114+ if package_json_path. exists ( ) {
115+ if let Ok ( content) = std:: fs:: read_to_string ( & package_json_path) {
116+ if let Ok ( json) = serde_json:: from_str :: < serde_json:: Value > ( & content) {
117+ if let Some ( version) = json. get ( "version" ) . and_then ( |v| v. as_str ( ) ) {
118+ return Some ( version. to_string ( ) ) ;
119+ }
120+ }
127121 }
128122 }
129123 }
@@ -355,6 +349,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
355349 . infer_version ( & resource)
356350 . await
357351 . unwrap_or_else ( || "" . to_string ( ) ) ;
352+ dbg ! ( & version, & resource) ;
358353 let pair = [ resource, version] ;
359354 if !reqs. iter ( ) . any ( |p| p[ 0 ] == pair[ 0 ] && p[ 1 ] == pair[ 1 ] ) {
360355 reqs. push ( pair) ;
@@ -407,75 +402,20 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
407402 Ok ( ( ) )
408403}
409404
410- #[ plugin_hook( NormalModuleFactoryFactorize for CollectSharedEntryPlugin ) ]
411- async fn factorize ( & self , data : & mut ModuleFactoryCreateData ) -> Result < Option < BoxModule > > {
412- let dep = data. dependencies [ 0 ]
413- . as_module_dependency ( )
414- . expect ( "should be module dependency" ) ;
415- if matches ! (
416- dep. dependency_type( ) ,
417- DependencyType :: ConsumeSharedFallback | DependencyType :: ProvideModuleForShared
418- ) {
419- return Ok ( None ) ;
420- }
421- let request = dep. request ( ) ;
422-
423- // Reuse the matching logic from consume_shared_plugin
424- let consumes = self . get_matched_consumes ( ) ;
425-
426- // 1. Exact match - use `unresolved`
427- if let Some ( matched) = consumes. unresolved . get ( request) {
428- self
429- . record_entry ( & data. context , request, matched. clone ( ) , |d| {
430- data. diagnostics . push ( d)
431- } )
432- . await ;
433- return Ok ( None ) ;
434- }
435-
436- // 2. Prefix match - use `prefixed`
437- for ( prefix, options) in & consumes. prefixed {
438- if request. starts_with ( prefix) {
439- let remainder = & request[ prefix. len ( ) ..] ;
440- self
441- . record_entry (
442- & data. context ,
443- request,
444- Arc :: new ( ConsumeOptions {
445- import : options. import . as_ref ( ) . map ( |i| i. to_owned ( ) + remainder) ,
446- import_resolved : options. import_resolved . clone ( ) ,
447- share_key : options. share_key . clone ( ) + remainder,
448- share_scope : options. share_scope . clone ( ) ,
449- required_version : options. required_version . clone ( ) ,
450- package_name : options. package_name . clone ( ) ,
451- strict_version : options. strict_version ,
452- singleton : options. singleton ,
453- eager : options. eager ,
454- } ) ,
455- |d| data. diagnostics . push ( d) ,
456- )
457- . await ;
458- return Ok ( None ) ;
459- }
460- }
461-
462- Ok ( None )
463- }
464-
465405impl Plugin for CollectSharedEntryPlugin {
466406 fn name ( & self ) -> & ' static str {
467407 "rspack.CollectSharedEntryPlugin"
468408 }
469409
470410 fn apply ( & self , ctx : & mut rspack_core:: ApplyContext < ' _ > ) -> Result < ( ) > {
471- ctx
472- . compiler_hooks
473- . this_compilation
474- . tap ( this_compilation:: new ( self ) ) ;
475- ctx
476- . normal_module_factory_hooks
477- . factorize
478- . tap ( factorize:: new ( self ) ) ;
411+ // ctx
412+ // .compiler_hooks
413+ // .this_compilation
414+ // .tap(this_compilation::new(self));
415+ // ctx
416+ // .normal_module_factory_hooks
417+ // .factorize
418+ // .tap(factorize::new(self));
479419 ctx
480420 . compilation_hooks
481421 . process_assets
0 commit comments