Skip to content

Commit 78972cb

Browse files
authored
fix(lib): restart on config changes (#360)
1 parent 9f6f61b commit 78972cb

4 files changed

Lines changed: 119 additions & 2 deletions

File tree

‎packages/rstack/src/rslibConfig.ts‎

Lines changed: 24 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 { ConfigParams, RslibConfig, RslibConfigDefinition } from '@rslib/core';
23
import { loadRstackConfig, type Configs } from './config.ts';
34

@@ -13,8 +14,29 @@ const resolveRslibConfig = async (configs: Configs, params: ConfigParams): Promi
1314
};
1415

1516
const loadRslibConfig = (async (params: ConfigParams) => {
16-
const { configs } = await loadRstackConfig();
17-
return resolveRslibConfig(configs, params);
17+
const { configs, filePath, dependencies } = await loadRstackConfig();
18+
const config = await resolveRslibConfig(configs, params);
19+
20+
if (!filePath) {
21+
return config;
22+
}
23+
24+
const watchFiles = config.dev?.watchFiles;
25+
const watchConfig: WatchFiles = {
26+
paths: [filePath, ...dependencies],
27+
type: 'restart',
28+
};
29+
30+
return {
31+
...config,
32+
dev: {
33+
...config.dev,
34+
watchFiles: [
35+
...(watchFiles ? (Array.isArray(watchFiles) ? watchFiles : [watchFiles]) : []),
36+
watchConfig,
37+
],
38+
},
39+
};
1840
}) as RslibConfigDefinition;
1941

2042
export default loadRslibConfig;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { writeFile } from 'node:fs/promises';
2+
import path from 'node:path';
3+
import { waitForFile } from '@rstackjs/test-utils';
4+
import { test } from '#test-helpers';
5+
6+
test('should restart lib watch build when Rstack config changes', async ({
7+
prepareDist,
8+
execCliAsync,
9+
logHelper,
10+
}) => {
11+
const dist1 = await prepareDist();
12+
const dist2 = await prepareDist('dist-2');
13+
const configFile = path.join(import.meta.dirname, 'test-temp-rstack.config.ts');
14+
const userWatchFile = path.join(import.meta.dirname, 'test-temp-user-watch.txt');
15+
16+
const writeConfig = (distPath: string) =>
17+
writeFile(
18+
configFile,
19+
`import { define } from 'rstack';
20+
21+
define.lib({
22+
dev: {
23+
watchFiles: {
24+
paths: ${JSON.stringify(userWatchFile)},
25+
type: 'restart',
26+
},
27+
},
28+
output: {
29+
distPath: '${distPath}',
30+
},
31+
});
32+
`,
33+
);
34+
35+
await writeFile(userWatchFile, 'initial\n');
36+
await writeConfig('dist');
37+
38+
execCliAsync('lib --watch --config test-temp-rstack.config.ts');
39+
await logHelper.expectLog('build completed, watching for changes...');
40+
await waitForFile(path.join(dist1, 'index.js'));
41+
logHelper.clearLogs();
42+
43+
await writeConfig('dist-2');
44+
45+
await logHelper.expectLog('restarting build as test-temp-rstack.config.ts changed');
46+
await logHelper.expectLog('build completed, watching for changes...');
47+
await waitForFile(path.join(dist2, 'index.js'));
48+
logHelper.clearLogs();
49+
50+
await writeFile(userWatchFile, 'changed\n');
51+
52+
await logHelper.expectLog('restarting build as test-temp-user-watch.txt changed');
53+
await logHelper.expectLog('build completed, watching for changes...');
54+
});
55+
56+
test('should restart lib watch build when an imported config file changes', async ({
57+
prepareDist,
58+
execCliAsync,
59+
logHelper,
60+
}) => {
61+
const dist1 = await prepareDist('dist-import-1');
62+
const dist2 = await prepareDist('dist-import-2');
63+
const configFile = path.join(import.meta.dirname, 'test-temp-import.config.ts');
64+
const importedFile = path.join(import.meta.dirname, 'test-temp-imported.ts');
65+
66+
await writeFile(importedFile, "export const distPath = 'dist-import-1';\n");
67+
await writeFile(
68+
configFile,
69+
`import { define } from 'rstack';
70+
import { distPath } from './test-temp-imported.ts';
71+
72+
define.lib({
73+
output: {
74+
distPath,
75+
},
76+
});
77+
`,
78+
);
79+
80+
execCliAsync('lib --watch --config test-temp-import.config.ts');
81+
await logHelper.expectLog('build completed, watching for changes...');
82+
await waitForFile(path.join(dist1, 'index.js'));
83+
logHelper.clearLogs();
84+
85+
await writeFile(importedFile, "export const distPath = 'dist-import-2';\n");
86+
87+
await logHelper.expectLog('restarting build as test-temp-imported.ts changed');
88+
await logHelper.expectLog('build completed, watching for changes...');
89+
await waitForFile(path.join(dist2, 'index.js'));
90+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"private": true,
3+
"type": "module"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const value = 'reload lib config';

0 commit comments

Comments
 (0)