From 9dbdf8dad8882b11d96dfdc4b83a1e1006325d46 Mon Sep 17 00:00:00 2001 From: frami Date: Wed, 17 Apr 2024 20:00:39 -0300 Subject: [PATCH 1/2] fix: preserve extension if the import is a package --- src/plugin.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/plugin.rs b/src/plugin.rs index c7c75e6..44c8a64 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -11,7 +11,9 @@ pub struct Config { } fn replace_ts_extension(src: &ast::Str, config: &Config) -> Option { - if src.value.ends_with(".ts") && !src.value.ends_with(".d.ts") { + if !src.value.starts_with('.') { + return None; + } else if src.value.ends_with(".ts") && !src.value.ends_with(".d.ts") { if let Some(file) = src.value.strip_suffix(".ts") { return Some(format!("{}.js", file).into()); } From f5b047070efffcfb313526af08c25021f50809d0 Mon Sep 17 00:00:00 2001 From: frami Date: Wed, 17 Apr 2024 20:42:53 -0300 Subject: [PATCH 2/2] docs: update README.md --- README.md | 59 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 68461fd..57db620 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,59 @@ # swc-plugin-allow-importing-ts-extensions + SWC Plugin for allowImportingTsExtensions +## When and Why + +If you are using SWC to transpile your TypeScript code, and your target is `es2016` or higher, you can use this plugin to import `.ts` files extensions (instead of `.js`), and it will be transpiled to `.js` files. + ## Usage +First, setup your `tsconfig.json` to allow importing `.ts` files extensions. -```json - { - "$schema": "http://json.schemastore.org/swcrc", - "jsc": { - "experimental": { - "plugins": [ - ["swc-plugin-allow-importing-ts-extensions", {}] - ] - } +```json5 +// tsconfig.json +{ + compilerOptions: { + // ... + noEmit: true, + allowImportingTsExtensions: true, }, + // ... } ``` + +Then, add the plugin to your `.swcrc` file. + +```json5 +// .swcrc +{ + $schema: "http://json.schemastore.org/swcrc", + jsc: { + experimental: { + plugins: [["swc-plugin-allow-importing-ts-extensions", {}]], + }, + }, +} +``` + +## Options + +```json5 +// .swcrc +{ + // ... + plugins: [ + [ + "swc-plugin-allow-importing-ts-extensions", + { + preserveImportExtension: true, // default: false + }, + ], + ], + // ... +} +``` + +| option | type | default | description | +| ------------------------- | --------- | ------- | -------------------------------------------------------- | +| `preserveImportExtension` | `boolean` | `false` | If `true` all imports with extension `.mts` and `.cts` with be map to `.mjs` and `.cjs` respectively, otherwise it will map to `.js` |