Skip to content

Commit 9172d8b

Browse files
committed
fix(types): preserve config factory contextual typing
1 parent 7475a74 commit 9172d8b

3 files changed

Lines changed: 80 additions & 6 deletions

File tree

‎packages/rstack/src/config.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { AsyncLocalStorage } from 'node:async_hooks';
22
import { loadConfig } from '@rstackjs/load-config';
3-
import type { RsbuildConfigDefinition } from '@rsbuild/core';
4-
import type { RslibConfigDefinition } from '@rslib/core';
3+
import type {
4+
defineConfig as defineAppConfig,
5+
RsbuildConfigDefinition,
6+
} from '@rsbuild/core';
7+
import type {
8+
defineConfig as defineRslibConfig,
9+
RslibConfigDefinition,
10+
} from '@rslib/core';
511
import type { RslintConfig } from '@rslint/core';
612
import type { UserConfig, UserConfigAsyncFn } from '@rspress/core';
713
import type { RstestConfigExport } from '@rstest/core';
@@ -99,15 +105,15 @@ type Define = {
99105
*
100106
* @see {@link https://rstack.rs/config | Configuration guide}
101107
*/
102-
app: (config: RsbuildConfigDefinition) => void;
108+
app: typeof defineAppConfig;
103109
/**
104110
* Defines the Rslib config for libraries.
105111
*
106112
* This config is used by the `rs lib` command.
107113
*
108114
* @see {@link https://rstack.rs/config | Configuration guide}
109115
*/
110-
lib: (config: RslibConfigDefinition) => void;
116+
lib: typeof defineRslibConfig;
111117
/**
112118
* Defines the Rspress config for documentation.
113119
*
@@ -174,8 +180,14 @@ const setConfig = <T extends keyof Configs>(
174180
};
175181

176182
export const define: Define = {
177-
app: (config) => setConfig('app', config),
178-
lib: (config) => setConfig('lib', config),
183+
app: ((config) => {
184+
setConfig('app', config);
185+
return config;
186+
}) as typeof defineAppConfig,
187+
lib: <Config extends RslibConfigDefinition>(config: Config) => {
188+
setConfig('lib', config);
189+
return config;
190+
},
179191
doc: (config) => setConfig('doc', config),
180192
test: (config) => setConfig('test', config),
181193
lint: (config) =>

‎packages/rstack/tests/types/resolution-bundler/index.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,43 @@ void configs;
2828

2929
void createRsbuild({ config: appConfig });
3030
define.app(appConfig);
31+
32+
// App config factories should accept `html.meta` entries with different keys.
33+
define.app(() => ({
34+
html: {
35+
meta: [
36+
{
37+
viewport: {
38+
width: 'device-width',
39+
},
40+
},
41+
{ description: 'Rstack' },
42+
],
43+
},
44+
}));
45+
3146
define.lib(libConfig);
47+
48+
// Lib config factories should accept multiple entries with different names.
49+
define.lib(() => ({
50+
lib: [
51+
{
52+
format: 'esm',
53+
source: { entry: { index: './src/index.ts' } },
54+
},
55+
{
56+
format: 'esm',
57+
source: { entry: { worker: './src/worker.ts' } },
58+
},
59+
],
60+
}));
61+
3262
define.lint(lintConfig);
3363
define.lint(({ js, ts }) => [
3464
js.configs.recommended,
3565
ts.configs.recommendedTypeChecked,
3666
]);
67+
3768
define.doc({});
3869
define.test({});
3970
define.staged({});

‎packages/rstack/tests/types/resolution-nodenext/index.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,43 @@ void configs;
2828

2929
void createRsbuild({ config: appConfig });
3030
define.app(appConfig);
31+
32+
// App config factories should accept `html.meta` entries with different keys.
33+
define.app(() => ({
34+
html: {
35+
meta: [
36+
{
37+
viewport: {
38+
width: 'device-width',
39+
},
40+
},
41+
{ description: 'Rstack' },
42+
],
43+
},
44+
}));
45+
3146
define.lib(libConfig);
47+
48+
// Lib config factories should accept multiple entries with different names.
49+
define.lib(() => ({
50+
lib: [
51+
{
52+
format: 'esm',
53+
source: { entry: { index: './src/index.ts' } },
54+
},
55+
{
56+
format: 'esm',
57+
source: { entry: { worker: './src/worker.ts' } },
58+
},
59+
],
60+
}));
61+
3262
define.lint(lintConfig);
3363
define.lint(({ js, ts }) => [
3464
js.configs.recommended,
3565
ts.configs.recommendedTypeChecked,
3666
]);
67+
3768
define.doc({});
3869
define.test({});
3970
define.staged({});

0 commit comments

Comments
 (0)