diff --git a/src/emit.rs b/src/emit.rs index c632b86..304d3bf 100644 --- a/src/emit.rs +++ b/src/emit.rs @@ -25,6 +25,8 @@ pub enum SourceMapOption { pub struct EmitOptions { /// How and if source maps should be generated. pub source_map: SourceMapOption, + /// The `"file"` field of the generated source map. + pub source_map_file: Option, /// Whether to inline the source contents in the source map. Defaults to `true`. pub inline_sources: bool, /// Whether to keep comments in the output. Defaults to `false`. @@ -35,6 +37,7 @@ impl Default for EmitOptions { fn default() -> Self { EmitOptions { source_map: SourceMapOption::default(), + source_map_file: None, inline_sources: true, keep_comments: false, } @@ -103,8 +106,15 @@ pub fn emit( let source_map_config = SourceMapConfig { inline_sources: emit_options.inline_sources, }; + let mut source_map = source_map.build_source_map_with_config( + &src_map_buf, + None, + source_map_config, + ); + if let Some(file) = &emit_options.source_map_file { + source_map.set_file(Some(file.to_string())); + } source_map - .build_source_map_with_config(&src_map_buf, None, source_map_config) .to_writer(&mut buf) .map_err(|e| EmitError::SourceMap(e.into()))?; diff --git a/src/transpiling/mod.rs b/src/transpiling/mod.rs index 62ad06c..8c3d211 100644 --- a/src/transpiling/mod.rs +++ b/src/transpiling/mod.rs @@ -1511,6 +1511,43 @@ const a = _jsx(Foo, { ); } + #[test] + fn test_source_map_with_file() { + let specifier = + ModuleSpecifier::parse("https://deno.land/x/mod.tsx").unwrap(); + let source = r#"{ const foo = "bar"; };"#; + let module = parse_module(ParseParams { + specifier, + text_info: SourceTextInfo::from_string(source.to_string()), + media_type: MediaType::Tsx, + capture_tokens: false, + maybe_syntax: None, + scope_analysis: false, + }) + .unwrap(); + let emit_options = EmitOptions { + source_map: SourceMapOption::Separate, + source_map_file: Some("mod.tsx".to_owned()), + ..Default::default() + }; + let emit_result = module + .transpile(&TranspileOptions::default(), &emit_options) + .unwrap() + .into_source(); + assert_eq!( + &emit_result.text, + r#"{ + const foo = "bar"; +}"# + ); + assert_eq!( + emit_result.source_map.as_deref(), + Some( + r#"{"version":3,"file":"mod.tsx","sources":["https://deno.land/x/mod.tsx"],"sourcesContent":["{ const foo = \"bar\"; };"],"names":[],"mappings":"AAAA;EAAE,MAAM,MAAM;AAAO"}"# + ) + ); + } + #[test] fn test_no_source_map() { let specifier =