Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/rstack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RslintConfigFactory = (

export type RslintConfigDefinition = RslintConfig | RslintConfigFactory;

export type Configs = {
export type RstackConfigDefinitions = {
app?: RsbuildConfigDefinition;
lib?: RslibConfigDefinition;
doc?: RspressConfigDefinition;
Expand All @@ -34,14 +34,14 @@ export type Configs = {
};

/** Shared configuration input; lint factories receive the tool exports. */
export type RstackConfig = Omit<Configs, 'lint'> & {
export type RstackConfig = Omit<RstackConfigDefinitions, 'lint'> & {
extends?: readonly RstackConfig[];
lint?: RslintConfigDefinition;
};

const normalizeLintConfig = (
config: RslintConfigDefinition,
): NonNullable<Configs['lint']> =>
): NonNullable<RstackConfigDefinitions['lint']> =>
typeof config === 'function'
? async () => config(await import('@rslint/core'))
: config;
Expand All @@ -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[];
};
Expand All @@ -79,7 +79,7 @@ export type LoadRstackConfigOptions = {
};

type ConfigSession = {
configs: Configs;
configs: RstackConfigDefinitions;
extends?: readonly RstackConfig[];
active: boolean;
};
Expand Down Expand Up @@ -194,9 +194,9 @@ type Define = {
staged: (config: StagedConfig) => void;
};

const setConfig = <T extends keyof Configs>(
const setConfig = <T extends keyof RstackConfigDefinitions>(
type: T,
config: Configs[T],
config: RstackConfigDefinitions[T],
): void => {
const session = getConfigSessionStorage().getStore();

Expand Down
2 changes: 1 addition & 1 deletion packages/rstack/src/configExports.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** Public configuration APIs exposed through `rstack/config`. */
export {
loadRstackConfig,
type Configs,
type RstackConfigDefinitions,
type LoadedRstackConfig,
type LoadRstackConfigOptions,
} from './config.ts';
26 changes: 14 additions & 12 deletions packages/rstack/src/configLayers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -24,7 +24,7 @@ type ConfigValues = {
staged: StagedConfig;
};

type ConfigArgs<K extends keyof Configs> = K extends 'app'
type ConfigArgs<K extends keyof RstackConfigDefinitions> = K extends 'app'
? [params: AppConfigParams]
: K extends 'lib'
? [params: LibConfigParams]
Expand All @@ -33,8 +33,8 @@ type ConfigArgs<K extends keyof Configs> = 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<RstackConfig, string>();

const visit = (config: RstackConfig, path: string): void => {
Expand Down Expand Up @@ -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 <K extends keyof Configs>(
layers: readonly Configs[],
export const resolveConfigLayers = async <
K extends keyof RstackConfigDefinitions,
>(
layers: readonly RstackConfigDefinitions[],
kind: K,
...args: ConfigArgs<K>
): Promise<ConfigValues[K][]> => {
Expand All @@ -91,14 +93,14 @@ export const resolveConfigLayers = async <K extends keyof Configs>(
};

export const resolveRslintConfig = async (
layers: readonly Configs[],
layers: readonly RstackConfigDefinitions[],
): Promise<RslintConfig | undefined> => {
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<StagedConfig | undefined> => {
const configs = await resolveConfigLayers(layers, 'staged');
if (configs.length <= 1) {
Expand All @@ -114,7 +116,7 @@ export const resolveStagedConfig = async (
};

export const resolveFmtConfigLayers = async (
layers: readonly Configs[],
layers: readonly RstackConfigDefinitions[],
): Promise<FmtConfig | undefined> => {
const configs = await resolveConfigLayers(layers, 'fmt');
return configs.length > 1
Expand All @@ -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<Configs> => {
layers: readonly RstackConfigDefinitions[],
): Promise<RstackConfigDefinitions> => {
if (layers.length <= 1) {
return layers[0] ?? {};
}

const configs: Configs = {};
const configs: RstackConfigDefinitions = {};
for (const kind of [
'app',
'lib',
Expand Down
4 changes: 2 additions & 2 deletions packages/rstack/src/fmt/config.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,7 +10,7 @@ import type {
} from './types.ts';

type ResolveFmtConfigOptions = {
layers: readonly Configs[];
layers: readonly RstackConfigDefinitions[];
configFilePath: string | null;
cwd: string;
};
Expand Down
4 changes: 2 additions & 2 deletions packages/rstack/src/rsbuildConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RsbuildConfig> => {
const configs = await resolveConfigLayers(layers, 'app', params);
Expand Down
4 changes: 2 additions & 2 deletions packages/rstack/src/rslibConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RslibConfig> => {
const configs = await resolveConfigLayers(layers, 'lib', params);
Expand Down
4 changes: 2 additions & 2 deletions packages/rstack/src/rspressConfig.ts
Original file line number Diff line number Diff line change
@@ -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<UserConfig> => {
const configs = await resolveConfigLayers(layers, 'doc');
if (configs.length <= 1) {
Expand Down
10 changes: 5 additions & 5 deletions packages/rstack/src/rstestConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RstestConfig['extends'] | undefined> => {
// Prefer the app when both app and lib are defined. Merging both adapters can
Expand Down Expand Up @@ -55,7 +55,7 @@ const injectExtends = <T extends RstestConfig>(
};

const extendsConfig = async (
layers: readonly Configs[],
layers: readonly RstackConfigDefinitions[],
testConfig: RstestConfig,
params: ConfigParams,
) => {
Expand Down Expand Up @@ -88,7 +88,7 @@ const extendsConfig = async (
};

export const mergeRstestConfigLayers = async (
layers: readonly Configs[],
layers: readonly RstackConfigDefinitions[],
): Promise<RstestConfig> => {
const configs = await resolveConfigLayers(layers, 'test');
return configs.length > 1
Expand All @@ -97,7 +97,7 @@ export const mergeRstestConfigLayers = async (
};

export const resolveRstestConfig = async (
layers: readonly Configs[],
layers: readonly RstackConfigDefinitions[],
params: ConfigParams,
): Promise<RstestConfig> => {
const testConfig = await mergeRstestConfigLayers(layers);
Expand Down
4 changes: 2 additions & 2 deletions packages/rstack/tests/types/resolution-bundler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,7 +21,7 @@ const loadOptions: LoadRstackConfigOptions = {
configFilePath: 'rstack.config.ts',
};
const loadedConfig: Promise<LoadedRstackConfig> = loadRstackConfig(loadOptions);
const configs: Configs = {};
const configs: RstackConfigDefinitions = {};
const sharedConfig: RstackConfig = {
app: appConfig,
lint: lintConfig,
Expand Down
4 changes: 2 additions & 2 deletions packages/rstack/tests/types/resolution-nodenext/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,7 +21,7 @@ const loadOptions: LoadRstackConfigOptions = {
configFilePath: 'rstack.config.ts',
};
const loadedConfig: Promise<LoadedRstackConfig> = loadRstackConfig(loadOptions);
const configs: Configs = {};
const configs: RstackConfigDefinitions = {};
const sharedConfig: RstackConfig = {
app: appConfig,
lint: lintConfig,
Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/guide/api-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion website/docs/zh/guide/api-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const { configs, filePath, dependencies } = await loadRstackConfig({

`loadRstackConfig()` 仅负责加载配置,不会执行工具配置函数。调用方仍需完成相应工具的配置解析和初始化,才能运行工具。

`rstack/config` 还导出相关类型:`LoadRstackConfigOptions`(参数)、`LoadedRstackConfig`(返回值)和 `Configs`(工具配置)。
`rstack/config` 还导出相关类型:`LoadRstackConfigOptions`(参数)、`LoadedRstackConfig`(返回值)和 `RstackConfigDefinitions`(工具配置)。

## TypeScript 类型 \{#typescript-types}

Expand Down
Loading