diff --git a/README.md b/README.md index 1b931f0f..4a1401a8 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ export type User = { user_id: number, first_name: string, last_name: string, }; | format | Enables formatting of the generated TypeScript bindings.
Currently, this unfortunately adds quite a few dependencies. | | no-serde-warnings | By default, warnings are printed during build if unsupported serde attributes are encountered.
Enabling this feature silences these warnings. | | import-esm | When enabled,`import` statements in the generated file will have the `.js` extension in the end of the path to conform to the ES Modules spec.
Example: `import { MyStruct } from "./my_struct.js"` | +| import-deno | When enabled,`import` statements in the generated file will have the `.ts` extension in the end of the path to conform to the Deno spec.
Example: `import { MyStruct } from "./my_struct.ts"` | | serde-json-impl | Implement `TS` for types from *serde_json* | | chrono-impl | Implement `TS` for types from *chrono* | | bigdecimal-impl | Implement `TS` for types from *bigdecimal* | @@ -92,8 +93,8 @@ export type User = { user_id: number, first_name: string, last_name: string, }; | ordered-float-impl | Implement `TS` for types from *ordered_float* | | heapless-impl | Implement `TS` for types from *heapless* | | semver-impl | Implement `TS` for types from *semver* | -| smol_str-impl | Implement `TS` for types from *smol_str* | -| tokio-impl | Implement `TS` for types from *tokio* | +| smol_str-impl | Implement `TS` for types from *smol_str* | +| tokio-impl | Implement `TS` for types from *tokio* |
diff --git a/ts-rs/Cargo.toml b/ts-rs/Cargo.toml index 85d00d48..0c82bf6e 100644 --- a/ts-rs/Cargo.toml +++ b/ts-rs/Cargo.toml @@ -35,6 +35,7 @@ smol_str-impl = ["smol_str"] serde-json-impl = ["serde_json"] no-serde-warnings = ["ts-rs-macros/no-serde-warnings"] import-esm = [] +import-deno = [] tokio-impl = ["tokio"] [dev-dependencies] diff --git a/ts-rs/src/export.rs b/ts-rs/src/export.rs index 3640c96e..87818330 100644 --- a/ts-rs/src/export.rs +++ b/ts-rs/src/export.rs @@ -347,6 +347,10 @@ fn import_path(from: &Path, import: &Path) -> Result { str_path }; + if cfg!(feature = "import-deno") { + return Ok(path); + } + let path_without_extension = path.trim_end_matches(".ts"); Ok(if cfg!(feature = "import-esm") { diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index b2bbc073..cfa429a6 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -79,6 +79,7 @@ //! | format | Enables formatting of the generated TypeScript bindings.
Currently, this unfortunately adds quite a few dependencies. | //! | no-serde-warnings | By default, warnings are printed during build if unsupported serde attributes are encountered.
Enabling this feature silences these warnings. | //! | import-esm | When enabled,`import` statements in the generated file will have the `.js` extension in the end of the path to conform to the ES Modules spec.
Example: `import { MyStruct } from "./my_struct.js"` | +//! | import-deno | When enabled,`import` statements in the generated file will have the `.ts` extension in the end of the path to conform to the Deno spec.
Example: `import { MyStruct } from "./my_struct.ts"` | //! | serde-json-impl | Implement `TS` for types from *serde_json* | //! | chrono-impl | Implement `TS` for types from *chrono* | //! | bigdecimal-impl | Implement `TS` for types from *bigdecimal* | @@ -90,8 +91,8 @@ //! | ordered-float-impl | Implement `TS` for types from *ordered_float* | //! | heapless-impl | Implement `TS` for types from *heapless* | //! | semver-impl | Implement `TS` for types from *semver* | -//! | smol_str-impl | Implement `TS` for types from *smol_str* | -//! | tokio-impl | Implement `TS` for types from *tokio* | +//! | smol_str-impl | Implement `TS` for types from *smol_str* | +//! | tokio-impl | Implement `TS` for types from *tokio* | //! //!
//! diff --git a/ts-rs/tests/integration/imports.rs b/ts-rs/tests/integration/imports.rs index bd7e5f81..87baae5d 100644 --- a/ts-rs/tests/integration/imports.rs +++ b/ts-rs/tests/integration/imports.rs @@ -28,42 +28,44 @@ fn test_def() { TestEnum::export_all().unwrap(); let text = std::fs::read_to_string(TestEnum::default_output_path().unwrap()).unwrap(); - let expected = match (cfg!(feature = "format"), cfg!(feature = "import-esm")) { - (true, true) => concat!( - "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n", - "import type { TestTypeA } from \"./ts_rs_test_type_a.js\";\n", - "import type { TestTypeB } from \"./ts_rs_test_type_b.js\";\n", - "\n", - "export type TestEnum = { \"C\": { value: TestTypeB } } | {\n", - " \"A1\": { value: TestTypeA };\n", - "} | { \"A2\": { value: TestTypeA } };\n", - ), - (true, false) => concat!( + let extension = if cfg!(feature = "import-deno") { + ".ts" + } else if cfg!(feature = "import-esm") { + ".js" + } else { + "" + }; + + let import_a = format!( + "import type {{ TestTypeA }} from \"./ts_rs_test_type_a{}\";\n", + extension + ); + + let import_b = format!( + "import type {{ TestTypeB }} from \"./ts_rs_test_type_b{}\";\n", + extension + ); + + let expected = if cfg!(feature = "format") { + vec!( "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n", - "import type { TestTypeA } from \"./ts_rs_test_type_a\";\n", - "import type { TestTypeB } from \"./ts_rs_test_type_b\";\n", + &import_a, + &import_b, "\n", "export type TestEnum = { \"C\": { value: TestTypeB } } | {\n", " \"A1\": { value: TestTypeA };\n", "} | { \"A2\": { value: TestTypeA } };\n", - ), - (false, true) => concat!( + ) + } else { + vec!( "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n", - "import type { TestTypeA } from \"./ts_rs_test_type_a.js\";\n", - "import type { TestTypeB } from \"./ts_rs_test_type_b.js\";\n", + &import_a, + &import_b, "\n", "export type TestEnum = { \"C\": { value: TestTypeB, } } | { \"A1\": { value: TestTypeA, } } | { \"A2\": { value: TestTypeA, } };", "\n", - ), - (false, false) => concat!( - "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n", - "import type { TestTypeA } from \"./ts_rs_test_type_a\";\n", - "import type { TestTypeB } from \"./ts_rs_test_type_b\";\n", - "\n", - "export type TestEnum = { \"C\": { value: TestTypeB, } } | { \"A1\": { value: TestTypeA, } } | { \"A2\": { value: TestTypeA, } };", - "\n", - ), - }; + ) + }.join("").to_string(); assert_eq!(text, expected); } diff --git a/ts-rs/tests/integration/issue_168.rs b/ts-rs/tests/integration/issue_168.rs index 732ba9be..190671e0 100644 --- a/ts-rs/tests/integration/issue_168.rs +++ b/ts-rs/tests/integration/issue_168.rs @@ -32,7 +32,7 @@ struct Baz { } #[test] -#[cfg(not(feature = "import-esm"))] +#[cfg(not(any(feature = "import-esm", feature = "import-deno")))] fn issue_168() { assert_eq!( FooInlined::export_to_string().unwrap(), diff --git a/ts-rs/tests/integration/issue_232.rs b/ts-rs/tests/integration/issue_232.rs index 5cb6f684..582d105e 100644 --- a/ts-rs/tests/integration/issue_232.rs +++ b/ts-rs/tests/integration/issue_232.rs @@ -42,7 +42,7 @@ enum Enum { } #[test] -#[cfg(not(feature = "import-esm"))] +#[cfg(not(any(feature = "import-esm", feature = "import-deno")))] fn issue_232() { println!("{}", StateInlinedVec::export_to_string().unwrap()); assert_eq!(