@@ -303,63 +303,45 @@ export async function createDevServer<
303303 ( _stats , entryName , utils ) => getTransformedHtml ( entryName , utils ) ,
304304 ) ;
305305
306- const environmentAPI = Object . fromEntries (
307- Object . entries ( context . environments ) . map ( ( [ name , environment ] ) => {
308- return [
309- name ,
310- {
311- getStats : async ( ) => {
312- if ( ! buildManager ) {
313- throw new Error (
314- `${ color . dim ( '[rsbuild:server]' ) } Can not call ` +
315- `${ color . yellow ( 'getStats' ) } when ` +
316- `${ color . yellow ( 'runCompile' ) } is false` ,
317- ) ;
318- }
319- await waitLastCompileDone ;
320- return lastStats [ environment . index ] ;
321- } ,
322- context : environment ,
323- loadBundle : async < T > ( entryName : string ) => {
324- if ( ! buildManager ) {
325- throw new Error (
326- `${ color . dim ( '[rsbuild:server]' ) } Can not call ` +
327- `${ color . yellow ( 'loadBundle' ) } when ` +
328- `${ color . yellow ( 'runCompile' ) } is false` ,
329- ) ;
330- }
331- await waitLastCompileDone ;
332- return cacheableLoadBundle (
333- lastStats [ environment . index ] ,
334- entryName ,
335- {
336- readFileSync : buildManager . readFileSync ,
337- environment,
338- } ,
339- ) as T ;
340- } ,
341- getTransformedHtml : async ( entryName : string ) => {
342- if ( ! buildManager ) {
343- throw new Error (
344- `${ color . dim ( '[rsbuild:server]' ) } Can not call ` +
345- `${ color . yellow ( 'getTransformedHtml' ) } when ` +
346- `${ color . yellow ( 'runCompile' ) } is false` ,
347- ) ;
348- }
349- await waitLastCompileDone ;
350- return cacheableTransformedHtml (
351- lastStats [ environment . index ] ,
352- entryName ,
353- {
354- readFileSync : buildManager . readFileSync ,
355- environment,
356- } ,
357- ) ;
358- } ,
359- } ,
360- ] ;
361- } ) ,
362- ) ;
306+ const environmentAPI : EnvironmentAPI = { } ;
307+
308+ const getErrorMsg = ( method : string ) =>
309+ `${ color . dim ( '[rsbuild:server]' ) } Can not call ` +
310+ `${ color . yellow ( method ) } when ` +
311+ `${ color . yellow ( 'runCompile' ) } is false` ;
312+
313+ context . environmentList . forEach ( ( environment , index ) => {
314+ environmentAPI [ environment . name ] = {
315+ context : environment ,
316+ getStats : async ( ) => {
317+ if ( ! buildManager ) {
318+ throw new Error ( getErrorMsg ( 'getStats' ) ) ;
319+ }
320+ await waitLastCompileDone ;
321+ return lastStats [ index ] ;
322+ } ,
323+ loadBundle : async < T > ( entryName : string ) => {
324+ if ( ! buildManager ) {
325+ throw new Error ( getErrorMsg ( 'loadBundle' ) ) ;
326+ }
327+ await waitLastCompileDone ;
328+ return cacheableLoadBundle ( lastStats [ index ] , entryName , {
329+ readFileSync : buildManager . readFileSync ,
330+ environment,
331+ } ) as T ;
332+ } ,
333+ getTransformedHtml : async ( entryName : string ) => {
334+ if ( ! buildManager ) {
335+ throw new Error ( getErrorMsg ( 'getTransformedHtml' ) ) ;
336+ }
337+ await waitLastCompileDone ;
338+ return cacheableTransformedHtml ( lastStats [ index ] , entryName , {
339+ readFileSync : buildManager . readFileSync ,
340+ environment,
341+ } ) ;
342+ } ,
343+ } ;
344+ } ) ;
363345
364346 const connect = requireCompiledPackage ( 'connect' ) ;
365347 const middlewares = connect ( ) ;
0 commit comments