@withstudiocms/config-utils
Version:
Utilities for managing configuration files
48 lines (47 loc) • 2.46 kB
TypeScript
import type { ImportBundledFileArgs, LoadAndBundleConfigFileArgs, LoadAndBundleConfigFileResult } from './types.js';
/**
* Bundle arbitrary `mjs` or `ts` file.
* Simplified fork from Vite's `bundleConfigFile` function.
*
* @see https://github.com/vitejs/vite/blob/main/packages/vite/src/node/config.ts#L961
*/
export declare function bundleConfigFile({ fileUrl }: {
fileUrl: URL;
}): Promise<{
code: string;
dependencies: string[];
}>;
/**
* Forked from Vite config loader, replacing CJS-based path concat with ESM only
*
* @see https://github.com/vitejs/vite/blob/main/packages/vite/src/node/config.ts#L1074
*/
export declare function importBundledFile({ code, root, label, }: ImportBundledFileArgs): Promise<{
default?: unknown;
}>;
/**
* Loads and bundles a configuration file, then imports the bundled module.
*
* @param params - The arguments for loading and bundling the config file.
* @param params.root - The root directory for resolving imports.
* @param params.fileUrl - The URL or path to the configuration file to load.
* @param params.label - A label used for logging or identification purposes.
* @returns A promise that resolves to an object containing the imported module (`mod`)
* and an array of its dependencies.
*/
export declare function loadAndBundleConfigFile({ root, fileUrl, label, }: LoadAndBundleConfigFileArgs): Promise<LoadAndBundleConfigFileResult>;
/**
* Loads a configuration file from a list of possible paths relative to a given root URL.
*
* Iterates through the provided `configPaths`, checking for the existence of each file.
* If a file is found, it is loaded and bundled, and its default export is returned as the configuration object.
* If no valid configuration file is found, or if the file does not have a valid default export, the function returns `undefined`
* or throws an error, respectively.
*
* @typeParam R - The expected shape of the configuration object.
* @param root - The base URL to resolve configuration file paths against.
* @param configPaths - An array of possible configuration file paths to check, relative to `root`.
* @returns A promise that resolves to the configuration object of type `R`, or `undefined` if no valid config file is found.
* @throws If a config file is found but does not have a valid default export.
*/
export declare function loadConfigFile<R>(root: URL, configPaths: string[], label?: string): Promise<R | undefined>;