@@ -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
2444type Define = {
2545 /**
@@ -58,10 +78,12 @@ type Define = {
5878} ;
5979
6080const 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
6789export 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-
7997export 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