Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add source_map_file option for emit #244

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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