Skip to content

Commit 4d3eb6a

Browse files
committed
feat!: defaults transform to typescript
Due to the incomplete support of jsdoc in `oxc-transform`, using native TypeScript can achieve optimal speed. Therefore, this change will set `typescript` as the default transformer.
1 parent c05550c commit 4d3eb6a

File tree

7 files changed

+43
-22
lines changed

7 files changed

+43
-22
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,23 @@
7979
},
8080
"peerDependencies": {
8181
"@swc/core": "^1.6.6",
82+
"oxc-transform": "^0.28.0",
8283
"typescript": "^5.5.2"
8384
},
8485
"peerDependenciesMeta": {
8586
"@swc/core": {
8687
"optional": true
8788
},
89+
"oxc-transform": {
90+
"optional": true
91+
},
8892
"typescript": {
8993
"optional": true
9094
}
9195
},
9296
"dependencies": {
9397
"@rollup/pluginutils": "^5.1.0",
9498
"oxc-parser": "^0.27.0",
95-
"oxc-transform": "^0.27.0",
9699
"unplugin": "^1.14.0"
97100
},
98101
"devDependencies": {
@@ -104,6 +107,7 @@
104107
"esbuild": "^0.23.1",
105108
"eslint": "^9.10.0",
106109
"fast-glob": "^3.3.2",
110+
"oxc-transform": "^0.27.0",
107111
"prettier": "^3.3.3",
108112
"rolldown": "nightly",
109113
"rollup": "^4.21.2",

pnpm-lock.yaml

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

src/core/options.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ export type Options = {
1111
} & (
1212
| {
1313
/**
14-
* `@swc/core` should be installed yourself if you want to use `swc` transformer.
14+
* `oxc-transform` or `@swc/core` should be installed yourself
15+
* if you want to use `oxc` or `swc` transformer.
1516
*/
1617
transformer?: 'oxc' | 'swc'
1718
}
1819
| {
1920
/**
2021
* `typescript` should be installed yourself.
21-
* @default oxc
22+
* @default 'typescript'
2223
*/
2324
transformer: 'typescript'
2425
/** Only for typescript transformer */
@@ -38,7 +39,7 @@ export function resolveOptions(options: Options): OptionsResolved {
3839
include: options.include || [/\.[cm]?ts$/],
3940
exclude: options.exclude || [/node_modules/],
4041
enforce: 'enforce' in options ? options.enforce : 'pre',
41-
transformer: options.transformer || 'oxc',
42+
transformer: options.transformer || 'typescript',
4243
ignoreErrors: options.ignoreErrors || false,
4344
extraOutdir: options.extraOutdir,
4445
}

src/core/transformer.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,36 @@ export interface TransformResult {
55
errors: Array<string>
66
}
77

8+
function tryImport<T>(pkg: string): Promise<T | null> {
9+
try {
10+
return import(pkg)
11+
} catch {
12+
return Promise.resolve(null)
13+
}
14+
}
15+
816
export async function oxcTransform(
917
id: string,
1018
code: string,
1119
): Promise<TransformResult> {
12-
const { isolatedDeclaration } = await import('oxc-transform')
13-
return isolatedDeclaration(id, code, { sourcemap: false })
20+
const oxc = await tryImport<typeof import('oxc-transform')>('oxc-transform')
21+
if (!oxc) {
22+
return {
23+
code: '',
24+
errors: [
25+
'oxc-transform is required for transforming TypeScript, please install `oxc-transform`.',
26+
],
27+
}
28+
}
29+
return oxc.isolatedDeclaration(id, code, { sourcemap: false })
1430
}
1531

1632
export async function swcTransform(
1733
id: string,
1834
code: string,
1935
): Promise<TransformResult> {
20-
let swc: typeof import('@swc/core')
21-
try {
22-
swc = await import('@swc/core')
23-
} catch {
36+
const swc = await tryImport<typeof import('@swc/core')>('@swc/core')
37+
if (!swc) {
2438
return {
2539
code: '',
2640
errors: [
@@ -61,11 +75,8 @@ export async function tsTransform(
6175
code: string,
6276
transformOptions?: TranspileOptions,
6377
): Promise<TransformResult> {
64-
let ts: typeof import('typescript')
65-
66-
try {
67-
ts = await import('typescript')
68-
} catch {
78+
const ts = await tryImport<typeof import('typescript')>('typescript')
79+
if (!ts) {
6980
return {
7081
code: '',
7182
errors: [

tests/__snapshots__/esbuild.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export {
1414
};
1515
",
1616
"// temp/main.d.ts
17-
import { type Num } from "./types";
17+
import { type Num } from './types';
1818
export type Str = string;
1919
export declare function hello(s: Str): Str;
2020
export declare let num: Num;
@@ -30,7 +30,7 @@ export type Num2 = number;
3030
3131
exports[`esbuild > write mode 1`] = `
3232
[
33-
"import { type Num } from "./types";
33+
"import { type Num } from './types';
3434
export type Str = string;
3535
export declare function hello(s: Str): Str;
3636
export declare let num: Num;

tests/__snapshots__/rollup.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let num = 1;
1111
export { hello, num };
1212
",
1313
"// temp/main.d.ts
14-
import { type Num } from "./types";
14+
import { type Num } from './types';
1515
export type Str = string;
1616
export declare function hello(s: Str): Str;
1717
export declare let num: Num;

tests/rolldown.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ test('rolldown', async () => {
99

1010
const bundle = await rolldown({
1111
input,
12-
plugins: [UnpluginIsolatedDecl({ extraOutdir: 'temp' })],
12+
plugins: [
13+
UnpluginIsolatedDecl({
14+
extraOutdir: 'temp',
15+
transformer: 'oxc',
16+
}),
17+
],
1318
logLevel: 'silent',
1419
})
1520

0 commit comments

Comments
 (0)