@@ -2,13 +2,109 @@ use std::collections::HashMap;
22
33use napi:: bindgen_prelude:: * ;
44use napi_derive:: napi;
5- use rspack_core:: { LoaderContext , RunnerContext } ;
6- use rspack_error:: error;
5+ use rspack_core:: { rspack_sources:: SourceMap , LoaderContext , RunnerContext } ;
76use rspack_loader_runner:: { LoaderItem , State as LoaderState } ;
8- use rspack_napi:: threadsafe_js_value_ref:: ThreadsafeJsValueRef ;
7+ use rspack_napi:: {
8+ napi:: JsString , string:: JsStringExt , threadsafe_js_value_ref:: ThreadsafeJsValueRef ,
9+ } ;
910
1011use crate :: { JsModuleWrapper , JsResourceData , JsRspackError } ;
1112
13+ #[ napi( object) ]
14+ pub struct JsSourceMap {
15+ 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 > ,
22+ }
23+
24+ pub struct JsSourceMapWrapper ( SourceMap ) ;
25+
26+ impl JsSourceMapWrapper {
27+ pub fn new ( source_map : SourceMap ) -> Self {
28+ Self ( source_map)
29+ }
30+
31+ pub fn take ( self ) -> SourceMap {
32+ self . 0
33+ }
34+ }
35+
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+ } ;
61+
62+ let js_source_map = JsSourceMap {
63+ version : 3 ,
64+ file,
65+ sources,
66+ sources_content : if sources_content. is_empty ( ) {
67+ None
68+ } else {
69+ Some ( sources_content)
70+ } ,
71+ names,
72+ mappings,
73+ source_root,
74+ } ;
75+ ToNapiValue :: to_napi_value ( env, js_source_map)
76+ }
77+ }
78+
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+ } ;
90+
91+ Ok ( JsSourceMapWrapper ( SourceMap :: new (
92+ js_source_map. mappings . into_string ( ) ,
93+ js_source_map
94+ . sources
95+ . into_iter ( )
96+ . map ( |source| source. into_string ( ) )
97+ . collect :: < Vec < _ > > ( ) ,
98+ sources_content,
99+ js_source_map
100+ . names
101+ . into_iter ( )
102+ . map ( |source| source. into_string ( ) )
103+ . collect :: < Vec < _ > > ( ) ,
104+ ) ) )
105+ }
106+ }
107+
12108#[ napi( object) ]
13109pub struct JsLoaderItem {
14110 pub request : String ,
@@ -57,21 +153,19 @@ impl From<LoaderState> for JsLoaderState {
57153pub struct JsLoaderContext {
58154 #[ napi( ts_type = "Readonly<JsResourceData>" ) ]
59155 pub resource_data : JsResourceData ,
60- /// Will be deprecated. Use module.module_identifier instead
61- #[ napi( js_name = "_moduleIdentifier" , ts_type = "Readonly<string>" ) ]
62- pub module_identifier : String ,
63156 #[ napi( js_name = "_module" , ts_type = "JsModule" ) ]
64157 pub module : JsModuleWrapper ,
65158 #[ napi( ts_type = "Readonly<boolean>" ) ]
66159 pub hot : bool ,
67160
68161 /// Content maybe empty in pitching stage
69- pub content : Either < Null , Buffer > ,
162+ pub content : Either3 < Null , Buffer , String > ,
70163 #[ napi( ts_type = "any" ) ]
71164 pub additional_data : Option < ThreadsafeJsValueRef < Unknown > > ,
72165 #[ napi( js_name = "__internal__parseMeta" ) ]
73166 pub parse_meta : HashMap < String , String > ,
74- pub source_map : Option < Buffer > ,
167+ #[ napi( ts_type = "JsSourceMap" ) ]
168+ pub source_map : Option < JsSourceMapWrapper > ,
75169 pub cacheable : bool ,
76170 pub file_dependencies : Vec < String > ,
77171 pub context_dependencies : Vec < String > ,
@@ -96,25 +190,21 @@ impl TryFrom<&mut LoaderContext<RunnerContext>> for JsLoaderContext {
96190
97191 Ok ( JsLoaderContext {
98192 resource_data : cx. resource_data . as_ref ( ) . into ( ) ,
99- module_identifier : module. identifier ( ) . to_string ( ) ,
100193 module : JsModuleWrapper :: new ( module, cx. context . compilation_id , None ) ,
101194 hot : cx. hot ,
102195 content : match cx. content ( ) {
103- Some ( c) => Either :: B ( c. to_owned ( ) . into_bytes ( ) . into ( ) ) ,
104- None => Either :: A ( Null ) ,
196+ Some ( c) => match c {
197+ rspack_core:: Content :: String ( s) => Either3 :: C ( s. to_string ( ) ) ,
198+ rspack_core:: Content :: Buffer ( vec) => Either3 :: B ( vec. clone ( ) . into ( ) ) ,
199+ } ,
200+ None => Either3 :: A ( Null ) ,
105201 } ,
106202 parse_meta : cx. parse_meta . clone ( ) . into_iter ( ) . collect ( ) ,
107203 additional_data : cx
108204 . additional_data ( )
109205 . and_then ( |data| data. get :: < ThreadsafeJsValueRef < Unknown > > ( ) )
110206 . cloned ( ) ,
111- source_map : cx
112- . source_map ( )
113- . cloned ( )
114- . map ( |v| v. to_json ( ) )
115- . transpose ( )
116- . map_err ( |e| error ! ( e. to_string( ) ) ) ?
117- . map ( |v| v. into_bytes ( ) . into ( ) ) ,
207+ source_map : cx. source_map ( ) . cloned ( ) . map ( JsSourceMapWrapper :: new) ,
118208 cacheable : cx. cacheable ,
119209 file_dependencies : cx
120210 . file_dependencies
0 commit comments