|
| 1 | +import fs from 'node:fs'; |
| 2 | +import { dirname, isAbsolute, join } from 'node:path'; |
| 3 | +import { |
| 4 | + loadConfig as loadRsbuildConfig, |
| 5 | + type LoadConfigOptions as RsbuildLoadConfigOptions, |
| 6 | +} from '@rsbuild/core'; |
| 7 | +import type { RslibConfig } from './types'; |
| 8 | +import { color } from './utils/color'; |
| 9 | +import { logger } from './utils/logger'; |
| 10 | + |
| 11 | +export type ConfigParams = { |
| 12 | + env: string; |
| 13 | + command: string; |
| 14 | + envMode?: string; |
| 15 | + meta?: Record<string, unknown>; |
| 16 | +}; |
| 17 | + |
| 18 | +export type RslibConfigSyncFn = (env: ConfigParams) => RslibConfig; |
| 19 | + |
| 20 | +export type RslibConfigAsyncFn = (env: ConfigParams) => Promise<RslibConfig>; |
| 21 | + |
| 22 | +export type RslibConfigExport = |
| 23 | + | RslibConfig |
| 24 | + | RslibConfigSyncFn |
| 25 | + | RslibConfigAsyncFn; |
| 26 | + |
| 27 | +export type LoadConfigOptions = Pick< |
| 28 | + RsbuildLoadConfigOptions, |
| 29 | + 'cwd' | 'path' | 'envMode' | 'meta' | 'loader' |
| 30 | +>; |
| 31 | + |
| 32 | +export type ConfigLoader = RsbuildLoadConfigOptions['loader']; |
| 33 | + |
| 34 | +export type LoadConfigResult = { |
| 35 | + /** |
| 36 | + * The loaded configuration object. |
| 37 | + */ |
| 38 | + content: RslibConfig; |
| 39 | + /** |
| 40 | + * The path to the loaded configuration file. |
| 41 | + * Return `null` if the configuration file is not found. |
| 42 | + */ |
| 43 | + filePath: string | null; |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * This function helps you to autocomplete configuration types. |
| 48 | + * It accepts a Rslib config object, or a function that returns a config. |
| 49 | + */ |
| 50 | +export function defineConfig(config: RslibConfig): RslibConfig; |
| 51 | +export function defineConfig(config: RslibConfigSyncFn): RslibConfigSyncFn; |
| 52 | +export function defineConfig(config: RslibConfigAsyncFn): RslibConfigAsyncFn; |
| 53 | +export function defineConfig(config: RslibConfigExport): RslibConfigExport; |
| 54 | +export function defineConfig(config: RslibConfigExport) { |
| 55 | + return config; |
| 56 | +} |
| 57 | + |
| 58 | +const resolveConfigPath = ( |
| 59 | + root: string, |
| 60 | + customConfig?: string, |
| 61 | +): string | null => { |
| 62 | + if (customConfig) { |
| 63 | + const customConfigPath = isAbsolute(customConfig) |
| 64 | + ? customConfig |
| 65 | + : join(root, customConfig); |
| 66 | + if (fs.existsSync(customConfigPath)) { |
| 67 | + return customConfigPath; |
| 68 | + } |
| 69 | + logger.warn(`Cannot find config file: ${color.dim(customConfigPath)}\n`); |
| 70 | + } |
| 71 | + |
| 72 | + const CONFIG_FILES = [ |
| 73 | + // `.mjs` and `.ts` are the most used configuration types, |
| 74 | + // so we resolve them first for performance |
| 75 | + 'rslib.config.mjs', |
| 76 | + 'rslib.config.ts', |
| 77 | + 'rslib.config.js', |
| 78 | + 'rslib.config.cjs', |
| 79 | + 'rslib.config.mts', |
| 80 | + 'rslib.config.cts', |
| 81 | + ]; |
| 82 | + |
| 83 | + for (const file of CONFIG_FILES) { |
| 84 | + const configFile = join(root, file); |
| 85 | + |
| 86 | + if (fs.existsSync(configFile)) { |
| 87 | + return configFile; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return null; |
| 92 | +}; |
| 93 | + |
| 94 | +export async function loadConfig({ |
| 95 | + cwd = process.cwd(), |
| 96 | + path, |
| 97 | + envMode, |
| 98 | + meta, |
| 99 | + loader, |
| 100 | +}: LoadConfigOptions): Promise<LoadConfigResult> { |
| 101 | + const configFilePath = resolveConfigPath(cwd, path); |
| 102 | + |
| 103 | + if (!configFilePath) { |
| 104 | + logger.debug('no config file found.'); |
| 105 | + return { |
| 106 | + content: {} as RslibConfig, |
| 107 | + filePath: null, |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + const { content } = await loadRsbuildConfig({ |
| 112 | + cwd: dirname(configFilePath), |
| 113 | + path: configFilePath, |
| 114 | + envMode, |
| 115 | + meta, |
| 116 | + loader, |
| 117 | + }); |
| 118 | + |
| 119 | + return { content: content as RslibConfig, filePath: configFilePath }; |
| 120 | +} |
0 commit comments