Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(cli): Support generating .d.ts files #9097

Merged
merged 6 commits into from
Jun 22, 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
7 changes: 7 additions & 0 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,13 @@ impl Compiler {
) -> Result<TransformOutput, Error> {
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 {
Expand Down
3 changes: 3 additions & 0 deletions crates/swc_cli_impl/examples/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() -> anyhow::Result<()> {
swc_cli_impl::run()
}
12 changes: 11 additions & 1 deletion crates/swc_cli_impl/src/commands/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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__") {
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
Expand Down
5 changes: 5 additions & 0 deletions packages/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/types/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
Loading