Skip to content

Commit 0306f60

Browse files
authored
perf(register): cache readDefaultTsConfig results (#918)
Previously, readDefaultTsConfig would read the tsconfig.json file every time it was called, which would be at least once for every file being compiled. By caching the results so that the tsconfig.json file only needs to be read once, build times can be dramatically reduced, bringing them in-line with build times when using a hardcoded config. As an example, on a personal project, this shaves around 25 seconds off of a 40 second build, meaning it speeds up the build by more than two times.
1 parent 170baa0 commit 0306f60

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

packages/register/read-default-tsconfig.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import * as ts from 'typescript'
88

99
const debug = debugFactory('@swc-node')
1010

11+
const configCache: Record<string, Partial<ts.CompilerOptions & { fallbackToTs: (path: string) => boolean }>> = {}
12+
1113
export function readDefaultTsConfig(
1214
tsConfigPath = process.env.SWC_NODE_PROJECT ?? process.env.TS_NODE_PROJECT ?? join(process.cwd(), 'tsconfig.json'),
1315
) {
@@ -25,6 +27,10 @@ export function readDefaultTsConfig(
2527

2628
const fullTsConfigPath = resolve(tsConfigPath)
2729

30+
if (fullTsConfigPath in configCache) {
31+
return configCache[fullTsConfigPath]
32+
}
33+
2834
if (!existsSync(fullTsConfigPath)) {
2935
return compilerOptions
3036
}
@@ -50,6 +56,8 @@ export function readDefaultTsConfig(
5056
console.info(yellow(`Read ${tsConfigPath} failed: ${(e as Error).message}`))
5157
}
5258

59+
configCache[fullTsConfigPath] = compilerOptions
60+
5361
return compilerOptions
5462
}
5563

@@ -128,7 +136,7 @@ export function tsCompilerOptionsToSwcConfig(options: ts.CompilerOptions, filena
128136
keepClassNames: true,
129137
externalHelpers: Boolean(options.importHelpers),
130138
react:
131-
options.jsxFactory ?? options.jsxFragmentFactory ?? options.jsx ?? options.jsxImportSource
139+
(options.jsxFactory ?? options.jsxFragmentFactory ?? options.jsx ?? options.jsxImportSource)
132140
? {
133141
pragma: options.jsxFactory,
134142
pragmaFrag: options.jsxFragmentFactory,

0 commit comments

Comments
 (0)