From e71b6d31b1179dffb8a6e2d652b394273a6d7852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Sat, 22 Jun 2024 09:35:15 +0900 Subject: [PATCH] feat(cli): Support generating `.d.ts` files (#9097) **Description:** I verified that `swc_cli` (Rust) works with ```json { "jsc": { "parser": { "syntax": "typescript" }, "experimental": { "emitIsolatedDts": true } } } ``` --- crates/swc/src/lib.rs | 7 +++++++ crates/swc_cli_impl/examples/cli.rs | 3 +++ crates/swc_cli_impl/src/commands/compile.rs | 12 +++++++++++- packages/types/index.ts | 5 +++++ packages/types/package.json | 2 +- 5 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 crates/swc_cli_impl/examples/cli.rs diff --git a/crates/swc/src/lib.rs b/crates/swc/src/lib.rs index a529608a7544..6c157b86c192 100644 --- a/crates/swc/src/lib.rs +++ b/crates/swc/src/lib.rs @@ -957,6 +957,13 @@ impl Compiler { ) -> Result { self.run(|| { let program = config.program; + + if config.emit_isolated_dts && !config.syntax.typescript() { + handler.warn( + "jsc.experimental.emitIsolatedDts is enabled but the syntax is not TypeScript", + ); + } + 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 { 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() +} 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 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; 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": {