@@ -33,6 +33,7 @@ import type {
3333  AutoExternal , 
3434  BannerAndFooter , 
3535  DeepRequired , 
36+   ExcludesFalse , 
3637  Format , 
3738  LibConfig , 
3839  LibOnlyConfig , 
@@ -1182,7 +1183,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11821183export  async  function  composeCreateRsbuildConfig ( 
11831184  rslibConfig : RslibConfig , 
11841185  path ?: string , 
1185- ) : Promise < {  format : Format ;  config : RsbuildConfig  } [ ] >  { 
1186+ ) : Promise < {  id ?:  string ;   format : Format ;  config : RsbuildConfig  } [ ] >  { 
11861187  const  constantRsbuildConfig  =  await  createConstantRsbuildConfig ( ) ; 
11871188  const  configPath  =  path  ??  rslibConfig . _privateMeta ?. configFilePath ! ; 
11881189  const  {  lib : libConfigsArray ,  ...sharedRsbuildConfig  }  =  rslibConfig ; 
@@ -1217,6 +1218,7 @@ export async function composeCreateRsbuildConfig(
12171218    delete  userConfig . output . externals ; 
12181219
12191220    return  { 
1221+       id : libConfig . id , 
12201222      format : libConfig . format ! , 
12211223      // The merge order represents the priority of the configuration 
12221224      // The priorities from high to low are as follows: 
@@ -1230,6 +1232,7 @@ export async function composeCreateRsbuildConfig(
12301232        constantRsbuildConfig , 
12311233        libRsbuildConfig , 
12321234        omit < LibConfig ,  keyof  LibOnlyConfig > ( userConfig ,  { 
1235+           id : true , 
12331236          bundle : true , 
12341237          format : true , 
12351238          autoExtension : true , 
@@ -1254,30 +1257,43 @@ export async function composeCreateRsbuildConfig(
12541257export  async  function  composeRsbuildEnvironments ( 
12551258  rslibConfig : RslibConfig , 
12561259) : Promise < Record < string ,  EnvironmentConfig > >  { 
1257-   const  rsbuildConfigObject  =  await  composeCreateRsbuildConfig ( rslibConfig ) ; 
1260+   const  rsbuildConfigWithLibInfo  = 
1261+     await  composeCreateRsbuildConfig ( rslibConfig ) ; 
1262+ 
1263+   // User provided ids should take precedence over generated ids. 
1264+   const  usedIds  =  new  Set < string > ( 
1265+     rsbuildConfigWithLibInfo 
1266+       . map ( ( {  id } )  =>  id ) 
1267+       . filter ( Boolean  as  any  as  ExcludesFalse ) , 
1268+   ) ; 
12581269  const  environments : RsbuildConfig [ 'environments' ]  =  { } ; 
1259-   const  formatCount : Record < Format ,  number >  =  rsbuildConfigObject . reduce ( 
1270+   const  formatCount : Record < Format ,  number >  =  rsbuildConfigWithLibInfo . reduce ( 
12601271    ( acc ,  {  format } )  =>  { 
12611272      acc [ format ]  =  ( acc [ format ]  ??  0 )  +  1 ; 
12621273      return  acc ; 
12631274    } , 
12641275    { }  as  Record < Format ,  number > , 
12651276  ) ; 
12661277
1267-   const  formatIndex :  Record < Format ,   number >   =  { 
1268-     esm :  0 , 
1269-     cjs :  0 , 
1270-     umd :  0 , 
1271-     mf :  0 , 
1272-   } ; 
1278+   const  getDefaultId   =   ( format :  Format ) :  string   =>  { 
1279+     const   getMaybeId   =   ( )   =>   { 
1280+        let   index   =   0 ; 
1281+        while   ( usedIds . has ( ` ${ format } ${ index } ` ) )   { 
1282+          index ++ ; 
1283+        } 
12731284
1274-   for  ( const  {  format,  config }  of  rsbuildConfigObject )  { 
1275-     const  currentFormatCount  =  formatCount [ format ] ; 
1276-     const  currentFormatIndex  =  formatIndex [ format ] ++ ; 
1285+       const  finalId  = 
1286+         formatCount [ format ]  ===  1  &&  index  ===  0  ? ''  : `${ format } ${ index }  ` ; 
1287+       usedIds . add ( finalId ) ; 
1288+       return  finalId ; 
1289+     } ; 
1290+ 
1291+     return  getMaybeId ( ) ; 
1292+   } ; 
12771293
1278-      environments [ 
1279-        currentFormatCount   ===  1  ? format  : ` ${ format } ${ currentFormatIndex } ` 
1280-     ]  =  config ; 
1294+   for   ( const   {  format ,  id ,  config  }   of   rsbuildConfigWithLibInfo )   { 
1295+     const   libId   =   typeof   id   ===  'string'  ? id  : getDefaultId ( format ) ; 
1296+     environments [ libId ]  =  config ; 
12811297  } 
12821298
12831299  return  environments ; 
0 commit comments