Skip to content

Commit 70f1c63

Browse files
committed
fix(doc): restart dev server on config changes
1 parent eea2805 commit 70f1c63

2 files changed

Lines changed: 106 additions & 3 deletions

File tree

packages/rstack/src/rspressConfig.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { WatchFiles } from '@rsbuild/core';
12
import type { UserConfig } from '@rspress/core';
23
import { loadRstackConfig, type Configs } from './config.ts';
34

@@ -13,6 +14,30 @@ const resolveRspressConfig = async (configs: Configs): Promise<UserConfig> => {
1314
};
1415

1516
export default async (): Promise<UserConfig> => {
16-
const { configs } = await loadRstackConfig();
17-
return resolveRspressConfig(configs);
17+
const { configs, filePath, dependencies } = await loadRstackConfig();
18+
const config = await resolveRspressConfig(configs);
19+
20+
if (!filePath) {
21+
return config;
22+
}
23+
24+
const watchFiles = config.builderConfig?.dev?.watchFiles;
25+
const watchConfig: WatchFiles = {
26+
paths: [filePath, ...dependencies],
27+
type: 'restart',
28+
};
29+
30+
return {
31+
...config,
32+
builderConfig: {
33+
...config.builderConfig,
34+
dev: {
35+
...config.builderConfig?.dev,
36+
watchFiles: [
37+
...(watchFiles ? (Array.isArray(watchFiles) ? watchFiles : [watchFiles]) : []),
38+
watchConfig,
39+
],
40+
},
41+
},
42+
};
1843
};

packages/rstack/tests/config/define-doc/index.test.ts

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { getDistFiles, getFileContent } from '@rstackjs/test-utils';
1+
import { writeFile } from 'node:fs/promises';
2+
import path from 'node:path';
3+
import { getDistFiles, getFileContent, getRandomPort } from '@rstackjs/test-utils';
24
import { test } from '#test-helpers';
35

46
const expectedText = 'define.doc works';
@@ -13,3 +15,79 @@ test('should build docs with define.doc config', async ({ prepareDist, execCli,
1315

1416
expect(output).toContain(expectedText);
1517
}, 30_000);
18+
19+
test('should restart doc dev server when Rstack config changes', async ({
20+
execCliAsync,
21+
logHelper,
22+
}) => {
23+
const configFile = path.join(import.meta.dirname, 'test-temp-rstack.config.ts');
24+
const userWatchFile = path.join(import.meta.dirname, 'test-temp-user-watch.txt');
25+
26+
const writeConfig = (title: string) =>
27+
writeFile(
28+
configFile,
29+
`import { define } from 'rstack';
30+
31+
define.doc({
32+
root: 'docs',
33+
title: '${title}',
34+
builderConfig: {
35+
dev: {
36+
watchFiles: {
37+
paths: ${JSON.stringify(userWatchFile)},
38+
type: 'restart',
39+
},
40+
},
41+
},
42+
});
43+
`,
44+
);
45+
46+
await writeFile(userWatchFile, 'initial\n');
47+
await writeConfig('before config change');
48+
49+
execCliAsync(`doc --config test-temp-rstack.config.ts --port ${await getRandomPort()}`);
50+
await logHelper.expectBuildEnd();
51+
logHelper.clearLogs();
52+
53+
await writeConfig('after config change');
54+
55+
await logHelper.expectLog('restarting server as test-temp-rstack.config.ts changed');
56+
await logHelper.expectBuildEnd();
57+
logHelper.clearLogs();
58+
59+
await writeFile(userWatchFile, 'changed\n');
60+
61+
await logHelper.expectLog('restarting server as test-temp-user-watch.txt changed');
62+
await logHelper.expectBuildEnd();
63+
}, 30_000);
64+
65+
test('should restart doc dev server when an imported config file changes', async ({
66+
execCliAsync,
67+
logHelper,
68+
}) => {
69+
const configFile = path.join(import.meta.dirname, 'test-temp-import.config.ts');
70+
const importedFile = path.join(import.meta.dirname, 'test-temp-imported.ts');
71+
72+
await writeFile(importedFile, "export const title = 'before import change';\n");
73+
await writeFile(
74+
configFile,
75+
`import { define } from 'rstack';
76+
import { title } from './test-temp-imported.ts';
77+
78+
define.doc({
79+
root: 'docs',
80+
title,
81+
});
82+
`,
83+
);
84+
85+
execCliAsync(`doc --config test-temp-import.config.ts --port ${await getRandomPort()}`);
86+
await logHelper.expectBuildEnd();
87+
logHelper.clearLogs();
88+
89+
await writeFile(importedFile, "export const title = 'after import change';\n");
90+
91+
await logHelper.expectLog('restarting server as test-temp-imported.ts changed');
92+
await logHelper.expectBuildEnd();
93+
}, 30_000);

0 commit comments

Comments
 (0)