@@ -2,7 +2,10 @@ extern crate napi;
22extern crate napi_derive;
33extern crate oxc_resolver;
44
5- use std:: path:: { Path , PathBuf } ;
5+ use std:: {
6+ path:: { Path , PathBuf } ,
7+ sync:: Arc ,
8+ } ;
69
710use napi_derive:: napi;
811use oxc_resolver:: { ResolveOptions , Resolver } ;
@@ -37,26 +40,28 @@ pub fn sync(path: String, request: String) -> ResolveResult {
3740
3841#[ napi]
3942pub struct ResolverFactory {
40- resolver : Resolver ,
43+ resolver : Arc < Resolver > ,
4144}
4245
4346#[ napi]
4447impl ResolverFactory {
4548 #[ napi( constructor) ]
4649 pub fn new ( options : NapiResolveOptions ) -> Self {
47- Self { resolver : Resolver :: new ( Self :: normalize_options ( options) ) }
50+ Self { resolver : Arc :: new ( Resolver :: new ( Self :: normalize_options ( options) ) ) }
4851 }
4952
5053 #[ napi]
5154 pub fn default ( ) -> Self {
5255 let default_options = ResolveOptions :: default ( ) ;
53- Self { resolver : Resolver :: new ( default_options) }
56+ Self { resolver : Arc :: new ( Resolver :: new ( default_options) ) }
5457 }
5558
5659 /// Clone the resolver using the same underlying cache.
5760 #[ napi]
5861 pub fn clone_with_options ( & self , options : NapiResolveOptions ) -> Self {
59- Self { resolver : self . resolver . clone_with_options ( Self :: normalize_options ( options) ) }
62+ Self {
63+ resolver : Arc :: new ( self . resolver . clone_with_options ( Self :: normalize_options ( options) ) ) ,
64+ }
6065 }
6166
6267 /// Clear the underlying cache.
@@ -72,6 +77,14 @@ impl ResolverFactory {
7277 resolve ( & self . resolver , & path, & request)
7378 }
7479
80+ #[ allow( clippy:: needless_pass_by_value) ]
81+ #[ napi( js_name = "async" ) ]
82+ pub async fn resolve_async ( & self , path : String , request : String ) -> ResolveResult {
83+ let path = PathBuf :: from ( path) ;
84+ let resolver = self . resolver . clone ( ) ;
85+ tokio:: spawn ( async move { resolve ( & resolver, & path, & request) } ) . await . unwrap ( )
86+ }
87+
7588 fn normalize_options ( op : NapiResolveOptions ) -> ResolveOptions {
7689 let default = ResolveOptions :: default ( ) ;
7790 // merging options
0 commit comments