-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
read-default-tsconfig.ts
142 lines (130 loc) · 4.43 KB
/
read-default-tsconfig.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { existsSync } from 'fs'
import { join, dirname, resolve } from 'path'
import type { Options } from '@swc-node/core'
import { yellow } from 'colorette'
import debugFactory from 'debug'
import * as ts from 'typescript'
const debug = debugFactory('@swc-node')
export function readDefaultTsConfig(
tsConfigPath = process.env.SWC_NODE_PROJECT ?? process.env.TS_NODE_PROJECT ?? join(process.cwd(), 'tsconfig.json'),
) {
let compilerOptions: Partial<ts.CompilerOptions & { fallbackToTs: (path: string) => boolean }> = {
target: ts.ScriptTarget.ES2018,
module: ts.ModuleKind.CommonJS,
moduleResolution: ts.ModuleResolutionKind.NodeJs,
sourceMap: true,
esModuleInterop: true,
}
if (!tsConfigPath) {
return compilerOptions
}
const fullTsConfigPath = resolve(tsConfigPath)
if (!existsSync(fullTsConfigPath)) {
return compilerOptions
}
try {
debug(`Read config file from ${fullTsConfigPath}`)
const { config } = ts.readConfigFile(fullTsConfigPath, ts.sys.readFile)
const { options, errors, fileNames } = ts.parseJsonConfigFileContent(config, ts.sys, dirname(fullTsConfigPath))
if (!errors.length) {
compilerOptions = options
compilerOptions.files = fileNames
} else {
console.info(yellow(`Convert compiler options from json failed, ${errors.map((d) => d.messageText).join('\n')}`))
}
} catch (e) {
console.info(yellow(`Read ${tsConfigPath} failed: ${(e as Error).message}`))
}
return compilerOptions
}
function toTsTarget(target: ts.ScriptTarget): Options['target'] {
switch (target) {
case ts.ScriptTarget.ES3:
return 'es3'
case ts.ScriptTarget.ES5:
return 'es5'
case ts.ScriptTarget.ES2015:
return 'es2015'
case ts.ScriptTarget.ES2016:
return 'es2016'
case ts.ScriptTarget.ES2017:
return 'es2017'
case ts.ScriptTarget.ES2018:
return 'es2018'
case ts.ScriptTarget.ES2019:
return 'es2019'
case ts.ScriptTarget.ES2020:
return 'es2020'
case ts.ScriptTarget.ES2021:
return 'es2021'
case ts.ScriptTarget.ES2022:
case ts.ScriptTarget.ESNext:
case ts.ScriptTarget.Latest:
return 'es2022'
case ts.ScriptTarget.JSON:
return 'es5'
}
}
function toModule(moduleKind: ts.ModuleKind) {
switch (moduleKind) {
case ts.ModuleKind.CommonJS:
return 'commonjs'
case ts.ModuleKind.UMD:
return 'umd'
case ts.ModuleKind.AMD:
return 'amd'
case ts.ModuleKind.ES2015:
case ts.ModuleKind.ES2020:
case ts.ModuleKind.ES2022:
case ts.ModuleKind.ESNext:
case ts.ModuleKind.Node16:
case ts.ModuleKind.NodeNext:
case ts.ModuleKind.None:
return 'es6'
case ts.ModuleKind.System:
throw new TypeError('Do not support system kind module')
}
}
export function tsCompilerOptionsToSwcConfig(options: ts.CompilerOptions, filename: string): Options {
const isJsx = filename.endsWith('.tsx') || filename.endsWith('.jsx') || Boolean(options.jsx)
return {
module: toModule(options.module ?? ts.ModuleKind.ES2015),
sourcemap: Boolean(options.sourceMap),
experimentalDecorators: options.experimentalDecorators ?? false,
emitDecoratorMetadata: options.emitDecoratorMetadata ?? false,
esModuleInterop: options.esModuleInterop ?? false,
swc: {
filename,
inputSourceMap: options.inlineSourceMap,
sourceRoot: options.sourceRoot,
jsc: {
externalHelpers: Boolean(options.importHelpers),
parser: {
syntax: 'typescript',
tsx: isJsx,
dynamicImport: true,
},
paths: Object.fromEntries(
Object.entries(options.paths ?? {}).map(([aliasKey, aliasPaths]) => [
aliasKey,
((aliasPaths as string[]) ?? []).map((path) => resolve(options.baseUrl ?? './', path)),
]),
) as Options['paths'],
keepClassNames: true,
target: toTsTarget(options.target ?? ts.ScriptTarget.ES2018),
transform: {
react:
options.jsxFactory || options.jsxFragmentFactory || options.jsx || options.jsxImportSource
? {
pragma: options.jsxFactory,
pragmaFrag: options.jsxFragmentFactory,
importSource: options.jsxImportSource ?? 'react',
runtime: (options.jsx ?? 0) >= ts.JsxEmit.ReactJSX ? 'automatic' : 'classic',
useBuiltins: true,
}
: undefined,
},
},
},
}
}