Skip to content

Commit ade0af9

Browse files
committed
fix: preserve rstack config state across imports
1 parent 217ae33 commit ade0af9

2 files changed

Lines changed: 34 additions & 14 deletions

File tree

packages/rstack/src/cli/commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join } from 'node:path';
22
import { parseArgs } from 'node:util';
3-
import { setConfigPath } from '../config.js';
3+
import { getConfigState } from '../config.js';
44
import { runStagedCLI } from '../staged.js';
55

66
declare global {
@@ -135,7 +135,7 @@ async function runRslintCLI(): Promise<void> {
135135

136136
export async function setupCommands(): Promise<void> {
137137
const { args, configPath } = parseCliArgs(process.argv.slice(2));
138-
setConfigPath(configPath);
138+
getConfigState().configPath = configPath;
139139
const command = args[0];
140140

141141
if (!command || command === '-h' || command === '--help') {

packages/rstack/src/config.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,28 @@ export type Configs = {
1818
staged?: StagedConfig;
1919
};
2020

21-
let configs: Configs = {};
22-
let configPath: string | undefined;
21+
type ConfigState = {
22+
configs: Configs;
23+
configPath?: string;
24+
};
25+
26+
declare global {
27+
// rslint-disable-next-line no-var
28+
var __rstackConfigState: ConfigState | undefined;
29+
}
30+
31+
export const getConfigState = (): ConfigState => {
32+
// Rsbuild's fresh import loader can load this module more than once when it
33+
// imports the internal rstack config. Keep CLI state on globalThis so the
34+
// `--config` path set by the CLI is visible to the fresh-imported instance.
35+
if (!globalThis.__rstackConfigState) {
36+
globalThis.__rstackConfigState = {
37+
configs: {},
38+
};
39+
}
40+
41+
return globalThis.__rstackConfigState;
42+
};
2343

2444
type Define = {
2545
/**
@@ -58,10 +78,12 @@ type Define = {
5878
};
5979

6080
const setConfig = <T extends keyof Configs>(type: T, config: Configs[T]): void => {
61-
if (configs[type]) {
81+
const state = getConfigState();
82+
83+
if (state.configs[type]) {
6284
throw new Error(`The "${type}" config has already been defined.`);
6385
}
64-
configs[type] = config;
86+
state.configs[type] = config;
6587
};
6688

6789
export const define: Define = {
@@ -72,16 +94,14 @@ export const define: Define = {
7294
staged: (config) => setConfig('staged', config),
7395
};
7496

75-
export const setConfigPath = (path: string | undefined): void => {
76-
configPath = path;
77-
};
78-
7997
export const loadRstackConfig = async (): Promise<Configs> => {
98+
const state = getConfigState();
99+
80100
await loadConfig({
81101
loader: 'native',
82102
exportName: false,
83-
...(configPath !== undefined
84-
? { path: configPath }
103+
...(state.configPath !== undefined
104+
? { path: state.configPath }
85105
: {
86106
configFileNames: [
87107
'rstack.config.ts',
@@ -92,7 +112,7 @@ export const loadRstackConfig = async (): Promise<Configs> => {
92112
}),
93113
});
94114

95-
const result = configs;
96-
configs = {};
115+
const result = state.configs;
116+
state.configs = {};
97117
return result;
98118
};

0 commit comments

Comments
 (0)