Skip to content

Commit e826573

Browse files
committed
feat: allow convert with tsconfig value directly
1 parent 0509e8c commit e826573

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ npm i tsconfig-to-swcconfig
1313

1414
## Usage
1515

16+
## Convert config in a tsconfig file
17+
1618
```typescript
1719
import { convert } from 'tsconfig-to-swcconfig'
1820

@@ -30,6 +32,32 @@ convert('tsconfig-filename.json', process.cwd(), {
3032
})
3133
```
3234

35+
## Convert tsconfig value
36+
37+
Convert tsconfig value directly:
38+
39+
```typescript
40+
import { convert } from 'tsconfig-to-swcconfig'
41+
42+
const swcConfig = convertTsConfig({
43+
module: 'commonjs',
44+
target: 'es2018',
45+
strict: true,
46+
esModuleInterop: true,
47+
})
48+
```
49+
50+
Advanced usage:
51+
52+
```typescript
53+
import { convert } from 'tsconfig-to-swcconfig'
54+
55+
const swcConfig = convertTsConfig(
56+
{ target: 'es2018' }, // tsconfig
57+
{ minify: true }, // more swc config to override...
58+
)
59+
```
60+
3361
## License
3462

3563
MIT

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tsconfig-to-swcconfig",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Convert tsconfig to swc config",
55
"main": "dist/index.js",
66
"scripts": {

src/index.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type * as swcType from '@swc/core'
2+
import type * as tsType from 'typescript'
23
import deepmerge from 'deepmerge'
34
import { getTSOptions } from './utils'
45

@@ -8,8 +9,16 @@ export function convert(
89
/** cwd */
910
cwd: string = process.cwd(),
1011
/** swc configs to override */
12+
swcOptions?: swcType.Options,
13+
): swcType.Options {
14+
const tsOptions = getTSOptions(filename, cwd) ?? {}
15+
return convertTsConfig(tsOptions, swcOptions)
16+
}
17+
18+
export function convertTsConfig(
19+
tsOptions: tsType.CompilerOptions,
1120
swcOptions: swcType.Options = {},
12-
) {
21+
): swcType.Options {
1322
// https://json.schemastore.org/tsconfig
1423
const {
1524
esModuleInterop = false,
@@ -25,7 +34,7 @@ export function convert(
2534
alwaysStrict = false,
2635
noImplicitUseStrict = false,
2736
paths,
28-
} = getTSOptions(filename, cwd) ?? {}
37+
} = tsOptions
2938
const module = (_module as unknown as string)?.toLowerCase()
3039

3140
const transformedOptions = deepmerge(

test/index.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,32 @@ import { convert } from '../src/index'
44

55
test('convert tsconfig file', (t) => {
66
let result = convert()
7-
t.match(result.jsc.target, 'es2018')
7+
t.match(result.jsc?.target, 'es2018')
88

99
result = convert(
1010
'tsconfig-not-default.json',
1111
path.resolve(__dirname, 'fixtures', 'tsconfig'),
1212
{ minify: true },
1313
)
1414
t.match(result.sourceMaps, false)
15-
t.match(result.module.type, 'commonjs')
16-
t.match(result.module.noInterop, false)
15+
t.match(result.module?.type, 'commonjs')
16+
t.match(result.module?.noInterop, false)
1717
// @ts-ignore
18-
t.match(result.module.strictMode, true)
19-
t.match(result.jsc.externalHelpers, true)
20-
t.match(result.jsc.target, 'es3')
21-
t.match(result.jsc.parser?.decorators, true)
22-
t.match(result.jsc.transform?.decoratorMetadata, true)
23-
t.match(result.jsc.transform?.react?.pragma, 'React.createElement')
24-
t.match(result.jsc.transform?.react?.pragmaFrag, 'React.Fragment')
25-
t.match(result.jsc.keepClassNames, false)
18+
t.match(result.module?.strictMode, true)
19+
t.match(result.jsc?.externalHelpers, true)
20+
t.match(result.jsc?.target, 'es3')
21+
t.match(result.jsc?.parser?.decorators, true)
22+
t.match(result.jsc?.transform?.decoratorMetadata, true)
23+
t.match(result.jsc?.transform?.react?.pragma, 'React.createElement')
24+
t.match(result.jsc?.transform?.react?.pragmaFrag, 'React.Fragment')
25+
t.match(result.jsc?.keepClassNames, false)
2626
t.match(result.minify, true)
2727

2828
result = convert(
2929
'tsconfig-edge-case.json',
3030
path.resolve(__dirname, 'fixtures', 'tsconfig'),
3131
)
32-
t.match(result.module.noInterop, true)
32+
t.match(result.module?.noInterop, true)
3333

3434
t.end()
3535
})

0 commit comments

Comments
 (0)