From c1b24198a394e8cb8047495ad502edc321203cad Mon Sep 17 00:00:00 2001 From: CPunisher <1343316114@qq.com> Date: Tue, 24 Sep 2024 11:57:05 +0800 Subject: [PATCH 1/2] output dts --- crates/swc_cli_impl/src/commands/compile.rs | 26 ++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/swc_cli_impl/src/commands/compile.rs b/crates/swc_cli_impl/src/commands/compile.rs index cef751ebff5a..438dc940b0e9 100644 --- a/crates/swc_cli_impl/src/commands/compile.rs +++ b/crates/swc_cli_impl/src/commands/compile.rs @@ -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 = + 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=")?; From 2c31ee3bad3cb3724e8410a4bf4f76a1e7e1f9e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 1 Oct 2024 11:43:16 +0900 Subject: [PATCH 2/2] Create little-kangaroos-whisper.md --- .changeset/little-kangaroos-whisper.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/little-kangaroos-whisper.md diff --git a/.changeset/little-kangaroos-whisper.md b/.changeset/little-kangaroos-whisper.md new file mode 100644 index 000000000000..84c789d82319 --- /dev/null +++ b/.changeset/little-kangaroos-whisper.md @@ -0,0 +1,6 @@ +--- +swc_core: patch +swc_cli_impl: patch +--- + +fix(es/codegen): Emit .d.ts when using --out-file