8
8
} from '@rsbuild/core' ;
9
9
import { DEFAULT_CONFIG_NAME , DEFAULT_EXTENSIONS } from './constant' ;
10
10
import type {
11
+ Format ,
11
12
LibConfig ,
12
13
RslibConfig ,
13
14
RslibConfigAsyncFn ,
@@ -73,6 +74,9 @@ export async function loadConfig(
73
74
74
75
export async function createInternalRsbuildConfig ( ) : Promise < RsbuildConfig > {
75
76
return defineRsbuildConfig ( {
77
+ dev : {
78
+ progressBar : false ,
79
+ } ,
76
80
tools : {
77
81
htmlPlugin : false ,
78
82
} ,
@@ -143,17 +147,20 @@ export function convertLibConfigToRsbuildConfig(
143
147
144
148
export async function composeCreateRsbuildConfig (
145
149
rslibConfig : RslibConfig ,
146
- ) : Promise < RsbuildConfig [ ] > {
150
+ ) : Promise < Partial < Record < Format , RsbuildConfig > > > {
147
151
const internalRsbuildConfig = await createInternalRsbuildConfig ( ) ;
148
152
149
153
const { lib : libConfigsArray , ...sharedRsbuildConfig } = rslibConfig ;
150
154
151
155
if ( ! libConfigsArray ) {
152
- logger . error ( 'You must specify lib field in config file.' ) ;
153
- return [ ] ;
156
+ throw new Error (
157
+ `Expect lib field to be an array, but got ${ libConfigsArray } .` ,
158
+ ) ;
154
159
}
155
160
156
- const composedRsbuildConfig = libConfigsArray . map ( ( libConfig : LibConfig ) => {
161
+ const composedRsbuildConfig : Partial < Record < Format , RsbuildConfig > > = { } ;
162
+
163
+ for ( const libConfig of libConfigsArray ) {
157
164
const { format, ...overrideRsbuildConfig } = libConfig ;
158
165
159
166
// Merge order matters, keep `internalRsbuildConfig` at the last position
@@ -164,23 +171,21 @@ export async function composeCreateRsbuildConfig(
164
171
internalRsbuildConfig ,
165
172
) ;
166
173
167
- return convertLibConfigToRsbuildConfig ( libConfig , mergedRsbuildConfig ) ;
168
- } ) ;
174
+ composedRsbuildConfig [ format ! ] = convertLibConfigToRsbuildConfig (
175
+ libConfig ,
176
+ mergedRsbuildConfig ,
177
+ ) ;
178
+ }
169
179
170
180
return composedRsbuildConfig ;
171
181
}
172
182
173
183
export async function initRsbuild ( rslibConfig : RslibConfig ) {
174
- // TODO: use environment API instead
175
- const rsbuildConfigArray = await composeCreateRsbuildConfig ( rslibConfig ) ;
184
+ const rsbuildConfigObject = await composeCreateRsbuildConfig ( rslibConfig ) ;
176
185
177
- const rsbuildPromises = rsbuildConfigArray . map (
178
- async ( rsbuildConfig : RsbuildConfig ) => {
179
- return createRsbuild ( { rsbuildConfig } ) ;
186
+ return createRsbuild ( {
187
+ rsbuildConfig : {
188
+ environments : rsbuildConfigObject ,
180
189
} ,
181
- ) ;
182
-
183
- const rsbuildInstances = await Promise . all ( rsbuildPromises ) ;
184
-
185
- return rsbuildInstances ;
190
+ } ) ;
186
191
}
0 commit comments