@@ -12,7 +12,7 @@ use std::{
1212
1313use napi:: { Task , bindgen_prelude:: AsyncTask } ;
1414use napi_derive:: napi;
15- use oxc_resolver:: { PackageJson , ResolveOptions , Resolver } ;
15+ use oxc_resolver:: { PackageJson , ResolveError , ResolveOptions , Resolver } ;
1616
1717use self :: options:: { NapiResolveOptions , StrOrStrList } ;
1818
@@ -24,6 +24,7 @@ mod tracing;
2424pub struct ResolveResult {
2525 pub path : Option < String > ,
2626 pub error : Option < String > ,
27+ pub builtin : Option < Builtin > ,
2728 /// Module type for this path.
2829 ///
2930 /// Enable with `ResolveOptions#moduleType`.
@@ -37,23 +38,47 @@ pub struct ResolveResult {
3738 pub package_json_path : Option < String > ,
3839}
3940
41+ /// Node.js builtin module when `Options::builtin_modules` is enabled.
42+ #[ napi( object) ]
43+ pub struct Builtin {
44+ /// Resolved module.
45+ ///
46+ /// Always prefixed with "node:" in compliance with the ESM specification.
47+ pub resolved : String ,
48+
49+ /// Whether the request was prefixed with `node:` or not.
50+ /// `fs` -> `false`.
51+ /// `node:fs` returns `true`.
52+ pub is_runtime_module : bool ,
53+ }
54+
4055fn resolve ( resolver : & Resolver , path : & Path , request : & str ) -> ResolveResult {
4156 match resolver. resolve ( path, request) {
4257 Ok ( resolution) => ResolveResult {
4358 path : Some ( resolution. full_path ( ) . to_string_lossy ( ) . to_string ( ) ) ,
4459 error : None ,
60+ builtin : None ,
4561 module_type : resolution. module_type ( ) . map ( ModuleType :: from) ,
4662 package_json_path : resolution
4763 . package_json ( )
4864 . and_then ( |p| p. path ( ) . to_str ( ) )
4965 . map ( |p| p. to_string ( ) ) ,
5066 } ,
51- Err ( err) => ResolveResult {
52- path : None ,
53- module_type : None ,
54- error : Some ( err. to_string ( ) ) ,
55- package_json_path : None ,
56- } ,
67+ Err ( err) => {
68+ let error = err. to_string ( ) ;
69+ ResolveResult {
70+ path : None ,
71+ builtin : match err {
72+ ResolveError :: Builtin { resolved, is_runtime_module } => {
73+ Some ( Builtin { resolved, is_runtime_module } )
74+ }
75+ _ => None ,
76+ } ,
77+ module_type : None ,
78+ error : Some ( error) ,
79+ package_json_path : None ,
80+ }
81+ }
5782 }
5883}
5984
@@ -250,11 +275,11 @@ impl ResolverFactory {
250275 symlinks : op. symlinks . unwrap_or ( default. symlinks ) ,
251276 builtin_modules : op. builtin_modules . unwrap_or ( default. builtin_modules ) ,
252277 module_type : op. module_type . unwrap_or ( default. module_type ) ,
253- #[ cfg( feature = "yarn_pnp" ) ]
254- pnp_manifest : default. pnp_manifest ,
255278 allow_package_exports_in_directory_resolve : op
256279 . allow_package_exports_in_directory_resolve
257280 . unwrap_or ( default. allow_package_exports_in_directory_resolve ) ,
281+ #[ cfg( feature = "yarn_pnp" ) ]
282+ pnp_manifest : default. pnp_manifest ,
258283 }
259284 }
260285}
0 commit comments