1
1
use crate :: collector:: { new_ident_from_id, GlobalCollect , Id , ImportKind } ;
2
- use crate :: parse:: {
3
- emit_source_code, might_need_handle_watch, HookAnalysis , PathData , TransformModule ,
4
- TransformOutput ,
5
- } ;
2
+ use crate :: parse:: PathData ;
6
3
use crate :: transform:: { add_handle_watch, create_synthetic_named_import} ;
7
4
use crate :: words:: * ;
8
5
9
- use std:: collections:: BTreeMap ;
10
- use std:: path:: Path ;
11
-
12
- use anyhow:: { Context , Error } ;
13
- use path_slash:: PathExt ;
6
+ use anyhow:: Error ;
14
7
use swc_atoms:: JsWord ;
15
8
use swc_common:: comments:: { SingleThreadedComments , SingleThreadedCommentsMap } ;
16
- use swc_common:: { sync :: Lrc , SourceMap , DUMMY_SP } ;
9
+ use swc_common:: DUMMY_SP ;
17
10
use swc_ecmascript:: ast;
18
11
use swc_ecmascript:: utils:: private_ident;
19
12
@@ -152,32 +145,6 @@ pub fn new_module(ctx: NewModuleCtx) -> Result<(ast::Module, SingleThreadedComme
152
145
Ok ( ( module, comments) )
153
146
}
154
147
155
- pub fn fix_path < S : AsRef < Path > , D : AsRef < Path > > (
156
- src : S ,
157
- dest : D ,
158
- ident : & str ,
159
- ) -> Result < JsWord , Error > {
160
- let src = src. as_ref ( ) ;
161
- let dest = dest. as_ref ( ) ;
162
- if ident. starts_with ( '.' ) {
163
- let diff = pathdiff:: diff_paths ( src, dest) ;
164
-
165
- if let Some ( diff) = diff {
166
- let normalize = diff. to_slash_lossy ( ) ;
167
- let relative = relative_path:: RelativePath :: new ( & normalize) ;
168
- let final_path = relative. join ( ident) . normalize ( ) ;
169
- let final_str = final_path. as_str ( ) ;
170
- return Ok ( if final_str. starts_with ( '.' ) {
171
- JsWord :: from ( final_str)
172
- } else {
173
- JsWord :: from ( format ! ( "./{}" , final_str) )
174
- } ) ;
175
- }
176
- }
177
-
178
- Ok ( JsWord :: from ( ident) )
179
- }
180
-
181
148
fn create_named_export ( expr : Box < ast:: Expr > , name : & str ) -> ast:: ModuleItem {
182
149
ast:: ModuleItem :: ModuleDecl ( ast:: ModuleDecl :: ExportDecl ( ast:: ExportDecl {
183
150
span : DUMMY_SP ,
@@ -198,125 +165,6 @@ fn create_named_export(expr: Box<ast::Expr>, name: &str) -> ast::ModuleItem {
198
165
} ) )
199
166
}
200
167
201
- #[ test]
202
- fn test_fix_path ( ) {
203
- assert_eq ! (
204
- fix_path( "src" , "" , "./state.qwik.mjs" ) . unwrap( ) ,
205
- JsWord :: from( "./src/state.qwik.mjs" )
206
- ) ;
207
-
208
- assert_eq ! (
209
- fix_path( "src/path" , "" , "./state" ) . unwrap( ) ,
210
- JsWord :: from( "./src/path/state" )
211
- ) ;
212
-
213
- assert_eq ! (
214
- fix_path( "src" , "" , "../state" ) . unwrap( ) ,
215
- JsWord :: from( "./state" )
216
- ) ;
217
- assert_eq ! (
218
- fix_path( "a" , "a" , "./state" ) . unwrap( ) ,
219
- JsWord :: from( "./state" )
220
- ) ;
221
- }
222
-
223
- pub fn generate_entries (
224
- mut output : TransformOutput ,
225
- core_module : & JsWord ,
226
- explicit_extensions : bool ,
227
- root_dir : Option < & Path > ,
228
- ) -> Result < TransformOutput , anyhow:: Error > {
229
- let source_map = Lrc :: new ( SourceMap :: default ( ) ) ;
230
- let mut entries_map: BTreeMap < & str , Vec < & HookAnalysis > > = BTreeMap :: new ( ) ;
231
- let mut new_modules = Vec :: with_capacity ( output. modules . len ( ) ) ;
232
- {
233
- let hooks: Vec < & HookAnalysis > = output. modules . iter ( ) . flat_map ( |m| & m. hook ) . collect ( ) ;
234
- for hook in hooks {
235
- if let Some ( ref e) = hook. entry {
236
- entries_map
237
- . entry ( e. as_ref ( ) )
238
- . or_insert_with ( Vec :: new)
239
- . push ( hook) ;
240
- }
241
- }
242
-
243
- for ( entry, hooks) in & entries_map {
244
- let module = new_entry_module ( entry, hooks, core_module, explicit_extensions) ;
245
- let ( code, map) =
246
- emit_source_code ( Lrc :: clone ( & source_map) , None , & module, root_dir, false )
247
- . context ( "Emitting source code" ) ?;
248
- new_modules. push ( TransformModule {
249
- path : [ entry, ".js" ] . concat ( ) ,
250
- code,
251
- map,
252
- is_entry : true ,
253
- hook : None ,
254
- order : 0 ,
255
- } ) ;
256
- }
257
- }
258
- output. modules . append ( & mut new_modules) ;
259
-
260
- Ok ( output)
261
- }
262
-
263
- fn new_entry_module (
264
- path : & str ,
265
- hooks : & [ & HookAnalysis ] ,
266
- core_module : & JsWord ,
267
- explicit_extensions : bool ,
268
- ) -> ast:: Module {
269
- let mut module = ast:: Module {
270
- span : DUMMY_SP ,
271
- body : Vec :: with_capacity ( hooks. len ( ) ) ,
272
- shebang : None ,
273
- } ;
274
- let mut need_handle_watch = false ;
275
- for hook in hooks {
276
- // TODO fix the path from the entry to the hook in case of mismatched location
277
- let mut src = fix_path (
278
- & hook. path . to_string ( ) ,
279
- Path :: new ( path) . parent ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ,
280
- & [ "./" , & hook. canonical_filename ] . concat ( ) ,
281
- )
282
- . unwrap ( )
283
- . to_string ( ) ;
284
- if explicit_extensions {
285
- src = src + "." + hook. extension . as_ref ( ) ;
286
- }
287
- if might_need_handle_watch ( & hook. ctx_kind , & hook. ctx_name ) {
288
- need_handle_watch = true ;
289
- }
290
- module
291
- . body
292
- . push ( ast:: ModuleItem :: ModuleDecl ( ast:: ModuleDecl :: ExportNamed (
293
- ast:: NamedExport {
294
- span : DUMMY_SP ,
295
- type_only : false ,
296
- asserts : None ,
297
- src : Some ( Box :: new ( ast:: Str {
298
- span : DUMMY_SP ,
299
- value : JsWord :: from ( src) ,
300
- raw : None ,
301
- } ) ) ,
302
- specifiers : vec ! [ ast:: ExportSpecifier :: Named ( ast:: ExportNamedSpecifier {
303
- is_type_only: false ,
304
- span: DUMMY_SP ,
305
- orig: ast:: ModuleExportName :: Ident ( ast:: Ident :: new(
306
- hook. name. clone( ) ,
307
- DUMMY_SP ,
308
- ) ) ,
309
- exported: None ,
310
- } ) ] ,
311
- } ,
312
- ) ) ) ;
313
- }
314
- if need_handle_watch {
315
- add_handle_watch ( & mut module. body , core_module) ;
316
- }
317
- module
318
- }
319
-
320
168
pub fn transform_function_expr (
321
169
expr : ast:: Expr ,
322
170
use_lexical_scope : & Id ,
0 commit comments