@@ -21,8 +21,8 @@ use oxc::{
2121 } ,
2222} ;
2323use oxc_resolver:: {
24- CompilerOptionsSerde , EnforceExtension , PackageType , ResolveOptions , Resolver , TsConfigSerde ,
25- TsconfigOptions , TsconfigReferences ,
24+ Cache , CompilerOptionsSerde , EnforceExtension , PackageType , Resolution , ResolveOptions ,
25+ Resolver , TsConfigSerde , TsconfigOptions , TsconfigReferences ,
2626} ;
2727use phf:: Set ;
2828
@@ -486,16 +486,7 @@ pub fn create_resolve<'env>(
486486 . map ( |p| p. contains ( NODE_MODULES_PATH ) )
487487 . unwrap_or ( false )
488488 {
489- #[ cfg_attr( not( target_os = "windows" ) , allow( unused_mut) ) ]
490- let mut url = if resolution. query ( ) . is_some ( ) || resolution. fragment ( ) . is_some ( ) {
491- format ! ( "{PATH_PREFIX}{}" , resolution. full_path( ) . to_string_lossy( ) )
492- } else {
493- format ! ( "{PATH_PREFIX}{}" , resolution. path( ) . to_string_lossy( ) )
494- } ;
495- #[ cfg( target_os = "windows" ) ]
496- {
497- url = url. replace ( "\\ " , "/" ) ;
498- }
489+ let url = oxc_resolved_path_to_url ( & resolution) ;
499490 return Ok ( Either :: A ( ResolveFnOutput {
500491 short_circuit : Some ( true ) ,
501492 format : {
@@ -522,6 +513,29 @@ pub fn create_resolve<'env>(
522513 url,
523514 import_attributes : Some ( Either :: A ( context. import_attributes . clone ( ) ) ) ,
524515 } ) ) ;
516+ } else {
517+ let format = resolution
518+ . package_json ( )
519+ . and_then ( |p| {
520+ p. r#type . map ( |p| match p {
521+ PackageType :: CommonJs => "commonjs" ,
522+ PackageType :: Module => "module" ,
523+ } )
524+ } )
525+ . or_else ( || match p. extension ( ) . and_then ( |ext| ext. to_str ( ) ) {
526+ Some ( "cjs" | "cts" | "node" ) => Some ( "commonjs" ) ,
527+ Some ( "mts" | "mjs" ) => Some ( "module" ) ,
528+ _ => None ,
529+ } )
530+ . unwrap_or ( "commonjs" )
531+ . to_string ( ) ;
532+ tracing:: debug!( "oxc resolved path node_modules: {} {}" , p. display( ) , format) ;
533+ return Ok ( Either :: A ( ResolveFnOutput {
534+ format : Either3 :: A ( format) ,
535+ short_circuit : Some ( true ) ,
536+ url : oxc_resolved_path_to_url ( & resolution) ,
537+ import_attributes : Some ( Either :: A ( context. import_attributes . clone ( ) ) ) ,
538+ } ) ) ;
525539 }
526540 }
527541
@@ -809,3 +823,17 @@ fn join_errors(errors: Vec<OxcDiagnostic>, source_str: &str) -> String {
809823 . collect :: < Vec < _ > > ( )
810824 . join ( "\n " )
811825}
826+
827+ fn oxc_resolved_path_to_url < C : Cache > ( resolution : & Resolution < C > ) -> String {
828+ #[ cfg_attr( not( target_os = "windows" ) , allow( unused_mut) ) ]
829+ let mut url = if resolution. query ( ) . is_some ( ) || resolution. fragment ( ) . is_some ( ) {
830+ format ! ( "{PATH_PREFIX}{}" , resolution. full_path( ) . to_string_lossy( ) )
831+ } else {
832+ format ! ( "{PATH_PREFIX}{}" , resolution. path( ) . to_string_lossy( ) )
833+ } ;
834+ #[ cfg( target_os = "windows" ) ]
835+ {
836+ url = url. replace ( "\\ " , "/" ) ;
837+ }
838+ url
839+ }
0 commit comments