Skip to content
New issue

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

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

Already on GitHub? # to your account

fix(es/codegen): Emit .d.ts when using --out-file #9582

Merged
merged 2 commits into from
Oct 1, 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
6 changes: 6 additions & 0 deletions .changeset/little-kangaroos-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_cli_impl: patch
---

fix(es/codegen): Emit .d.ts when using --out-file
26 changes: 23 additions & 3 deletions crates/swc_cli_impl/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,11 @@ impl CompileOptions {
)?;
let mut buf = File::create(single_out_file)?;
let mut buf_srcmap = None;
let mut buf_dts = None;
let mut source_map_path = None;

// write all transformed files to single output buf
result?.iter().try_for_each(|r| {
for r in result?.iter() {
if let Some(src_map) = r.map.as_ref() {
if buf_srcmap.is_none() {
let map_out_file = if let Some(source_map_target) = &self.source_map_target
Expand Down Expand Up @@ -494,8 +495,27 @@ impl CompileOptions {
.and(Ok(()))?;
}

buf.write(r.code.as_bytes()).and(Ok(()))
})?;
if let Some(extra) = &r.output {
let mut extra: serde_json::Map<String, serde_json::Value> =
serde_json::from_str(extra).context("failed to parse extra output")?;

if let Some(dts_code) = extra.remove("__swc_isolated_declarations__") {
if buf_dts.is_none() {
let dts_file_path = single_out_file.with_extension("d.ts");
buf_dts = Some(File::create(dts_file_path)?);
}

let dts_code = dts_code.as_str().expect("dts code should be string");
buf_dts
.as_ref()
.expect("dts buffer should be available")
.write(dts_code.as_bytes())
.and(Ok(()))?;
}
}

buf.write(r.code.as_bytes()).and(Ok(()))?;
}

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