Skip to content

Commit 496dca0

Browse files
committed
chore: add support for tsx
1 parent 557959e commit 496dca0

File tree

10 files changed

+133
-3
lines changed

10 files changed

+133
-3
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
# swc-plugin-allow-importing-ts-extensions
22
SWC Plugin for allowImportingTsExtensions
3+
4+
## Usage
5+
6+
7+
```json
8+
{
9+
"$schema": "http://json.schemastore.org/swcrc",
10+
"jsc": {
11+
"experimental": {
12+
"plugins": [
13+
["swc-plugin-allow-importing-ts-extensions", {}]
14+
]
15+
}
16+
},
17+
}
18+
```

package-lock.json

Lines changed: 42 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"provenance": true
99
},
1010
"workspaces": [
11+
"./tests/fixture/jsx",
1112
"./tests/fixture/simple",
1213
"./tests/fixture/no-preserve-extensions",
1314
"./tests/fixture/preserve-extensions"

src/plugin.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ fn replace_ts_extension(src: &ast::Str, config: &Config) -> Option<ast::Str> {
1515
if let Some(file) = src.value.strip_suffix(".ts") {
1616
return Some(format!("{}.js", file).into());
1717
}
18+
} else if src.value.ends_with(".tsx") && !src.value.ends_with(".d.tsx") {
19+
if let Some(file) = src.value.strip_suffix(".tsx") {
20+
return Some(format!("{}.js", file).into());
21+
}
1822
} else if src.value.ends_with(".mts") && !src.value.ends_with(".d.mts") {
1923
if let Some(file) = src.value.strip_suffix(".mts") {
2024
if config.preserve_import_extension {

tests/fixture.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ use allow_importing_ts_extensions::plugin;
44
use swc_common::{chain, Mark};
55
use swc_ecma_parser::{Syntax, TsConfig};
66
// use swc_ecma_transforms_base::resolver::resolver;
7+
use swc_ecma_transforms_base::resolver;
78
use swc_ecma_transforms_typescript::{typescript, Config};
89
use swc_ecma_transforms_testing::test_fixture;
910
use swc_ecma_visit::as_folder;
1011

12+
#[testing::fixture("tests/fixture/jsx/input.tsx")]
1113
#[testing::fixture("tests/fixture/simple/input.ts")]
1214
#[testing::fixture("tests/fixture/no-preserve-extensions/input.ts")]
1315
fn allow_importing_ts_extensions_default_fixture(input: PathBuf) {
1416
// let mark = Mark::fresh(Mark::root());
1517
let output = input.parent().unwrap().join("output.js");
1618
test_fixture(
17-
Syntax::Typescript(TsConfig::default()),
19+
Syntax::Typescript(TsConfig {
20+
tsx: true,
21+
..TsConfig::default()
22+
}),
1823
&|_tr| chain!(
24+
resolver(Mark::fresh(Mark::root()), Mark::fresh(Mark::root()), true),
1925
typescript(Config::default(), Mark::fresh(Mark::root())),
2026
as_folder(plugin::init(plugin::Config::default()))
2127
),

tests/fixture/jsx/.swcrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"$schema": "http://json.schemastore.org/swcrc",
3+
"jsc": {
4+
"parser": {
5+
"syntax": "typescript",
6+
"tsx": true,
7+
"decorators": true,
8+
"dynamicImport": true
9+
},
10+
"target": "es2020",
11+
"loose": false,
12+
"externalHelpers": false,
13+
// Requires v1.2.50 or upper and requires target to be es2016 or upper.
14+
"keepClassNames": false,
15+
"experimental": {
16+
"plugins": [
17+
["swc-plugin-allow-importing-ts-extensions", {}]
18+
]
19+
}
20+
},
21+
"isModule": true
22+
}

tests/fixture/jsx/input.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from "react"
2+
import App from "./lib/App.tsx"
3+
export default React.memo(function () {
4+
return <App />
5+
})

tests/fixture/jsx/lib/App.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
3+
export default React.memo(function App() {
4+
return <div />
5+
})

tests/fixture/jsx/output.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from "react";
2+
import App from "./lib/App.js";
3+
export default React.memo(function () {
4+
return <App />;
5+
});

tests/fixture/jsx/package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@tests/jsx",
3+
"version": "0.0.0-development",
4+
"description": "",
5+
"main": "input.js",
6+
"private": true,
7+
"directories": {
8+
"lib": "lib"
9+
},
10+
"scripts": {
11+
"test": "echo \"Error: no test specified\" && exit 1",
12+
"build": "rm -rf .swc && swc input.ts -o input.js",
13+
"format": "prettier --write output.js"
14+
},
15+
"author": "",
16+
"license": "MIT",
17+
"devDependencies": {
18+
"@swc/cli": "^0.3.12",
19+
"@swc/core": "^1.4.14",
20+
"prettier": "^3.2.5"
21+
},
22+
"dependencies": {
23+
"react": "^18.2.0",
24+
"swc-plugin-allow-importing-ts-extensions": "file:../../.."
25+
}
26+
}

0 commit comments

Comments
 (0)