Skip to content

Commit 39c5335

Browse files
authored
fix: reset config state after loading (#25)
1 parent 67571ee commit 39c5335

3 files changed

Lines changed: 41 additions & 17 deletions

File tree

packages/rstack/src/config.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,26 @@ export const define: Define = {
103103

104104
export const loadRstackConfig = async (): Promise<Configs> => {
105105
const state = getConfigState();
106+
state.configs = {};
106107

107-
await loadConfig({
108-
loader: 'native',
109-
exportName: false,
110-
...(state.configPath !== undefined
111-
? { path: state.configPath }
112-
: {
113-
configFileNames: [
114-
'rstack.config.ts',
115-
'rstack.config.js',
116-
'rstack.config.mts',
117-
'rstack.config.mjs',
118-
],
119-
}),
120-
});
108+
try {
109+
await loadConfig({
110+
loader: 'native',
111+
exportName: false,
112+
...(state.configPath !== undefined
113+
? { path: state.configPath }
114+
: {
115+
configFileNames: [
116+
'rstack.config.ts',
117+
'rstack.config.js',
118+
'rstack.config.mts',
119+
'rstack.config.mjs',
120+
],
121+
}),
122+
});
121123

122-
const result = state.configs;
123-
state.configs = {};
124-
return result;
124+
return state.configs;
125+
} finally {
126+
state.configs = {};
127+
}
125128
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import path from 'node:path';
2+
import { expect, test } from 'rstack/test';
3+
import { getConfigState, loadRstackConfig } from '../../../src/config.ts';
4+
5+
test('should reset config state before and after loading', async () => {
6+
const state = getConfigState();
7+
state.configs = { app: {} };
8+
state.configPath = path.join(import.meta.dirname, 'rstack.config.ts');
9+
10+
try {
11+
await expect(loadRstackConfig()).rejects.toThrow('test config error');
12+
expect(state.configs).toEqual({});
13+
} finally {
14+
state.configs = {};
15+
delete state.configPath;
16+
}
17+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { define } from 'rstack';
2+
3+
define.app({});
4+
throw new Error('test config error');

0 commit comments

Comments
 (0)