Skip to content

Commit ae32c79

Browse files
authored
fix(doc): restart dev server on config changes (#358)
1 parent 05c2d21 commit ae32c79

6 files changed

Lines changed: 112 additions & 5 deletions

File tree

packages/rstack/rstack.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ define.test(async () => {
1111
// Temporary projects may contain files that match Rstest's test glob.
1212
exclude: ['**/test-temp-*/**'],
1313
extends: withRslibConfig(),
14+
testTimeout: 30_000,
1415
source: {
1516
tsconfigPath: './tests/tsconfig.json',
1617
},

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ test('should build docs with define.doc config', async ({ prepareDist, execCli,
1212
const output = getFileContent(files, 'index.html');
1313

1414
expect(output).toContain(expectedText);
15-
}, 30_000);
15+
});

packages/rstack/tests/config/reload-app-config/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ define.app({
4545
);
4646

4747
await waitForFile(dist2);
48-
}, 30_000);
48+
});
4949

5050
test('should reload config when an imported file changes', async ({ execCliAsync, logHelper }) => {
5151
const configFile = path.join(import.meta.dirname, 'test-temp-import.config.ts');
@@ -70,4 +70,4 @@ define.app({
7070
await writeFile(importedFile, '// changed\n');
7171

7272
await logHelper.expectLog('restarting server as test-temp-imported.ts changed');
73-
}, 30_000);
73+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Reload doc config
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { writeFile } from 'node:fs/promises';
2+
import path from 'node:path';
3+
import { getRandomPort } from '@rstackjs/test-utils';
4+
import { test } from '#test-helpers';
5+
6+
test('should restart doc dev server when Rstack config changes', async ({
7+
execCliAsync,
8+
logHelper,
9+
}) => {
10+
const configFile = path.join(import.meta.dirname, 'test-temp-rstack.config.ts');
11+
const userWatchFile = path.join(import.meta.dirname, 'test-temp-user-watch.txt');
12+
13+
const writeConfig = (title: string) =>
14+
writeFile(
15+
configFile,
16+
`import { define } from 'rstack';
17+
18+
define.doc({
19+
root: 'docs',
20+
title: '${title}',
21+
builderConfig: {
22+
dev: {
23+
watchFiles: {
24+
paths: ${JSON.stringify(userWatchFile)},
25+
type: 'restart',
26+
},
27+
},
28+
},
29+
});
30+
`,
31+
);
32+
33+
await writeFile(userWatchFile, 'initial\n');
34+
await writeConfig('before config change');
35+
36+
execCliAsync(`doc --config test-temp-rstack.config.ts --port ${await getRandomPort()}`);
37+
await logHelper.expectBuildEnd();
38+
logHelper.clearLogs();
39+
40+
await writeConfig('after config change');
41+
42+
await logHelper.expectLog('restarting server as test-temp-rstack.config.ts changed');
43+
await logHelper.expectBuildEnd();
44+
logHelper.clearLogs();
45+
46+
await writeFile(userWatchFile, 'changed\n');
47+
48+
await logHelper.expectLog('restarting server as test-temp-user-watch.txt changed');
49+
await logHelper.expectBuildEnd();
50+
});
51+
52+
test('should restart doc dev server when an imported config file changes', async ({
53+
execCliAsync,
54+
logHelper,
55+
}) => {
56+
const configFile = path.join(import.meta.dirname, 'test-temp-import.config.ts');
57+
const importedFile = path.join(import.meta.dirname, 'test-temp-imported.ts');
58+
59+
await writeFile(importedFile, "export const title = 'before import change';\n");
60+
await writeFile(
61+
configFile,
62+
`import { define } from 'rstack';
63+
import { title } from './test-temp-imported.ts';
64+
65+
define.doc({
66+
root: 'docs',
67+
title,
68+
});
69+
`,
70+
);
71+
72+
execCliAsync(`doc --config test-temp-import.config.ts --port ${await getRandomPort()}`);
73+
await logHelper.expectBuildEnd();
74+
logHelper.clearLogs();
75+
76+
await writeFile(importedFile, "export const title = 'after import change';\n");
77+
78+
await logHelper.expectLog('restarting server as test-temp-imported.ts changed');
79+
await logHelper.expectBuildEnd();
80+
});

0 commit comments

Comments
 (0)