1
1
use std:: {
2
- borrow:: Cow ,
3
2
fs:: { self , File } ,
4
3
io:: { self , Read , Write } ,
5
4
path:: { Component , Path , PathBuf } ,
@@ -213,22 +212,10 @@ fn resolve_output_file_path(
213
212
214
213
fn emit_output (
215
214
mut output : TransformOutput ,
216
- source_file_name : & Option < String > ,
217
- source_root : & Option < String > ,
218
215
out_dir : & Option < PathBuf > ,
219
216
file_path : & Path ,
220
217
file_extension : PathBuf ,
221
218
) -> anyhow:: Result < ( ) > {
222
- let source_map = if let Some ( ref source_map) = & output. map {
223
- Some ( extend_source_map (
224
- source_map. to_owned ( ) ,
225
- source_file_name,
226
- source_root,
227
- ) ?)
228
- } else {
229
- None
230
- } ;
231
-
232
219
if let Some ( out_dir) = out_dir {
233
220
let output_file_path = resolve_output_file_path ( out_dir, file_path, file_extension) ?;
234
221
let output_dir = output_file_path
@@ -239,7 +226,7 @@ fn emit_output(
239
226
fs:: create_dir_all ( output_dir) ?;
240
227
}
241
228
242
- if let Some ( ref source_map) = source_map {
229
+ if let Some ( ref source_map) = output . map {
243
230
let source_map_path = output_file_path. with_extension ( "js.map" ) ;
244
231
245
232
output. code . push_str ( "\n //# sourceMappingURL=" ) ;
@@ -252,10 +239,10 @@ fn emit_output(
252
239
253
240
fs:: write ( output_file_path, & output. code ) ?;
254
241
} else {
255
- let source_map = if let Some ( ref source_map) = source_map {
256
- String :: from_utf8_lossy ( source_map)
242
+ let source_map = if let Some ( ref source_map) = output . map {
243
+ & * * source_map
257
244
} else {
258
- Cow :: Borrowed ( "" )
245
+ ""
259
246
} ;
260
247
261
248
println ! ( "{}\n {}\n {}" , file_path. display( ) , output. code, source_map, ) ;
@@ -484,17 +471,10 @@ impl CompileOptions {
484
471
buf_srcmap = Some ( File :: create ( map_out_file) ?) ;
485
472
}
486
473
487
- let source_map = extend_source_map (
488
- src_map. to_owned ( ) ,
489
- & self . source_file_name ,
490
- & self . source_root ,
491
- )
492
- . unwrap ( ) ;
493
-
494
474
buf_srcmap
495
475
. as_ref ( )
496
476
. expect ( "Srcmap buffer should be available" )
497
- . write ( & source_map )
477
+ . write ( src_map . as_bytes ( ) )
498
478
. and ( Ok ( ( ) ) ) ?;
499
479
}
500
480
@@ -520,14 +500,9 @@ impl CompileOptions {
520
500
let result = execute ( compiler, fm, options) ;
521
501
522
502
match result {
523
- Ok ( output) => emit_output (
524
- output,
525
- & self . source_file_name ,
526
- & self . source_root ,
527
- & self . out_dir ,
528
- & file_path,
529
- file_extension,
530
- ) ,
503
+ Ok ( output) => {
504
+ emit_output ( output, & self . out_dir , & file_path, file_extension)
505
+ }
531
506
Err ( e) => Err ( e) ,
532
507
}
533
508
} ,
@@ -536,33 +511,6 @@ impl CompileOptions {
536
511
}
537
512
}
538
513
539
- // TODO: remove once fixed in core https://github.com/swc-project/swc/issues/1388
540
- fn extend_source_map (
541
- source_map : String ,
542
- source_file_name : & Option < String > ,
543
- source_root : & Option < String > ,
544
- ) -> anyhow:: Result < Vec < u8 > > {
545
- let mut source_map = sourcemap:: SourceMap :: from_reader ( source_map. as_bytes ( ) )
546
- . context ( "failed to encode source map" ) ?;
547
-
548
- if !source_map. get_token_count ( ) != 0 {
549
- if let Some ( ref source_file_name) = source_file_name {
550
- source_map. set_source ( 0u32 , source_file_name) ;
551
- }
552
- }
553
-
554
- if source_root. is_some ( ) {
555
- source_map. set_source_root ( source_root. clone ( ) ) ;
556
- }
557
-
558
- let mut buf = vec ! [ ] ;
559
- source_map
560
- . to_writer ( & mut buf)
561
- . context ( "failed to decode source map" ) ?;
562
-
563
- Ok ( buf)
564
- }
565
-
566
514
#[ swc_trace]
567
515
impl super :: CommandRunner for CompileOptions {
568
516
fn execute ( & self ) -> anyhow:: Result < ( ) > {
0 commit comments