1- use std:: collections:: HashMap ;
1+ use std:: { collections:: HashMap , sync :: Arc } ;
22
33use napi:: bindgen_prelude:: * ;
44use napi_derive:: napi;
55use rspack_core:: { rspack_sources:: SourceMap , LoaderContext , RunnerContext } ;
66use rspack_loader_runner:: { LoaderItem , State as LoaderState } ;
7- use rspack_napi:: {
8- napi:: JsString , string:: JsStringExt , threadsafe_js_value_ref:: ThreadsafeJsValueRef ,
9- } ;
7+ use rspack_napi:: { threadsafe_js_value_ref:: ThreadsafeJsValueRef , JsUtf16Buffer } ;
108
119use crate :: { JsModuleWrapper , JsResourceData , JsRspackError } ;
1210
1311#[ napi( object) ]
1412pub struct JsSourceMap {
1513 pub version : u8 ,
16- pub file : Option < JsString > ,
17- pub sources : Vec < JsString > ,
18- pub sources_content : Option < Vec < JsString > > ,
19- pub names : Vec < JsString > ,
20- pub mappings : JsString ,
21- pub source_root : Option < JsString > ,
14+ pub file : Option < JsUtf16Buffer > ,
15+ pub sources : Vec < JsUtf16Buffer > ,
16+ pub sources_content : Option < Vec < JsUtf16Buffer > > ,
17+ pub names : Vec < JsUtf16Buffer > ,
18+ pub mappings : JsUtf16Buffer ,
19+ pub source_root : Option < JsUtf16Buffer > ,
2220}
2321
24- pub struct JsSourceMapWrapper ( SourceMap ) ;
22+ impl From < & SourceMap > for JsSourceMap {
23+ fn from ( value : & SourceMap ) -> Self {
24+ let file = value. file ( ) . map ( Into :: into) ;
2525
26- impl JsSourceMapWrapper {
27- pub fn new ( source_map : SourceMap ) -> Self {
28- Self ( source_map)
29- }
26+ let sources = value. sources ( ) . iter ( ) . map ( Into :: into) . collect :: < Vec < _ > > ( ) ;
3027
31- pub fn take ( self ) -> SourceMap {
32- self . 0
33- }
34- }
28+ let sources_content = value
29+ . sources_content ( )
30+ . iter ( )
31+ . map ( Into :: into)
32+ . collect :: < Vec < _ > > ( ) ;
3533
36- impl ToNapiValue for JsSourceMapWrapper {
37- unsafe fn to_napi_value ( env : sys:: napi_env , val : Self ) -> Result < sys:: napi_value > {
38- let env_wrapper = Env :: from_raw ( env) ;
39-
40- let file = match val. 0 . file ( ) {
41- Some ( s) => Some ( env_wrapper. create_string ( s) ?) ,
42- None => None ,
43- } ;
44- let mut sources = Vec :: with_capacity ( val. 0 . sources ( ) . len ( ) ) ;
45- for source in val. 0 . sources ( ) {
46- sources. push ( env_wrapper. create_string ( source) ?) ;
47- }
48- let mut sources_content = Vec :: with_capacity ( val. 0 . sources_content ( ) . len ( ) ) ;
49- for source_content in val. 0 . sources_content ( ) {
50- sources_content. push ( env_wrapper. create_string ( source_content) ?) ;
51- }
52- let mut names = Vec :: with_capacity ( val. 0 . sources_content ( ) . len ( ) ) ;
53- for name in val. 0 . names ( ) {
54- names. push ( env_wrapper. create_string ( name) ?) ;
55- }
56- let mappings = env_wrapper. create_string ( val. 0 . mappings ( ) ) ?;
57- let source_root = match val. 0 . source_root ( ) {
58- Some ( s) => Some ( env_wrapper. create_string ( s) ?) ,
59- None => None ,
60- } ;
34+ let names = value. names ( ) . iter ( ) . map ( Into :: into) . collect :: < Vec < _ > > ( ) ;
6135
62- let js_source_map = JsSourceMap {
36+ let mappings = value. mappings ( ) . into ( ) ;
37+
38+ let source_root = value. source_root ( ) . map ( Into :: into) ;
39+
40+ Self {
6341 version : 3 ,
6442 file,
6543 sources,
@@ -71,37 +49,55 @@ impl ToNapiValue for JsSourceMapWrapper {
7149 names,
7250 mappings,
7351 source_root,
74- } ;
75- ToNapiValue :: to_napi_value ( env, js_source_map)
52+ }
7653 }
7754}
7855
79- impl FromNapiValue for JsSourceMapWrapper {
80- unsafe fn from_napi_value ( env : sys:: napi_env , napi_val : sys:: napi_value ) -> Result < Self > {
81- let js_source_map: JsSourceMap = FromNapiValue :: from_napi_value ( env, napi_val) ?;
82-
83- let sources_content = match js_source_map. sources_content {
84- Some ( sources_content) => sources_content
85- . into_iter ( )
86- . map ( |source| source. into_string ( ) )
87- . collect :: < Vec < _ > > ( ) ,
88- None => vec ! [ ] ,
89- } ;
56+ impl From < & JsSourceMap > for SourceMap {
57+ fn from ( value : & JsSourceMap ) -> Self {
58+ let file = value. file . as_ref ( ) . map ( |file| file. to_string ( ) ) ;
9059
91- Ok ( JsSourceMapWrapper ( SourceMap :: new (
92- js_source_map. mappings . into_string ( ) ,
93- js_source_map
60+ let sources = Arc :: from (
61+ value
9462 . sources
95- . into_iter ( )
96- . map ( |source| source. into_string ( ) )
63+ . iter ( )
64+ . map ( |source| source. to_string ( ) )
9765 . collect :: < Vec < _ > > ( ) ,
98- sources_content,
99- js_source_map
66+ ) ;
67+
68+ let sources_content = Arc :: from (
69+ value
70+ . sources_content
71+ . as_ref ( )
72+ . map ( |sources_content| {
73+ sources_content
74+ . iter ( )
75+ . map ( |source_content| source_content. to_string ( ) )
76+ . collect :: < Vec < _ > > ( )
77+ } )
78+ . unwrap_or_default ( ) ,
79+ ) ;
80+
81+ let names = Arc :: from (
82+ value
10083 . names
101- . into_iter ( )
102- . map ( |source| source . into_string ( ) )
84+ . iter ( )
85+ . map ( |name| name . to_string ( ) )
10386 . collect :: < Vec < _ > > ( ) ,
104- ) ) )
87+ ) ;
88+
89+ let mappings = Arc :: from ( value. mappings . to_string ( ) ) ;
90+
91+ let source_root = value
92+ . source_root
93+ . as_ref ( )
94+ . map ( |source_root| Arc :: from ( source_root. to_string ( ) ) ) ;
95+
96+ let mut source_map = Self :: new ( mappings, sources, sources_content, names) ;
97+ source_map. set_file ( file) ;
98+ source_map. set_source_root ( source_root) ;
99+
100+ source_map
105101 }
106102}
107103
@@ -159,18 +155,18 @@ pub struct JsLoaderContext {
159155 pub hot : bool ,
160156
161157 /// Content maybe empty in pitching stage
162- pub content : Either3 < Null , Buffer , String > ,
158+ pub content : Either3 < Null , Buffer , JsUtf16Buffer > ,
163159 #[ napi( ts_type = "any" ) ]
164160 pub additional_data : Option < ThreadsafeJsValueRef < Unknown > > ,
165161 #[ napi( js_name = "__internal__parseMeta" ) ]
166- pub parse_meta : HashMap < String , String > ,
162+ pub parse_meta : HashMap < String , JsUtf16Buffer > ,
167163 #[ napi( ts_type = "JsSourceMap" ) ]
168- pub source_map : Option < JsSourceMapWrapper > ,
164+ pub source_map : Option < JsSourceMap > ,
169165 pub cacheable : bool ,
170- pub file_dependencies : Vec < String > ,
171- pub context_dependencies : Vec < String > ,
172- pub missing_dependencies : Vec < String > ,
173- pub build_dependencies : Vec < String > ,
166+ pub file_dependencies : Vec < JsUtf16Buffer > ,
167+ pub context_dependencies : Vec < JsUtf16Buffer > ,
168+ pub missing_dependencies : Vec < JsUtf16Buffer > ,
169+ pub build_dependencies : Vec < JsUtf16Buffer > ,
174170
175171 pub loader_items : Vec < JsLoaderItem > ,
176172 pub loader_index : i32 ,
@@ -199,37 +195,42 @@ impl TryFrom<&mut LoaderContext<RunnerContext>> for JsLoaderContext {
199195 hot : cx. hot ,
200196 content : match cx. content ( ) {
201197 Some ( c) => match c {
202- rspack_core:: Content :: String ( s) => Either3 :: C ( s. to_string ( ) ) ,
198+ rspack_core:: Content :: String ( s) => Either3 :: C ( s. into ( ) ) ,
203199 rspack_core:: Content :: Buffer ( vec) => Either3 :: B ( vec. clone ( ) . into ( ) ) ,
204200 } ,
205201 None => Either3 :: A ( Null ) ,
206202 } ,
207- parse_meta : cx. parse_meta . clone ( ) . into_iter ( ) . collect ( ) ,
203+ parse_meta : cx
204+ . parse_meta
205+ . clone ( )
206+ . into_iter ( )
207+ . map ( |( key, value) | ( key, ( & value) . into ( ) ) )
208+ . collect ( ) ,
208209 additional_data : cx
209210 . additional_data ( )
210211 . and_then ( |data| data. get :: < ThreadsafeJsValueRef < Unknown > > ( ) )
211212 . cloned ( ) ,
212- source_map : cx. source_map ( ) . cloned ( ) . map ( JsSourceMapWrapper :: new ) ,
213+ source_map : cx. source_map ( ) . map ( Into :: into ) ,
213214 cacheable : cx. cacheable ,
214215 file_dependencies : cx
215216 . file_dependencies
216217 . iter ( )
217- . map ( |i| i. to_string_lossy ( ) . to_string ( ) )
218+ . map ( |i| JsUtf16Buffer :: from ( i. to_string_lossy ( ) . as_ref ( ) ) )
218219 . collect ( ) ,
219220 context_dependencies : cx
220221 . context_dependencies
221222 . iter ( )
222- . map ( |i| i. to_string_lossy ( ) . to_string ( ) )
223+ . map ( |i| JsUtf16Buffer :: from ( i. to_string_lossy ( ) . as_ref ( ) ) )
223224 . collect ( ) ,
224225 missing_dependencies : cx
225226 . missing_dependencies
226227 . iter ( )
227- . map ( |i| i. to_string_lossy ( ) . to_string ( ) )
228+ . map ( |i| JsUtf16Buffer :: from ( i. to_string_lossy ( ) . as_ref ( ) ) )
228229 . collect ( ) ,
229230 build_dependencies : cx
230231 . build_dependencies
231232 . iter ( )
232- . map ( |i| i. to_string_lossy ( ) . to_string ( ) )
233+ . map ( |i| JsUtf16Buffer :: from ( i. to_string_lossy ( ) . as_ref ( ) ) )
233234 . collect ( ) ,
234235
235236 loader_items : cx. loader_items . iter ( ) . map ( Into :: into) . collect ( ) ,
0 commit comments