@@ -2,6 +2,7 @@ import ansis from 'ansis';
22import { createJiti } from 'jiti' ;
33import { mkdir , readFile , readdir , rm , stat } from 'node:fs/promises' ;
44import path from 'node:path' ;
5+ import { parseJsonConfigFileContent , readConfigFile , sys } from 'typescript' ;
56import type { Format , PersistConfig } from '@code-pushup/models' ;
67import { formatBytes } from './formatting.js' ;
78import { logMultipleResults } from './log-results.js' ;
@@ -77,16 +78,80 @@ export function logMultipleFileResults(
7778 ) ;
7879}
7980
80- const jitiImport = createJiti ( process . cwd ( ) ) ;
81+ export function loadTargetConfig ( tsConfigPath : string ) {
82+ const resolvedConfigPath = path . resolve ( tsConfigPath ) ;
83+ const { config, error } = readConfigFile ( resolvedConfigPath , sys . readFile ) ;
84+
85+ if ( error ) {
86+ throw new Error (
87+ `Error reading TypeScript config file at ${ tsConfigPath } :\n${ error . messageText } ` ,
88+ ) ;
89+ }
90+
91+ const parsedConfig = parseJsonConfigFileContent (
92+ config ,
93+ sys ,
94+ path . dirname ( resolvedConfigPath ) ,
95+ { } ,
96+ resolvedConfigPath ,
97+ ) ;
98+
99+ if ( parsedConfig . fileNames . length === 0 ) {
100+ throw new Error (
101+ 'No files matched by the TypeScript configuration. Check your "include", "exclude" or "files" settings.' ,
102+ ) ;
103+ }
104+
105+ return parsedConfig ;
106+ }
107+
81108type JitiOptions = Parameters < typeof createJiti > [ 1 ] ;
82109
110+ export function tsConfigToJitiOptionsTransformer (
111+ tsConfigPath : string ,
112+ ) : JitiOptions {
113+ const parsedConfig = loadTargetConfig ( tsConfigPath ) ;
114+ const compilerOptions = parsedConfig . options ;
115+
116+ const jitiOptions : JitiOptions = { } ;
117+
118+ if ( compilerOptions . paths ) {
119+ const aliases : Record < string , string > = { } ;
120+ const basePath = compilerOptions . baseUrl || path . dirname ( tsConfigPath ) ;
121+
122+ for ( const alias in compilerOptions . paths ) {
123+ const paths = compilerOptions . paths [ alias ] ;
124+ if ( paths && paths . length > 0 ) {
125+ aliases [ alias ] = path . resolve ( basePath , paths [ 0 ] as string ) ;
126+ }
127+ }
128+ jitiOptions . alias = aliases ;
129+ }
130+
131+ if ( compilerOptions . jsx ) {
132+ jitiOptions . jsx = true ;
133+ }
134+
135+ return jitiOptions ;
136+ }
137+
83138export async function importModule < T = unknown > (
84- options : JitiOptions & { filepath : string } ,
139+ options : JitiOptions & { filepath : string ; tsconfig ?: string } ,
85140) : Promise < T > {
86- const { filepath } = options ;
87- const { mod } = await jitiImport ( filepath ) ;
141+ const { filepath, tsconfig, ...jitiOptions } = options ;
142+
143+ const jitiOptionsFromTsConfig = tsconfig
144+ ? tsConfigToJitiOptionsTransformer ( tsconfig )
145+ : { } ;
146+
147+ const jiti = createJiti ( process . cwd ( ) , {
148+ ...jitiOptions ,
149+ ...jitiOptionsFromTsConfig ,
150+ } ) ;
151+
152+ const mod = await jiti . import ( filepath ) ;
88153
89- if ( typeof mod === 'object' && 'default' in mod ) {
154+ if ( mod && typeof mod === 'object' && 'default' in mod ) {
90155 return mod . default as T ;
91156 }
92157 return mod as T ;
0 commit comments