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(bindings/swc_cli): Append the sourceMappingURL= source map file … #6873

Merged
merged 1 commit into from
Jan 29, 2023
Merged
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
62 changes: 42 additions & 20 deletions bindings/swc_cli/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ fn resolve_output_file_path(
}

fn emit_output(
output: &TransformOutput,
mut output: TransformOutput,
out_dir: &Option<PathBuf>,
file_path: &Path,
file_extension: PathBuf,
Expand All @@ -219,12 +219,18 @@ fn emit_output(
fs::create_dir_all(output_dir)?;
}

fs::write(&output_file_path, &output.code)?;

if let Some(source_map) = &output.map {
let source_map_path = output_file_path.with_extension("js.map");

output.code.push_str("\n//# sourceMappingURL=");
output
.code
.push_str(&source_map_path.file_name().unwrap().to_string_lossy());

fs::write(source_map_path, source_map)?;
}

fs::write(output_file_path, &output.code)?;
} else {
println!(
"{}\n{}\n{}",
Expand Down Expand Up @@ -400,27 +406,38 @@ impl CompileOptions {
)?;
let mut buf = File::create(single_out_file)?;
let mut buf_srcmap = None;
let mut source_map_path = None;

// write all transformed files to single output buf
result?.iter().try_for_each(|r| {
if let Some(src_map) = r.map.as_ref() {
if buf_srcmap.is_none() {
// we'll init buf lazily as we don't read ./.swcrc directly to determine if
// sourcemap would be generated or not
let srcmap_buf_name =
if let Some(source_map_target) = &self.source_map_target {
File::create(source_map_target)?
} else {
File::create(single_out_file.with_extension(format!(
"{}map",
if let Some(ext) = single_out_file.extension() {
format!("{}.", ext.to_string_lossy())
} else {
"".to_string()
}
)))?
};
buf_srcmap = Some(srcmap_buf_name);
let map_out_file = if let Some(source_map_target) = &self.source_map_target
{
source_map_path = Some(source_map_target.clone());
source_map_target.into()
} else {
let map_out_file = single_out_file.with_extension(format!(
"{}map",
if let Some(ext) = single_out_file.extension() {
format!("{}.", ext.to_string_lossy())
} else {
"".to_string()
}
));

// Get the filename of the source map, as the source map will
// be created in the same directory next to the output.
source_map_path = Some(
map_out_file
.file_name()
.unwrap()
.to_string_lossy()
.to_string(),
);
map_out_file
};
buf_srcmap = Some(File::create(map_out_file)?);
}

buf_srcmap
Expand All @@ -433,6 +450,11 @@ impl CompileOptions {
buf.write(r.code.as_bytes()).and(Ok(()))
})?;

if let Some(source_map_path) = source_map_path {
buf.write_all(b"\n//# sourceMappingURL=")?;
buf.write_all(source_map_path.as_bytes())?;
}

buf.flush()
.context("Failed to write output into single file")
} else {
Expand All @@ -448,7 +470,7 @@ impl CompileOptions {

match result {
Ok(output) => {
emit_output(&output, &self.out_dir, &file_path, file_extension)
emit_output(output, &self.out_dir, &file_path, file_extension)
}
Err(e) => Err(e),
}
Expand Down