Skip to content

Commit

Permalink
feat: add source_map_file option for emit (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed Apr 29, 2024
1 parent f43031b commit 4eaf585
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// 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`.
Expand All @@ -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,
}
Expand Down Expand Up @@ -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()))?;

Expand Down
37 changes: 37 additions & 0 deletions src/transpiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 4eaf585

Please sign in to comment.