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

**Related issue:**

Closes https://github.com/swc-project/swc/issues/9512
This commit is contained in:
CPunisher 2024-10-01 10:43:50 +08:00 committed by GitHub
parent 77900d808e
commit 3d9d641f8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 3 deletions

View File

@ -0,0 +1,6 @@
---
swc_core: patch
swc_cli_impl: patch
---
fix(es/codegen): Emit .d.ts when using --out-file

View File

@ -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
@ -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=")?;