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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Dist
node_modules
dist/
dist-*/
test-results
doc_build

Expand Down
16 changes: 12 additions & 4 deletions packages/rstack/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { loadConfig } from '@rstackjs/load-config';
import { loadConfig, type LoadConfigResult } from '@rstackjs/load-config';
import type { RsbuildConfigDefinition } from '@rsbuild/core';
import type { RslibConfigDefinition } from '@rslib/core';
import type { RslintConfig } from '@rslint/core';
Expand All @@ -18,6 +18,10 @@ export type Configs = {
staged?: StagedConfig;
};

type LoadedRstackConfig = Pick<LoadConfigResult, 'filePath' | 'dependencies'> & {
configs: Configs;
};

type ConfigState = {
configs: Configs;
configPath?: string;
Expand Down Expand Up @@ -101,12 +105,12 @@ export const define: Define = {
staged: (config) => setConfig('staged', config),
};

export const loadRstackConfig = async (): Promise<Configs> => {
export const loadRstackConfig = async (): Promise<LoadedRstackConfig> => {
const state = getConfigState();
state.configs = {};

try {
await loadConfig({
const { filePath, dependencies } = await loadConfig({
loader: 'native',
exportName: false,
fresh: true,
Expand All @@ -122,7 +126,11 @@ export const loadRstackConfig = async (): Promise<Configs> => {
}),
});

return state.configs;
return {
configs: state.configs,
filePath,
dependencies,
};
} finally {
state.configs = {};
}
Expand Down
19 changes: 16 additions & 3 deletions packages/rstack/src/rsbuildConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RsbuildConfigDefinition, ConfigParams } from '@rsbuild/core';
import { mergeRsbuildConfig, type RsbuildConfigDefinition, type ConfigParams } from '@rsbuild/core';
import { loadRstackConfig, type Configs } from './config.js';

const resolveRsbuildConfig = async (configs: Configs, params: ConfigParams) => {
Expand All @@ -13,8 +13,21 @@ const resolveRsbuildConfig = async (configs: Configs, params: ConfigParams) => {
};

const loadRsbuildConfig: RsbuildConfigDefinition = async (params) => {
const configs = await loadRstackConfig();
return resolveRsbuildConfig(configs, params);
const { configs, filePath, dependencies } = await loadRstackConfig();
const config = await resolveRsbuildConfig(configs, params);

if (!filePath) {
return config;
}

return mergeRsbuildConfig(config, {
dev: {
watchFiles: {
Comment thread
chenjiahan marked this conversation as resolved.
paths: [filePath, ...dependencies],
Comment thread
chenjiahan marked this conversation as resolved.
type: 'reload-server',
},
},
});
};

export default loadRsbuildConfig;
2 changes: 1 addition & 1 deletion packages/rstack/src/rslibConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const resolveRslibConfig = async (configs: Configs, params: ConfigParams): Promi
};

const loadRslibConfig = (async (params: ConfigParams) => {
const configs = await loadRstackConfig();
const { configs } = await loadRstackConfig();
return resolveRslibConfig(configs, params);
}) as RslibConfigDefinition;

Expand Down
2 changes: 1 addition & 1 deletion packages/rstack/src/rslintConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { loadRstackConfig } from './config.js';
import type { RslintConfig } from '@rslint/core';

const configs = await loadRstackConfig();
const { configs } = await loadRstackConfig();
const lintExports = configs.lint ?? [];

let lintConfig: RslintConfig;
Expand Down
2 changes: 1 addition & 1 deletion packages/rstack/src/rspressConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ const resolveRspressConfig = async (configs: Configs): Promise<UserConfig> => {
};

export default async (): Promise<UserConfig> => {
const configs = await loadRstackConfig();
const { configs } = await loadRstackConfig();
return resolveRspressConfig(configs);
};
2 changes: 1 addition & 1 deletion packages/rstack/src/rstestConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const resolveRstestConfig = async (configs: Configs) => {
};

const loadRstestConfig = (async (params: ConfigParams) => {
const configs = await loadRstackConfig();
const { configs } = await loadRstackConfig();
const testConfig = await resolveRstestConfig(configs);
return extendsConfig(configs, testConfig, params);
}) as RstestConfigExport;
Expand Down
2 changes: 1 addition & 1 deletion packages/rstack/src/staged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export async function runStagedCLI(args: string[]): Promise<void> {
return;
}

const configs = await loadRstackConfig();
const { configs } = await loadRstackConfig();
const stagedConfig = configs.staged;
if (!stagedConfig) {
throw new Error(
Expand Down
50 changes: 50 additions & 0 deletions packages/rstack/test/config/reload-app-config/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { rm, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { expectFile, getRandomPort, test } from '#test-helpers';

test('should restart dev server and reload config when Rstack config changes', async ({
execCliAsync,
}) => {
const dist1 = path.join(import.meta.dirname, 'dist');
const dist2 = path.join(import.meta.dirname, 'dist-2');
const configFile = path.join(import.meta.dirname, 'test-temp-rstack.config.ts');

await rm(dist1, { recursive: true, force: true });
await rm(dist2, { recursive: true, force: true });
await rm(configFile, { force: true });

await writeFile(
configFile,
`import { define } from 'rstack';

define.app({
dev: {
writeToDisk: true,
},
server: { port: ${await getRandomPort()} },
});
`,
);

execCliAsync('dev --config test-temp-rstack.config.ts');

await expectFile(dist1);

await writeFile(
configFile,
`import { define } from 'rstack';

define.app({
dev: {
writeToDisk: true,
},
output: {
distPath: 'dist-2',
},
server: { port: ${await getRandomPort()} },
});
`,
);

await expectFile(dist2);
}, 30_000);
1 change: 1 addition & 0 deletions packages/rstack/test/config/reload-app-config/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hello!');