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
26 changes: 24 additions & 2 deletions packages/rstack/src/rslibConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { WatchFiles } from '@rsbuild/core';
import type { ConfigParams, RslibConfig, RslibConfigDefinition } from '@rslib/core';
import { loadRstackConfig, type Configs } from './config.ts';

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

const loadRslibConfig = (async (params: ConfigParams) => {
const { configs } = await loadRstackConfig();
return resolveRslibConfig(configs, params);
const { configs, filePath, dependencies } = await loadRstackConfig();
const config = await resolveRslibConfig(configs, params);

if (!filePath) {
return config;
}

const watchFiles = config.dev?.watchFiles;
const watchConfig: WatchFiles = {
paths: [filePath, ...dependencies],
type: 'restart',
};

return {
...config,
dev: {
...config.dev,
watchFiles: [
...(watchFiles ? (Array.isArray(watchFiles) ? watchFiles : [watchFiles]) : []),
watchConfig,
],
},
};
}) as RslibConfigDefinition;

export default loadRslibConfig;
90 changes: 90 additions & 0 deletions packages/rstack/tests/config/reload-lib-config/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import { waitForFile } from '@rstackjs/test-utils';
import { test } from '#test-helpers';

test('should restart lib watch build when Rstack config changes', async ({
prepareDist,
execCliAsync,
logHelper,
}) => {
const dist1 = await prepareDist();
const dist2 = await prepareDist('dist-2');
const configFile = path.join(import.meta.dirname, 'test-temp-rstack.config.ts');
const userWatchFile = path.join(import.meta.dirname, 'test-temp-user-watch.txt');

const writeConfig = (distPath: string) =>
writeFile(
configFile,
`import { define } from 'rstack';

define.lib({
dev: {
watchFiles: {
paths: ${JSON.stringify(userWatchFile)},
type: 'restart',
},
},
output: {
distPath: '${distPath}',
},
});
`,
);

await writeFile(userWatchFile, 'initial\n');
await writeConfig('dist');

execCliAsync('lib --watch --config test-temp-rstack.config.ts');
await logHelper.expectLog('build completed, watching for changes...');
await waitForFile(path.join(dist1, 'index.js'));
logHelper.clearLogs();

await writeConfig('dist-2');

await logHelper.expectLog('restarting build as test-temp-rstack.config.ts changed');
await logHelper.expectLog('build completed, watching for changes...');
await waitForFile(path.join(dist2, 'index.js'));
logHelper.clearLogs();

await writeFile(userWatchFile, 'changed\n');

await logHelper.expectLog('restarting build as test-temp-user-watch.txt changed');
await logHelper.expectLog('build completed, watching for changes...');
});

test('should restart lib watch build when an imported config file changes', async ({
prepareDist,
execCliAsync,
logHelper,
}) => {
const dist1 = await prepareDist('dist-import-1');
const dist2 = await prepareDist('dist-import-2');
const configFile = path.join(import.meta.dirname, 'test-temp-import.config.ts');
const importedFile = path.join(import.meta.dirname, 'test-temp-imported.ts');

await writeFile(importedFile, "export const distPath = 'dist-import-1';\n");
await writeFile(
configFile,
`import { define } from 'rstack';
import { distPath } from './test-temp-imported.ts';

define.lib({
output: {
distPath,
},
});
`,
);

execCliAsync('lib --watch --config test-temp-import.config.ts');
await logHelper.expectLog('build completed, watching for changes...');
await waitForFile(path.join(dist1, 'index.js'));
logHelper.clearLogs();

await writeFile(importedFile, "export const distPath = 'dist-import-2';\n");

await logHelper.expectLog('restarting build as test-temp-imported.ts changed');
await logHelper.expectLog('build completed, watching for changes...');
await waitForFile(path.join(dist2, 'index.js'));
});
4 changes: 4 additions & 0 deletions packages/rstack/tests/config/reload-lib-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 'reload lib config';