@@ -14,6 +14,7 @@ import {
1414  DEFAULT_CONFIG_NAME , 
1515  ENTRY_EXTENSIONS_PATTERN , 
1616  JS_EXTENSIONS_PATTERN , 
17+   RSLIB_ENTRY_QUERY , 
1718  SWC_HELPERS , 
1819}  from  './constant' ; 
1920import  { 
@@ -23,13 +24,15 @@ import {
2324  cssExternalHandler , 
2425  isCssGlobalFile , 
2526}  from  './css/cssConfig' ; 
27+ import  {  composeEntryChunkConfig  }  from  './plugins/EntryChunkPlugin' ; 
2628import  { 
2729  pluginCjsImportMetaUrlShim , 
2830  pluginEsmRequireShim , 
2931}  from  './plugins/shims' ; 
3032import  type  { 
3133  AutoExternal , 
3234  BannerAndFooter , 
35+   DeepRequired , 
3336  Format , 
3437  LibConfig , 
3538  LibOnlyConfig , 
@@ -598,7 +601,10 @@ const composeFormatConfig = ({
598601  } 
599602} ; 
600603
601- const  composeShimsConfig  =  ( format : Format ,  shims ?: Shims ) : RsbuildConfig  =>  { 
604+ const  composeShimsConfig  =  ( 
605+   format : Format , 
606+   shims ?: Shims , 
607+ ) : {  rsbuildConfig : RsbuildConfig ;  enabledShims : DeepRequired < Shims >  }  =>  { 
602608  const  resolvedShims  =  { 
603609    cjs : { 
604610      'import.meta.url' : shims ?. cjs ?. [ 'import.meta.url' ]  ??  true , 
@@ -610,9 +616,27 @@ const composeShimsConfig = (format: Format, shims?: Shims): RsbuildConfig => {
610616    } , 
611617  } ; 
612618
619+   const  enabledShims  =  { 
620+     cjs :
621+       format  ===  'cjs' 
622+         ? resolvedShims . cjs 
623+         : { 
624+             'import.meta.url' : false , 
625+           } , 
626+     esm :
627+       format  ===  'esm' 
628+         ? resolvedShims . esm 
629+         : { 
630+             __filename : false , 
631+             __dirname : false , 
632+             require : false , 
633+           } , 
634+   } ; 
635+ 
636+   let  rsbuildConfig : RsbuildConfig  =  { } ; 
613637  switch  ( format )  { 
614-     case  'esm' :
615-       return  { 
638+     case  'esm' :  { 
639+       rsbuildConfig   =  { 
616640        tools : { 
617641          rspack : { 
618642            node : { 
@@ -626,19 +650,23 @@ const composeShimsConfig = (format: Format, shims?: Shims): RsbuildConfig => {
626650          Boolean , 
627651        ) , 
628652      } ; 
653+       break ; 
654+     } 
629655    case  'cjs' :
630-       return  { 
656+       rsbuildConfig   =  { 
631657        plugins : [ 
632658          resolvedShims . cjs [ 'import.meta.url' ]  &&  pluginCjsImportMetaUrlShim ( ) , 
633659        ] . filter ( Boolean ) , 
634660      } ; 
661+       break ; 
635662    case  'umd' :
636-       return  { } ; 
637663    case  'mf' :
638-       return   { } ; 
664+       break ; 
639665    default :
640666      throw  new  Error ( `Unsupported format: ${ format }  ` ) ; 
641667  } 
668+ 
669+   return  {  rsbuildConfig,  enabledShims } ; 
642670} ; 
643671
644672export  const  composeModuleImportWarn  =  ( request : string ) : string  =>  { 
@@ -746,6 +774,16 @@ const composeSyntaxConfig = (
746774  } ; 
747775} ; 
748776
777+ const  appendEntryQuery  =  ( 
778+   entry : NonNullable < RsbuildConfig [ 'source' ] > [ 'entry' ] , 
779+ ) : NonNullable < RsbuildConfig [ 'source' ] > [ 'entry' ]  =>  { 
780+   const  newEntry : Record < string ,  string >  =  { } ; 
781+   for  ( const  key  in  entry )  { 
782+     newEntry [ key ]  =  `${ entry [ key ] }  ?${ RSLIB_ENTRY_QUERY }  ` ; 
783+   } 
784+   return  newEntry ; 
785+ } ; 
786+ 
749787const  composeEntryConfig  =  async  ( 
750788  entries : NonNullable < RsbuildConfig [ 'source' ] > [ 'entry' ] , 
751789  bundle : LibConfig [ 'bundle' ] , 
@@ -760,7 +798,7 @@ const composeEntryConfig = async (
760798    return  { 
761799      entryConfig : { 
762800        source : { 
763-           entry : entries , 
801+           entry : appendEntryQuery ( entries ) , 
764802        } , 
765803      } , 
766804      lcp : null , 
@@ -836,7 +874,7 @@ const composeEntryConfig = async (
836874  const  lcp  =  await  calcLongestCommonPath ( Object . values ( resolvedEntries ) ) ; 
837875  const  entryConfig : RsbuildConfig  =  { 
838876    source : { 
839-       entry : resolvedEntries , 
877+       entry : appendEntryQuery ( resolvedEntries ) , 
840878    } , 
841879  } ; 
842880
@@ -1000,7 +1038,7 @@ const composeTargetConfig = (
10001038const  composeExternalHelpersConfig  =  ( 
10011039  externalHelpers : boolean , 
10021040  pkgJson ?: PkgJson , 
1003- ) :  RsbuildConfig  =>  { 
1041+ )  =>  { 
10041042  let  defaultConfig  =  { 
10051043    tools : { 
10061044      swc : { 
@@ -1057,7 +1095,10 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
10571095    redirect =  { } , 
10581096    umdName, 
10591097  }  =  config ; 
1060-   const  shimsConfig  =  composeShimsConfig ( format ! ,  shims ) ; 
1098+   const  {  rsbuildConfig : shimsConfig ,  enabledShims }  =  composeShimsConfig ( 
1099+     format ! , 
1100+     shims , 
1101+   ) ; 
10611102  const  formatConfig  =  composeFormatConfig ( { 
10621103    format : format ! , 
10631104    pkgJson : pkgJson ! , 
@@ -1100,6 +1141,9 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11001141    cssModulesAuto , 
11011142  ) ; 
11021143  const  cssConfig  =  composeCssConfig ( lcp ,  config . bundle ) ; 
1144+   const  entryChunkConfig  =  composeEntryChunkConfig ( { 
1145+     enabledImportMetaUrlShim : enabledShims . cjs [ 'import.meta.url' ] , 
1146+   } ) ; 
11031147  const  dtsConfig  =  await  composeDtsConfig ( config ,  dtsExtension ) ; 
11041148  const  externalsWarnConfig  =  composeExternalsWarnConfig ( 
11051149    format ! , 
@@ -1127,6 +1171,7 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
11271171    targetConfig , 
11281172    entryConfig , 
11291173    cssConfig , 
1174+     entryChunkConfig , 
11301175    minifyConfig , 
11311176    dtsConfig , 
11321177    bannerFooterConfig , 
0 commit comments