From 182346f2bb58f23686f3629611b2a15cf99e8b4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 22 Jun 2024 09:05:28 +0900 Subject: [PATCH 1/6] Generate dts --- crates/swc_cli_impl/src/commands/compile.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/swc_cli_impl/src/commands/compile.rs b/crates/swc_cli_impl/src/commands/compile.rs index 3808e4ed4867..ffe74939e3b0 100644 --- a/crates/swc_cli_impl/src/commands/compile.rs +++ b/crates/swc_cli_impl/src/commands/compile.rs @@ -237,7 +237,17 @@ fn emit_output( fs::write(source_map_path, source_map)?; } - fs::write(output_file_path, &output.code)?; + fs::write(&output_file_path, &output.code)?; + + if let Some(extra) = &output.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__") { + let dts_file_path = output_file_path.with_extension("d.ts"); + fs::write(dts_file_path, dts_code.as_str().unwrap())?; + } + } } else { let source_map = if let Some(ref source_map) = output.map { &**source_map From 495380b328bb7a616a76c30ad2b55866119fb563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 22 Jun 2024 09:06:20 +0900 Subject: [PATCH 2/6] Binary --- crates/swc_cli_impl/examples/cli.rs | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 crates/swc_cli_impl/examples/cli.rs diff --git a/crates/swc_cli_impl/examples/cli.rs b/crates/swc_cli_impl/examples/cli.rs new file mode 100644 index 000000000000..7f23c223f727 --- /dev/null +++ b/crates/swc_cli_impl/examples/cli.rs @@ -0,0 +1,3 @@ +fn main() -> anyhow::Result<()> { + swc_cli_impl::run() +} From 2f53e6a90cc663b807f891b2a9037bc17afac22a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 22 Jun 2024 09:14:18 +0900 Subject: [PATCH 3/6] Config lint --- crates/swc/src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index a529608a7544..d23920d684eb 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -957,6 +957,17 @@ impl Compiler { ) -> Result { self.run(|| { let program = config.program; + + if config.emit_isolated_dts && !config.syntax.typescript() { + HANDLER.with(|h| { + h.struct_warn( + "jsc.experimental.emitIsolatedDts is enabled but the syntax is not \ + TypeScript", + ) + .emit(); + }); + } + let emit_dts = config.syntax.typescript() && config.emit_isolated_dts; let source_map_names = if config.source_maps.enabled() { let mut v = swc_compiler_base::IdentCollector { From 3e3aadc308816d1a211f69543a8b58aa433afdde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 22 Jun 2024 09:16:21 +0900 Subject: [PATCH 4/6] Config lint --- crates/swc/src/lib.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index d23920d684eb..6c157b86c192 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -959,13 +959,9 @@ impl Compiler { let program = config.program; if config.emit_isolated_dts && !config.syntax.typescript() { - HANDLER.with(|h| { - h.struct_warn( - "jsc.experimental.emitIsolatedDts is enabled but the syntax is not \ - TypeScript", - ) - .emit(); - }); + handler.warn( + "jsc.experimental.emitIsolatedDts is enabled but the syntax is not TypeScript", + ); } let emit_dts = config.syntax.typescript() && config.emit_isolated_dts; From e55c45025c8eb8428b990e9cc31f4d88b70f7d80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 22 Jun 2024 09:18:24 +0900 Subject: [PATCH 5/6] type --- packages/types/index.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/types/index.ts b/packages/types/index.ts index 99be5f70afbc..08ee67a7a554 100644 --- a/packages/types/index.ts +++ b/packages/types/index.ts @@ -634,6 +634,11 @@ export interface JscConfig { * Disable builtin transforms. If enabled, only Wasm plugins are used. */ disableBuiltinTransformsForInternalTesting?: boolean; + + /** + * Emit isolated dts files for each module. + */ + emitIsolatedDts?: boolean; }; baseUrl?: string; From 2f099b0e4ad975c15f564490108c49d7629c8e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4=20=28Donny=29?= Date: Sat, 22 Jun 2024 09:18:40 +0900 Subject: [PATCH 6/6] version --- packages/types/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/types/package.json b/packages/types/package.json index 088691c86ca3..a9b9d39d5988 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -1,7 +1,7 @@ { "name": "@swc/types", "packageManager": "yarn@4.0.2", - "version": "0.1.8", + "version": "0.1.9", "description": "Typings for the swc project.", "sideEffects": false, "scripts": {