| 2294 | } |
| 2295 | |
| 2296 | export async function loadConfigFromFile( |
| 2297 | configEnv: ConfigEnv, |
| 2298 | configFile?: string, |
| 2299 | configRoot: string = process.cwd(), |
| 2300 | logLevel?: LogLevel, |
| 2301 | customLogger?: Logger, |
| 2302 | configLoader: 'bundle' | 'runner' | 'native' = 'bundle', |
| 2303 | ): Promise<{ |
| 2304 | path: string |
| 2305 | config: UserConfig |
| 2306 | dependencies: string[] |
| 2307 | } | null> { |
| 2308 | if ( |
| 2309 | configLoader !== 'bundle' && |
| 2310 | configLoader !== 'runner' && |
| 2311 | configLoader !== 'native' |
| 2312 | ) { |
| 2313 | throw new Error( |
| 2314 | `Unsupported configLoader: ${configLoader}. Accepted values are 'bundle', 'runner', and 'native'.`, |
| 2315 | ) |
| 2316 | } |
| 2317 | |
| 2318 | const start = performance.now() |
| 2319 | const getTime = () => `${(performance.now() - start).toFixed(2)}ms` |
| 2320 | |
| 2321 | let resolvedPath: string | undefined |
| 2322 | |
| 2323 | if (configFile) { |
| 2324 | // explicit config path is always resolved from cwd |
| 2325 | resolvedPath = path.resolve(configFile) |
| 2326 | } else { |
| 2327 | // implicit config file loaded from inline root (if present) |
| 2328 | // otherwise from cwd |
| 2329 | for (const filename of DEFAULT_CONFIG_FILES) { |
| 2330 | const filePath = path.resolve(configRoot, filename) |
| 2331 | if (!fs.existsSync(filePath)) continue |
| 2332 | |
| 2333 | resolvedPath = filePath |
| 2334 | break |
| 2335 | } |
| 2336 | } |
| 2337 | |
| 2338 | if (!resolvedPath) { |
| 2339 | debug?.('no config file found.') |
| 2340 | return null |
| 2341 | } |
| 2342 | |
| 2343 | try { |
| 2344 | const resolver = |
| 2345 | configLoader === 'bundle' |
| 2346 | ? bundleAndLoadConfigFile |
| 2347 | : configLoader === 'runner' |
| 2348 | ? runnerImportConfigFile |
| 2349 | : nativeImportConfigFile |
| 2350 | const { configExport, dependencies } = await resolver(resolvedPath) |
| 2351 | debug?.(`config file loaded in ${getTime()}`) |
| 2352 | |
| 2353 | const config = await (typeof configExport === 'function' |