@@ -2,8 +2,8 @@ import { type JitiOptions, createJiti as createJitiSource } from 'jiti';
22import { stat } from 'node:fs/promises' ;
33import path from 'node:path' ;
44import type { CompilerOptions } from 'typescript' ;
5- import { fileExists } from './file-system' ;
6- import { loadTargetConfig } from './load-ts-config' ;
5+ import { fileExists } from './file-system.js ' ;
6+ import { loadTargetConfig } from './load-ts-config.js ' ;
77import { settlePromise } from './promises.js' ;
88
99export async function importModule < T = unknown > (
@@ -132,11 +132,17 @@ export async function createTsJiti(
132132) {
133133 const { tsconfigPath, ...jitiOptions } = options ;
134134 const fallbackTsconfigPath = path . resolve ( './tsconfig.json' ) ;
135- const tsDerivedJitiOptions : MappableJitiOptions = tsconfigPath
136- ? await jitiOptionsFromTsConfig ( tsconfigPath )
137- : ( await fileExists ( fallbackTsconfigPath ) )
138- ? await jitiOptionsFromTsConfig ( tsconfigPath )
139- : { } ;
135+
136+ const validPath : null | string =
137+ tsconfigPath == null
138+ ? ( await fileExists ( fallbackTsconfigPath ) )
139+ ? fallbackTsconfigPath
140+ : null
141+ : tsconfigPath ;
142+
143+ const tsDerivedJitiOptions : MappableJitiOptions = validPath
144+ ? await jitiOptionsFromTsConfig ( validPath )
145+ : { } ;
140146 return createJiti ( filepath , { ...jitiOptions , ...tsDerivedJitiOptions } ) ;
141147}
142148
@@ -147,26 +153,7 @@ export async function createTsJiti(
147153export async function jitiOptionsFromTsConfig (
148154 tsconfigPath : string ,
149155) : Promise < MappableJitiOptions > {
150- const compilerOptions = await deriveTsConfig ( tsconfigPath ) ;
151- const tsconfigDir = path . dirname ( tsconfigPath ) ;
152- return parseTsConfigToJitiConfig ( compilerOptions , tsconfigDir ) ;
153- }
154-
155- /**
156- * Read tsconfig file by path and return the parsed options as JSON object
157- * @param tsconfigPath
158- */
159- export async function deriveTsConfig (
160- tsconfigPath : string ,
161- ) : Promise < CompilerOptions > {
162- // check if tsconfig file exists
163- const exists = await fileExists ( tsconfigPath ) ;
164- if ( ! exists ) {
165- throw new Error (
166- `Tsconfig file not found at path: ${ tsconfigPath . replace ( / \\ / g, '/' ) } ` ,
167- ) ;
168- }
169-
170156 const { options } = loadTargetConfig ( tsconfigPath ) ;
171157 return options ;
158+ return parseTsConfigToJitiConfig ( options , path . dirname ( tsconfigPath ) ) ;
172159}
0 commit comments