1- import { createRsbuild , mergeRsbuildConfig } from '@rsbuild/core' ;
2- import type { RsbuildConfig , RsbuildInstance } from '@rsbuild/core' ;
3- import { composeCreateRsbuildConfig } from '../config' ;
1+ import { createRsbuild } from '@rsbuild/core' ;
2+ import type { RsbuildInstance } from '@rsbuild/core' ;
3+ import { composeRsbuildEnvironments , pruneEnvironments } from '../config' ;
44import type { RslibConfig } from '../types' ;
5+ import type { CommonOptions } from './commands' ;
56import { onBeforeRestart } from './restart' ;
67
78export async function startMFDevServer (
89 config : RslibConfig ,
10+ options : Pick < CommonOptions , 'lib' > = { } ,
911) : Promise < RsbuildInstance | undefined > {
10- const rsbuildInstance = await initMFRsbuild ( config ) ;
12+ const rsbuildInstance = await initMFRsbuild ( config , options ) ;
1113 return rsbuildInstance ;
1214}
1315
1416async function initMFRsbuild (
15- rslibConfig : RslibConfig ,
17+ config : RslibConfig ,
18+ options : Pick < CommonOptions , 'lib' > = { } ,
1619) : Promise < RsbuildInstance | undefined > {
17- const rsbuildConfigObject = await composeCreateRsbuildConfig ( rslibConfig ) ;
18- const mfRsbuildConfig = rsbuildConfigObject . find (
19- ( config ) => config . format === 'mf' ,
20- ) ;
20+ const { environments, environmentWithInfos } =
21+ await composeRsbuildEnvironments ( config ) ;
2122
22- if ( ! mfRsbuildConfig ) {
23- // no mf format, return.
24- return ;
23+ const selectedEnvironmentIds = environmentWithInfos
24+ . filter ( ( env ) => {
25+ const isMf = env . format === 'mf' ;
26+ if ( ! options ?. lib ) {
27+ return isMf ;
28+ }
29+ return env . id && options . lib . includes ( env . id ) ;
30+ } )
31+ . map ( ( env ) => env . id ) ;
32+
33+ if ( ! selectedEnvironmentIds . length ) {
34+ throw new Error ( 'No mf format found, please check your config.' ) ;
2535 }
2636
27- mfRsbuildConfig . config = changeEnvToDev ( mfRsbuildConfig . config ) ;
37+ const selectedEnvironments = pruneEnvironments (
38+ environments ,
39+ selectedEnvironmentIds ,
40+ ) ;
2841
2942 const rsbuildInstance = await createRsbuild ( {
3043 rsbuildConfig : {
31- ...mfRsbuildConfig . config ,
32- plugins : [
33- ...( rslibConfig . plugins || [ ] ) ,
34- ...( mfRsbuildConfig . config . plugins || [ ] ) ,
35- ] ,
36- server : mergeRsbuildConfig (
37- rslibConfig . server ,
38- mfRsbuildConfig . config . server ,
39- ) ,
44+ mode : 'development' ,
45+ root : config . root ,
46+ plugins : config . plugins ,
47+ dev : {
48+ ...( config . dev ?? { } ) ,
49+ writeToDisk : true ,
50+ } ,
51+ server : config . server ,
52+ tools : {
53+ rspack : {
54+ optimization : {
55+ nodeEnv : 'development' ,
56+ moduleIds : 'named' ,
57+ } ,
58+ } ,
59+ } ,
60+ environments : selectedEnvironments ,
4061 } ,
4162 } ) ;
4263
@@ -45,20 +66,3 @@ async function initMFRsbuild(
4566 onBeforeRestart ( devServer . server . close ) ;
4667 return rsbuildInstance ;
4768}
48-
49- function changeEnvToDev ( rsbuildConfig : RsbuildConfig ) {
50- return mergeRsbuildConfig ( rsbuildConfig , {
51- mode : 'development' ,
52- dev : {
53- writeToDisk : true ,
54- } ,
55- tools : {
56- rspack : {
57- optimization : {
58- nodeEnv : 'development' ,
59- moduleIds : 'named' ,
60- } ,
61- } ,
62- } ,
63- } ) ;
64- }
0 commit comments