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
35 changes: 27 additions & 8 deletions packages/rstack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type RslintConfigFactory = (
lint: typeof import('@rslint/core'),
) => RslintConfig | Promise<RslintConfig>;

type RslintConfigInput = RslintConfig | RslintConfigFactory;

export type Configs = {
app?: RsbuildConfigDefinition;
lib?: RslibConfigDefinition;
Expand All @@ -32,6 +34,29 @@ export type Configs = {
staged?: StagedConfig;
};

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

const normalizeLintConfig = (
config: RslintConfigInput,
): RslintConfigDefinition =>
typeof config === 'function'
? async () => config(await import('@rslint/core'))
: config;

/** Normalize one shared configuration without resolving factories or inheritance. */
export const normalizeRstackConfig = ({
extends: _extends,
lint,
...configs
}: RstackConfig): Configs =>
lint === undefined
? configs
: { ...configs, lint: normalizeLintConfig(lint) };

export type LoadedRstackConfig = {
configs: Configs;
filePath: string | null;
Expand Down Expand Up @@ -142,7 +167,7 @@ type Define = {
*
* @see {@link https://rstack.rs/config | Configuration guide}
*/
lint: (config: RslintConfig | RslintConfigFactory) => void;
lint: (config: RslintConfigInput) => void;
/**
* Defines the Prettier config for formatting.
*
Expand Down Expand Up @@ -190,13 +215,7 @@ export const define: Define = {
},
doc: (config) => setConfig('doc', config),
test: (config) => setConfig('test', config),
lint: (config) =>
setConfig(
'lint',
typeof config === 'function'
? async () => config(await import('@rslint/core'))
: config,
),
lint: (config) => setConfig('lint', normalizeLintConfig(config)),
fmt: (config) => setConfig('fmt', config),
staged: (config) => setConfig('staged', config),
};
Expand Down
46 changes: 46 additions & 0 deletions packages/rstack/tests/config/normalize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { expect, rs, test } from 'rstack/test';
import { normalizeRstackConfig, type RstackConfig } from '../../src/config.ts';
import { resolveConfigLayers } from '../../src/configLayers.ts';

test('preserves tool definitions without resolving factories or inheritance', () => {
const app = rs.fn(() => ({}));
const staged = rs.fn(() => ['rs lint']);
const shared: RstackConfig = {
extends: [{ fmt: { singleQuote: true } }],
app,
staged,
};

expect(normalizeRstackConfig(shared)).toEqual({ app, staged });
expect(shared.extends).toEqual([{ fmt: { singleQuote: true } }]);
expect(app).not.toHaveBeenCalled();
expect(staged).not.toHaveBeenCalled();
});

test('resolves sync and async lint factories lazily with tool exports', async () => {
const syncLint = rs.fn((lint: typeof import('@rslint/core')) => [
lint.js.configs.recommended,
]);
const asyncLint = rs.fn((lint: typeof import('@rslint/core')) =>
Promise.resolve([lint.ts.configs.recommended]),
);
const shared: RstackConfig[] = [
{ lint: [] },
{ lint: syncLint },
{ lint: asyncLint },
];
const configs = shared.map(normalizeRstackConfig);

expect(syncLint).not.toHaveBeenCalled();
expect(asyncLint).not.toHaveBeenCalled();
expect(shared[1].lint).toBe(syncLint);

const resolved = await resolveConfigLayers(configs, 'lint');
const { js, ts } = await import('@rslint/core');

expect(resolved).toEqual([
[],
[js.configs.recommended],
[ts.configs.recommended],
]);
});
51 changes: 51 additions & 0 deletions packages/rstack/tests/types/shared-config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { RstackConfig } from '../../../src/config.ts';

export const baseConfig: RstackConfig = {
app: { source: { entry: { index: './src/index.ts' } } },
lib: { lib: [{ format: 'esm' }] },
doc: { title: 'Docs' },
test: { retry: 2 },
lint: [],
fmt: { singleQuote: true },
staged: { '*.ts': 'rs lint' },
};

export const syncConfig: RstackConfig = {
extends: [baseConfig] as const,
app: ({ command }) => ({
source: { define: { COMMAND: JSON.stringify(command) } },
}),
lib: ({ env }) => ({
lib: [{ format: 'esm' }],
mode: env === 'production' ? 'production' : 'development',
}),
test: () => ({ retry: 1 }),
lint: ({ js, ts }) => [js.configs.recommended, ts.configs.recommended],
fmt: () => ({ singleQuote: true }),
staged: (files) => (files.length ? ['rs lint'] : []),
};

export const asyncConfig: RstackConfig = {
app: ({ env }) =>
Promise.resolve({
source: { define: { ENV: JSON.stringify(env) } },
}),
lib: () => Promise.resolve({ lib: [{ format: 'esm' }] }),
doc: () => Promise.resolve({ title: 'Docs' }),
test: () => Promise.resolve({ retry: 2 }),
lint: ({ js }) => Promise.resolve([js.configs.recommended]),
fmt: () => Promise.resolve({ singleQuote: true }),
staged: (files) => Promise.resolve(files.length ? ['rs fmt'] : []),
};

export function sharedConfig(options: { retry?: number } = {}): RstackConfig {
return {
extends: [syncConfig, asyncConfig],
test: { retry: options.retry ?? 2 },
};
}

export const invalidConfig: RstackConfig = {
// @ts-expect-error Lint factories receive tool exports, not build parameters.
lint: (_params: { env: string }) => [],
};
9 changes: 9 additions & 0 deletions packages/rstack/tests/types/shared-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"allowImportingTsExtensions": true
},
"include": ["index.ts"]
}
Loading