Skip to content

Commit

Permalink
refactor: use get-tsconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
songkeys committed Sep 27, 2023
1 parent 18ae520 commit 7f366a8
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 311 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

Convert tsconfig to swc config.

> Why?
>
> (https://github.com/swc-project/swc/issues/1348)
>
> `swc` has no plans to support `tsconfig.json`, but it could be useful in some cases. For example, migrating from `tsc` to `swc` in a large project, you can use this tool to convert `tsconfig.json` to `.swcrc`, and then modify the `.swcrc` to make it work.
## Install

```bash
Expand Down
211 changes: 16 additions & 195 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 4 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
{
"name": "tsconfig-to-swcconfig",
"version": "2.6.0",
"version": "2.7.0",
"description": "Convert tsconfig to swc config",
"keywords": [
"swc",
"tsconfig",
"converter",
"config"
],
"keywords": ["swc", "tsconfig", "converter", "config"],
"homepage": "https://github.com/Songkeys/tsconfig-to-swcconfig#readme",
"bugs": {
"url": "https://github.com/Songkeys/tsconfig-to-swcconfig/issues"
Expand All @@ -23,9 +18,7 @@
"t2s": "dist/cli.js",
"tsconfig-to-swcconfig": "dist/cli.js"
},
"files": [
"dist"
],
"files": ["dist"],
"scripts": {
"build": "tsc",
"format": "biome format . --write",
Expand All @@ -39,18 +32,15 @@
},
"dependencies": {
"@fastify/deepmerge": "^1.3.0",
"joycon": "^3.1.1",
"jsonc-parser": "^3.2.0"
"get-tsconfig": "^4.7.2"
},
"devDependencies": {
"@biomejs/biome": "^1.2.2",
"@swc/core": "^1.3.89",
"@tsconfig/node12": "^12.1.0",
"@types/node": "^20.7.0",
"@types/source-map-support": "^0.5.8",
"prettier": "^3.0.3",
"simple-git-hooks": "^2.9.0",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
11 changes: 9 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { writeFile } from 'node:fs'
import { parseArgs } from 'node:util'
import type swcType from '@swc/core'
import { convert } from './index'
import { parseParamValue } from './utils'

const {
values: { filename, cwd, output, help, set: overrideValues },
Expand Down Expand Up @@ -63,7 +62,15 @@ const overrides = overrideValues?.reduce((all, a) => {
return o[s]
}, all)

parent[key] = parseParamValue(prop, value)
if (value === 'undefined') {
parent[key] = undefined
} else {
try {
parent[key] = JSON.parse(value)
} catch (_) {
parent[key] = value
}
}

return all
}, {} as any) as swcType.Options
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Deepmerge from '@fastify/deepmerge'
import type swcType from '@swc/core'
import type tsType from 'typescript'
import { type TsConfigJson } from 'get-tsconfig'
import { getTSOptions } from './utils'

const deepmerge = Deepmerge()
Expand All @@ -18,7 +18,7 @@ export function convert(
}

export function convertTsConfig(
tsOptions: tsType.CompilerOptions,
tsOptions: TsConfigJson.CompilerOptions,
swcOptions: swcType.Options = {},
): swcType.Options {
// https://json.schemastore.org/tsconfig
Expand Down Expand Up @@ -92,7 +92,9 @@ export function convertTsConfig(
const availableModuleTypes = ['commonjs', 'amd', 'umd', 'es6'] as const
type Module = typeof availableModuleTypes[number]

function moduleType(m: tsType.ModuleKind | undefined): Module {
function moduleType(
m: TsConfigJson.CompilerOptions.Module | undefined,
): Module {
const module = (m as unknown as string)?.toLowerCase()
if (availableModuleTypes.includes(module as any)) {
return module as Module
Expand Down
Loading

0 comments on commit 7f366a8

Please sign in to comment.