1- use std:: { fmt:: Debug , sync:: Arc } ;
1+ use std:: { fmt:: Debug , path :: Path , sync:: Arc } ;
22
3- use napi:: bindgen_prelude:: { Either4 , Promise } ;
3+ use napi:: {
4+ bindgen_prelude:: { Either4 , Function , FunctionCallContext , Promise } ,
5+ Either , Env ,
6+ } ;
47use napi_derive:: napi;
58use rspack_core:: {
69 ExternalItem , ExternalItemFnCtx , ExternalItemFnResult , ExternalItemValue ,
@@ -10,7 +13,10 @@ use rspack_napi::threadsafe_function::ThreadsafeFunction;
1013use rspack_regex:: RspackRegex ;
1114use rustc_hash:: FxHashMap as HashMap ;
1215
13- use crate :: JsResolver ;
16+ use crate :: {
17+ callbackify, normalize_raw_resolve_options_with_dependency_type, ErrorCode ,
18+ RawResolveOptionsWithDependencyType , ResolveRequest ,
19+ } ;
1420
1521#[ napi( object) ]
1622pub struct RawHttpExternalsRspackPluginOptions {
@@ -105,12 +111,74 @@ impl RawExternalItemFnCtx {
105111 }
106112 }
107113
108- #[ napi]
109- pub fn get_resolver ( & self ) -> JsResolver {
110- JsResolver :: new (
111- self . resolver_factory . clone ( ) ,
112- self . resolve_options_with_dependency_type . clone ( ) ,
113- )
114+ #[ napi(
115+ ts_return_type = "(context: string, path: string, callback: (error?: Error, text?: string) => void) => void"
116+ ) ]
117+ pub fn get_resolve < ' a > (
118+ & self ,
119+ env : & ' a Env ,
120+ options : Option < RawResolveOptionsWithDependencyType > ,
121+ ) -> napi:: Result < Function < ' a , ( String , String , Function < ' static > ) , ( ) > > {
122+ let first = Arc :: new ( self . resolve_options_with_dependency_type . clone ( ) ) ;
123+ let second = Arc :: new (
124+ normalize_raw_resolve_options_with_dependency_type ( options, first. resolve_to_context )
125+ . map_err ( |e| napi:: Error :: from_reason ( e. to_string ( ) ) ) ?,
126+ ) ;
127+ let resolver_factory = self . resolver_factory . clone ( ) ;
128+
129+ let f: Function < ( String , String , Function < ' static > ) , ( ) > =
130+ env. create_function_from_closure ( "resolve" , move |ctx : FunctionCallContext | {
131+ let context = ctx. get :: < String > ( 0 ) ?;
132+ let request = ctx. get :: < String > ( 1 ) ?;
133+ let callback = ctx. get :: < Function < ' static > > ( 2 ) ?;
134+
135+ let first_clone = first. clone ( ) ;
136+ let second_clone = second. clone ( ) ;
137+ let resolver_factory = resolver_factory. clone ( ) ;
138+
139+ callbackify (
140+ callback,
141+ async move {
142+ let merged_resolve_options = match second_clone. resolve_options . as_ref ( ) {
143+ Some ( second_resolve_options) => match first_clone. resolve_options . as_ref ( ) {
144+ Some ( resolve_options) => Some ( Box :: new (
145+ resolve_options
146+ . clone ( )
147+ . merge ( * second_resolve_options. clone ( ) ) ,
148+ ) ) ,
149+ None => Some ( second_resolve_options. clone ( ) ) ,
150+ } ,
151+ None => first_clone. resolve_options . clone ( ) ,
152+ } ;
153+
154+ let merged_options = ResolveOptionsWithDependencyType {
155+ resolve_options : merged_resolve_options,
156+ resolve_to_context : second_clone. resolve_to_context ,
157+ dependency_category : second_clone. dependency_category ,
158+ } ;
159+ let resolver = resolver_factory. get ( merged_options) ;
160+
161+ match resolver. resolve ( Path :: new ( & context) , & request) . await {
162+ Ok ( rspack_core:: ResolveResult :: Resource ( resource) ) => {
163+ let resolve_request = ResolveRequest :: from ( resource) ;
164+ Ok ( match serde_json:: to_string ( & resolve_request) {
165+ Ok ( json) => Either :: < String , ( ) > :: A ( json) ,
166+ Err ( _) => Either :: B ( ( ) ) ,
167+ } )
168+ }
169+ Ok ( rspack_core:: ResolveResult :: Ignored ) => Ok ( Either :: B ( ( ) ) ) ,
170+ Err ( err) => Err ( napi:: Error :: new (
171+ ErrorCode :: Napi ( napi:: Status :: GenericFailure ) ,
172+ format ! ( "{err:?}" ) ,
173+ ) ) ,
174+ }
175+ } ,
176+ None :: < fn ( ) > ,
177+ )
178+ . map_err ( |e| napi:: Error :: from_reason ( e. reason . to_string ( ) ) )
179+ } ) ?;
180+
181+ Ok ( f)
114182 }
115183}
116184
0 commit comments