Skip to content

Commit 690c744

Browse files
authored
refactor(config): compose effective tool definitions from config layers (#542)
1 parent fbc35ad commit 690c744

5 files changed

Lines changed: 180 additions & 10 deletions

File tree

‎packages/rstack/src/config.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type { UserConfig, UserConfigAsyncFn } from '@rspress/core';
1313
import type { RstestConfigExport } from '@rstest/core';
1414
import type { FmtConfigDefinition } from './fmt/types.ts';
1515
import type { StagedConfig } from './staged.ts';
16+
import { composeConfigLayers } from './configLayers.ts';
1617

1718
export type RslintConfigDefinition =
1819
RslintConfig | (() => Promise<RslintConfig>);
@@ -58,6 +59,7 @@ export const normalizeRstackConfig = ({
5859
: { ...configs, lint: normalizeLintConfig(lint) };
5960

6061
export type LoadedRstackConfig = {
62+
/** Effective tool definitions. Configuration factories remain unevaluated. */
6163
configs: Configs;
6264
filePath: string | null;
6365
dependencies: string[];
@@ -251,7 +253,7 @@ export const loadRstackConfig = async ({
251253
});
252254

253255
return {
254-
configs: session.configs,
256+
configs: await composeConfigLayers([session.configs]),
255257
filePath,
256258
dependencies,
257259
};

‎packages/rstack/src/configLayers.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,79 @@ export const resolveStagedConfig = async (
112112
: { ...merged, ...config },
113113
);
114114
};
115+
116+
export const resolveFmtConfigLayers = async (
117+
layers: readonly Configs[],
118+
): Promise<FmtConfig | undefined> => {
119+
const configs = await resolveConfigLayers(layers, 'fmt');
120+
return configs.length > 1
121+
? (Object.assign({}, ...configs) as FmtConfig)
122+
: configs[0];
123+
};
124+
125+
/** Compose effective definitions without executing any tool or task factories. */
126+
export const composeConfigLayers = async (
127+
layers: readonly Configs[],
128+
): Promise<Configs> => {
129+
if (layers.length <= 1) {
130+
return layers[0] ?? {};
131+
}
132+
133+
const configs: Configs = {};
134+
for (const kind of [
135+
'app',
136+
'lib',
137+
'doc',
138+
'test',
139+
'lint',
140+
'fmt',
141+
'staged',
142+
] as const) {
143+
const matchingLayers = layers.filter((layer) => layer[kind] !== undefined);
144+
if (matchingLayers.length === 0) {
145+
continue;
146+
}
147+
if (matchingLayers.length === 1) {
148+
Object.assign(configs, { [kind]: matchingLayers[0][kind] });
149+
continue;
150+
}
151+
152+
switch (kind) {
153+
case 'app':
154+
configs.app = async (params) => {
155+
const { resolveRsbuildConfig } = await import('./rsbuildConfig.ts');
156+
return resolveRsbuildConfig(matchingLayers, params);
157+
};
158+
break;
159+
case 'lib':
160+
configs.lib = async (params) => {
161+
const { resolveRslibConfig } = await import('./rslibConfig.ts');
162+
return resolveRslibConfig(matchingLayers, params);
163+
};
164+
break;
165+
case 'doc':
166+
configs.doc = async () => {
167+
const { resolveRspressConfig } = await import('./rspressConfig.ts');
168+
return resolveRspressConfig(matchingLayers);
169+
};
170+
break;
171+
case 'test':
172+
configs.test = async () => {
173+
const { mergeRstestConfigLayers } = await import('./rstestConfig.ts');
174+
return mergeRstestConfigLayers(matchingLayers);
175+
};
176+
break;
177+
case 'lint':
178+
configs.lint = async () => (await resolveRslintConfig(matchingLayers))!;
179+
break;
180+
case 'fmt':
181+
configs.fmt = async () =>
182+
(await resolveFmtConfigLayers(matchingLayers))!;
183+
break;
184+
case 'staged':
185+
configs.staged = await resolveStagedConfig(matchingLayers);
186+
break;
187+
}
188+
}
189+
return configs;
190+
};

‎packages/rstack/src/fmt/config.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { dirname } from 'node:path';
22
import micromatch from 'micromatch';
33
import type { Configs } from '../config.ts';
4-
import { resolveConfigLayers } from '../configLayers.ts';
4+
import { resolveFmtConfigLayers } from '../configLayers.ts';
55
import { createRelativePathResolver } from './pathHelpers.ts';
66
import type {
77
FmtConfig,
@@ -157,11 +157,7 @@ const resolveFmtConfig = async ({
157157
configFilePath,
158158
cwd,
159159
}: ResolveFmtConfigOptions): Promise<ResolvedFmtConfig> => {
160-
const configs = await resolveConfigLayers(layers, 'fmt');
161-
const config =
162-
configs.length > 1
163-
? (Object.assign({}, ...configs) as FmtConfig)
164-
: configs[0];
160+
const config = await resolveFmtConfigLayers(layers);
165161
const rootPath = configFilePath ? dirname(configFilePath) : cwd;
166162

167163
return normalizeFmtConfig(config, rootPath);

‎packages/rstack/src/rstestConfig.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,20 @@ const extendsConfig = async (
8787
};
8888
};
8989

90+
export const mergeRstestConfigLayers = async (
91+
layers: readonly Configs[],
92+
): Promise<RstestConfig> => {
93+
const configs = await resolveConfigLayers(layers, 'test');
94+
return configs.length > 1
95+
? mergeRstestConfig(...configs)
96+
: (configs[0] ?? {});
97+
};
98+
9099
export const resolveRstestConfig = async (
91100
layers: readonly Configs[],
92101
params: ConfigParams,
93102
): Promise<RstestConfig> => {
94-
const configs = await resolveConfigLayers(layers, 'test');
95-
const testConfig =
96-
configs.length > 1 ? mergeRstestConfig(...configs) : (configs[0] ?? {});
103+
const testConfig = await mergeRstestConfigLayers(layers);
97104
return extendsConfig(layers, testConfig, params);
98105
};
99106

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import assert from 'node:assert/strict';
2+
import { createRequire } from 'node:module';
3+
import { expect, rs, test } from 'rstack/test';
4+
import {
5+
composeConfigLayers,
6+
resolveConfigLayers,
7+
} from '../../src/configLayers.ts';
8+
import { resolveFmtConfig } from '../../src/fmt/config.ts';
9+
import { resolveRstestConfig } from '../../src/rstestConfig.ts';
10+
11+
// TODO: Import the source directly after upgrading Rstest to fix
12+
// https://github.com/web-infra-dev/rstest/issues/1891.
13+
rs.mock(
14+
'../../src/rstestConfig.ts',
15+
() =>
16+
createRequire(import.meta.url)(
17+
'../../dist/rstestConfig.js',
18+
) as typeof import('../../src/rstestConfig.ts'),
19+
);
20+
21+
test('preserves single definitions and leaves missing tools absent', async () => {
22+
const app = rs.fn(() => ({}));
23+
const base = { app };
24+
const fmt = {};
25+
26+
expect(await composeConfigLayers([])).toEqual({});
27+
expect(await composeConfigLayers([base])).toBe(base);
28+
const configs = await composeConfigLayers([base, { fmt }]);
29+
expect(configs).toEqual({ app, fmt });
30+
expect(configs.fmt).toBe(fmt);
31+
expect(app).not.toHaveBeenCalled();
32+
});
33+
34+
test('resolves only the requested tool and preserves staged task generators', async () => {
35+
const app = rs.fn(() => ({}));
36+
const fmt = rs.fn(() => ({ semi: false }));
37+
const staged = rs.fn(() => 'rs check');
38+
const configs = await composeConfigLayers([
39+
{ app, fmt, staged: { '*.ts': 'rs lint' } },
40+
{ app: {}, fmt: { singleQuote: true }, staged },
41+
]);
42+
43+
expect(fmt).not.toHaveBeenCalled();
44+
expect(
45+
await resolveFmtConfig({
46+
layers: [configs],
47+
configFilePath: '/project/rstack.config.ts',
48+
cwd: '/other',
49+
}),
50+
).toEqual({
51+
rootPath: '/project',
52+
baseOptions: { semi: false, singleQuote: true },
53+
overrides: [],
54+
ignorePatterns: [],
55+
});
56+
expect(fmt).toHaveBeenCalledExactlyOnceWith();
57+
expect(app).not.toHaveBeenCalled();
58+
expect(configs.staged).toBe(staged);
59+
expect(staged).not.toHaveBeenCalled();
60+
});
61+
62+
test('applies automatic inheritance after merging test definitions', async () => {
63+
const app = rs.fn(() => ({ source: { define: { SHARED: true } } }));
64+
const configs = await composeConfigLayers([
65+
{ app, test: { retry: 1 } },
66+
{
67+
app: { source: { define: { PROJECT: true } } },
68+
test: { testTimeout: 5000 },
69+
},
70+
]);
71+
72+
expect(await resolveConfigLayers([configs], 'test')).toEqual([
73+
{ retry: 1, testTimeout: 5000 },
74+
]);
75+
expect(app).not.toHaveBeenCalled();
76+
77+
const params = { command: 'build', env: 'production' } as const;
78+
const config = await resolveRstestConfig([configs], params);
79+
assert(typeof config.extends === 'function');
80+
expect(config).toEqual({
81+
retry: 1,
82+
testTimeout: 5000,
83+
extends: config.extends,
84+
});
85+
expect(await config.extends(config)).toMatchObject({
86+
source: { define: { SHARED: true, PROJECT: true } },
87+
});
88+
expect(app).toHaveBeenCalledExactlyOnceWith(params);
89+
});

0 commit comments

Comments
 (0)