diff --git a/packages/rstack/src/config.ts b/packages/rstack/src/config.ts index 59b8f92c..f806237f 100644 --- a/packages/rstack/src/config.ts +++ b/packages/rstack/src/config.ts @@ -23,7 +23,7 @@ type RslintConfigFactory = ( export type RslintConfigDefinition = RslintConfig | RslintConfigFactory; -export type Configs = { +export type RstackConfigDefinitions = { app?: RsbuildConfigDefinition; lib?: RslibConfigDefinition; doc?: RspressConfigDefinition; @@ -34,14 +34,14 @@ export type Configs = { }; /** Shared configuration input; lint factories receive the tool exports. */ -export type RstackConfig = Omit & { +export type RstackConfig = Omit & { extends?: readonly RstackConfig[]; lint?: RslintConfigDefinition; }; const normalizeLintConfig = ( config: RslintConfigDefinition, -): NonNullable => +): NonNullable => typeof config === 'function' ? async () => config(await import('@rslint/core')) : config; @@ -51,14 +51,14 @@ export const normalizeRstackConfig = ({ extends: _extends, lint, ...configs -}: RstackConfig): Configs => +}: RstackConfig): RstackConfigDefinitions => lint === undefined ? configs : { ...configs, lint: normalizeLintConfig(lint) }; export type LoadedRstackConfig = { /** Effective tool definitions. Configuration factories remain unevaluated. */ - configs: Configs; + configs: RstackConfigDefinitions; filePath: string | null; dependencies: string[]; }; @@ -79,7 +79,7 @@ export type LoadRstackConfigOptions = { }; type ConfigSession = { - configs: Configs; + configs: RstackConfigDefinitions; extends?: readonly RstackConfig[]; active: boolean; }; @@ -194,9 +194,9 @@ type Define = { staged: (config: StagedConfig) => void; }; -const setConfig = ( +const setConfig = ( type: T, - config: Configs[T], + config: RstackConfigDefinitions[T], ): void => { const session = getConfigSessionStorage().getStore(); diff --git a/packages/rstack/src/configExports.ts b/packages/rstack/src/configExports.ts index 7da0a774..8f79ec1b 100644 --- a/packages/rstack/src/configExports.ts +++ b/packages/rstack/src/configExports.ts @@ -1,7 +1,7 @@ /** Public configuration APIs exposed through `rstack/config`. */ export { loadRstackConfig, - type Configs, + type RstackConfigDefinitions, type LoadedRstackConfig, type LoadRstackConfigOptions, } from './config.ts'; diff --git a/packages/rstack/src/configLayers.ts b/packages/rstack/src/configLayers.ts index bc46628e..615db230 100644 --- a/packages/rstack/src/configLayers.ts +++ b/packages/rstack/src/configLayers.ts @@ -7,7 +7,7 @@ import type { RslintConfig } from '@rslint/core'; import type { UserConfig as RspressConfig } from '@rspress/core'; import type { RstestConfig } from '@rstest/core'; import { - type Configs, + type RstackConfigDefinitions, type RstackConfig, normalizeRstackConfig, } from './config.ts'; @@ -24,7 +24,7 @@ type ConfigValues = { staged: StagedConfig; }; -type ConfigArgs = K extends 'app' +type ConfigArgs = K extends 'app' ? [params: AppConfigParams] : K extends 'lib' ? [params: LibConfigParams] @@ -33,8 +33,8 @@ type ConfigArgs = K extends 'app' /** Expand inherited configs before their children, preserving every occurrence. */ export const flattenConfigLayers = ( configs: readonly RstackConfig[], -): Configs[] => { - const layers: Configs[] = []; +): RstackConfigDefinitions[] => { + const layers: RstackConfigDefinitions[] = []; const ancestors = new Map(); const visit = (config: RstackConfig, path: string): void => { @@ -63,8 +63,10 @@ export const flattenConfigLayers = ( * Resolve one tool from ordered, normalized config layers. Lint factories are * already wrapped by define.lint. This function does not merge the results. */ -export const resolveConfigLayers = async ( - layers: readonly Configs[], +export const resolveConfigLayers = async < + K extends keyof RstackConfigDefinitions, +>( + layers: readonly RstackConfigDefinitions[], kind: K, ...args: ConfigArgs ): Promise => { @@ -91,14 +93,14 @@ export const resolveConfigLayers = async ( }; export const resolveRslintConfig = async ( - layers: readonly Configs[], + layers: readonly RstackConfigDefinitions[], ): Promise => { const configs = await resolveConfigLayers(layers, 'lint'); return configs.length > 1 ? configs.flat() : configs[0]; }; export const resolveStagedConfig = async ( - layers: readonly Configs[], + layers: readonly RstackConfigDefinitions[], ): Promise => { const configs = await resolveConfigLayers(layers, 'staged'); if (configs.length <= 1) { @@ -114,7 +116,7 @@ export const resolveStagedConfig = async ( }; export const resolveFmtConfigLayers = async ( - layers: readonly Configs[], + layers: readonly RstackConfigDefinitions[], ): Promise => { const configs = await resolveConfigLayers(layers, 'fmt'); return configs.length > 1 @@ -124,13 +126,13 @@ export const resolveFmtConfigLayers = async ( /** Compose effective definitions without executing any tool or task factories. */ export const composeConfigLayers = async ( - layers: readonly Configs[], -): Promise => { + layers: readonly RstackConfigDefinitions[], +): Promise => { if (layers.length <= 1) { return layers[0] ?? {}; } - const configs: Configs = {}; + const configs: RstackConfigDefinitions = {}; for (const kind of [ 'app', 'lib', diff --git a/packages/rstack/src/fmt/config.ts b/packages/rstack/src/fmt/config.ts index 1d9ba7b0..68f00747 100644 --- a/packages/rstack/src/fmt/config.ts +++ b/packages/rstack/src/fmt/config.ts @@ -1,6 +1,6 @@ import { dirname } from 'node:path'; import micromatch from 'micromatch'; -import type { Configs } from '../config.ts'; +import type { RstackConfigDefinitions } from '../config.ts'; import { resolveFmtConfigLayers } from '../configLayers.ts'; import { createRelativePathResolver } from './pathHelpers.ts'; import type { @@ -10,7 +10,7 @@ import type { } from './types.ts'; type ResolveFmtConfigOptions = { - layers: readonly Configs[]; + layers: readonly RstackConfigDefinitions[]; configFilePath: string | null; cwd: string; }; diff --git a/packages/rstack/src/rsbuildConfig.ts b/packages/rstack/src/rsbuildConfig.ts index a6ef3cc1..84207472 100644 --- a/packages/rstack/src/rsbuildConfig.ts +++ b/packages/rstack/src/rsbuildConfig.ts @@ -5,11 +5,11 @@ import { mergeRsbuildConfig, } from '@rsbuild/core'; import { withConfigMeta } from '@rstackjs/load-config'; -import { loadRstackConfig, type Configs } from './config.ts'; +import { loadRstackConfig, type RstackConfigDefinitions } from './config.ts'; import { resolveConfigLayers } from './configLayers.ts'; export const resolveRsbuildConfig = async ( - layers: readonly Configs[], + layers: readonly RstackConfigDefinitions[], params: ConfigParams, ): Promise => { const configs = await resolveConfigLayers(layers, 'app', params); diff --git a/packages/rstack/src/rslibConfig.ts b/packages/rstack/src/rslibConfig.ts index 3faaf14e..749d30b3 100644 --- a/packages/rstack/src/rslibConfig.ts +++ b/packages/rstack/src/rslibConfig.ts @@ -5,11 +5,11 @@ import { mergeRslibConfig, } from '@rslib/core'; import { withConfigMeta } from '@rstackjs/load-config'; -import { loadRstackConfig, type Configs } from './config.ts'; +import { loadRstackConfig, type RstackConfigDefinitions } from './config.ts'; import { resolveConfigLayers } from './configLayers.ts'; export const resolveRslibConfig = async ( - layers: readonly Configs[], + layers: readonly RstackConfigDefinitions[], params: ConfigParams, ): Promise => { const configs = await resolveConfigLayers(layers, 'lib', params); diff --git a/packages/rstack/src/rspressConfig.ts b/packages/rstack/src/rspressConfig.ts index 401cd401..afa5f0cd 100644 --- a/packages/rstack/src/rspressConfig.ts +++ b/packages/rstack/src/rspressConfig.ts @@ -1,10 +1,10 @@ import type { WatchFiles } from '@rsbuild/core'; import type { UserConfig } from '@rspress/core'; -import { loadRstackConfig, type Configs } from './config.ts'; +import { loadRstackConfig, type RstackConfigDefinitions } from './config.ts'; import { resolveConfigLayers } from './configLayers.ts'; export const resolveRspressConfig = async ( - layers: readonly Configs[], + layers: readonly RstackConfigDefinitions[], ): Promise => { const configs = await resolveConfigLayers(layers, 'doc'); if (configs.length <= 1) { diff --git a/packages/rstack/src/rstestConfig.ts b/packages/rstack/src/rstestConfig.ts index d02868ba..34700532 100644 --- a/packages/rstack/src/rstestConfig.ts +++ b/packages/rstack/src/rstestConfig.ts @@ -4,11 +4,11 @@ import { type RstestConfigExport, mergeRstestConfig, } from '@rstest/core'; -import { loadRstackConfig, type Configs } from './config.ts'; +import { loadRstackConfig, type RstackConfigDefinitions } from './config.ts'; import { resolveConfigLayers } from './configLayers.ts'; const resolveAutomaticExtends = async ( - layers: readonly Configs[], + layers: readonly RstackConfigDefinitions[], params: ConfigParams, ): Promise => { // Prefer the app when both app and lib are defined. Merging both adapters can @@ -55,7 +55,7 @@ const injectExtends = ( }; const extendsConfig = async ( - layers: readonly Configs[], + layers: readonly RstackConfigDefinitions[], testConfig: RstestConfig, params: ConfigParams, ) => { @@ -88,7 +88,7 @@ const extendsConfig = async ( }; export const mergeRstestConfigLayers = async ( - layers: readonly Configs[], + layers: readonly RstackConfigDefinitions[], ): Promise => { const configs = await resolveConfigLayers(layers, 'test'); return configs.length > 1 @@ -97,7 +97,7 @@ export const mergeRstestConfigLayers = async ( }; export const resolveRstestConfig = async ( - layers: readonly Configs[], + layers: readonly RstackConfigDefinitions[], params: ConfigParams, ): Promise => { const testConfig = await mergeRstestConfigLayers(layers); diff --git a/packages/rstack/tests/types/resolution-bundler/index.ts b/packages/rstack/tests/types/resolution-bundler/index.ts index 0065184e..b40f9b34 100644 --- a/packages/rstack/tests/types/resolution-bundler/index.ts +++ b/packages/rstack/tests/types/resolution-bundler/index.ts @@ -6,7 +6,7 @@ import { define, type RstackConfig } from 'rstack'; import { createRsbuild, defineConfig as defineAppConfig } from 'rstack/app'; import { loadRstackConfig, - type Configs, + type RstackConfigDefinitions, type LoadedRstackConfig, type LoadRstackConfigOptions, } from 'rstack/config'; @@ -21,7 +21,7 @@ const loadOptions: LoadRstackConfigOptions = { configFilePath: 'rstack.config.ts', }; const loadedConfig: Promise = loadRstackConfig(loadOptions); -const configs: Configs = {}; +const configs: RstackConfigDefinitions = {}; const sharedConfig: RstackConfig = { app: appConfig, lint: lintConfig, diff --git a/packages/rstack/tests/types/resolution-nodenext/index.ts b/packages/rstack/tests/types/resolution-nodenext/index.ts index 0065184e..b40f9b34 100644 --- a/packages/rstack/tests/types/resolution-nodenext/index.ts +++ b/packages/rstack/tests/types/resolution-nodenext/index.ts @@ -6,7 +6,7 @@ import { define, type RstackConfig } from 'rstack'; import { createRsbuild, defineConfig as defineAppConfig } from 'rstack/app'; import { loadRstackConfig, - type Configs, + type RstackConfigDefinitions, type LoadedRstackConfig, type LoadRstackConfigOptions, } from 'rstack/config'; @@ -21,7 +21,7 @@ const loadOptions: LoadRstackConfigOptions = { configFilePath: 'rstack.config.ts', }; const loadedConfig: Promise = loadRstackConfig(loadOptions); -const configs: Configs = {}; +const configs: RstackConfigDefinitions = {}; const sharedConfig: RstackConfig = { app: appConfig, lint: lintConfig, diff --git a/website/docs/en/guide/api-reference.mdx b/website/docs/en/guide/api-reference.mdx index 18b60a5a..4e63f2bd 100644 --- a/website/docs/en/guide/api-reference.mdx +++ b/website/docs/en/guide/api-reference.mdx @@ -130,7 +130,7 @@ The returned object has these fields: `loadRstackConfig()` only loads configurations; it does not run tool configuration functions. Before running a tool, the caller must resolve its configuration and initialize it. -`rstack/config` also exports the related types: `LoadRstackConfigOptions` for the parameters, `LoadedRstackConfig` for the return value, and `Configs` for the tool configurations. +`rstack/config` also exports the related types: `LoadRstackConfigOptions` for the parameters, `LoadedRstackConfig` for the return value, and `RstackConfigDefinitions` for the tool configurations. ## TypeScript types diff --git a/website/docs/zh/guide/api-reference.mdx b/website/docs/zh/guide/api-reference.mdx index 14f52c75..4d2c3fd8 100644 --- a/website/docs/zh/guide/api-reference.mdx +++ b/website/docs/zh/guide/api-reference.mdx @@ -130,7 +130,7 @@ const { configs, filePath, dependencies } = await loadRstackConfig({ `loadRstackConfig()` 仅负责加载配置,不会执行工具配置函数。调用方仍需完成相应工具的配置解析和初始化,才能运行工具。 -`rstack/config` 还导出相关类型:`LoadRstackConfigOptions`(参数)、`LoadedRstackConfig`(返回值)和 `Configs`(工具配置)。 +`rstack/config` 还导出相关类型:`LoadRstackConfigOptions`(参数)、`LoadedRstackConfig`(返回值)和 `RstackConfigDefinitions`(工具配置)。 ## TypeScript 类型 \{#typescript-types}