@@ -918,76 +918,90 @@ const composeEntryConfig = async (
918918 } ;
919919 }
920920
921- // In bundleless mode, resolve glob patterns and convert them to entry object.
922- const resolvedEntries : Record < string , string > = { } ;
923- for ( const key of Object . keys ( entries ) ) {
924- const entry = entries [ key ] ;
925-
926- // Entries in bundleless mode could be:
927- // 1. A string of glob pattern: { entry: { index: 'src/*.ts' } }
928- // 2. An array of glob patterns: { entry: { index: ['src/*.ts', 'src/*.tsx'] } }
929- // Not supported for now: entry description object
930- const entryFiles = Array . isArray ( entry )
931- ? entry
932- : typeof entry === 'string'
933- ? [ entry ]
934- : null ;
935-
936- if ( ! entryFiles ) {
937- throw new Error (
938- 'Entry can only be a string or an array of strings for now' ,
939- ) ;
940- }
921+ const scanGlobEntries = async ( calcLcp : boolean ) => {
922+ // In bundleless mode, resolve glob patterns and convert them to entry object.
923+ const resolvedEntries : Record < string , string > = { } ;
924+ for ( const key of Object . keys ( entries ) ) {
925+ const entry = entries [ key ] ;
926+
927+ // Entries in bundleless mode could be:
928+ // 1. A string of glob pattern: { entry: { index: 'src/*.ts' } }
929+ // 2. An array of glob patterns: { entry: { index: ['src/*.ts', 'src/*.tsx'] } }
930+ // Not supported for now: entry description object
931+ const entryFiles = Array . isArray ( entry )
932+ ? entry
933+ : typeof entry === 'string'
934+ ? [ entry ]
935+ : null ;
941936
942- // Turn entries in array into each separate entry.
943- const globEntryFiles = await glob ( entryFiles , {
944- cwd : root ,
945- absolute : true ,
946- } ) ;
937+ if ( ! entryFiles ) {
938+ throw new Error (
939+ 'Entry can only be a string or an array of strings for now' ,
940+ ) ;
941+ }
947942
948- // Filter the glob resolved entry files based on the allowed extensions
949- const resolvedEntryFiles = globEntryFiles . filter ( ( file ) =>
950- ENTRY_EXTENSIONS_PATTERN . test ( file ) ,
951- ) ;
943+ // Turn entries in array into each separate entry.
944+ const globEntryFiles = await glob ( entryFiles , {
945+ cwd : root ,
946+ absolute : true ,
947+ } ) ;
952948
953- if ( resolvedEntryFiles . length === 0 ) {
954- throw new Error ( `Cannot find ${ resolvedEntryFiles } ` ) ;
955- }
949+ // Filter the glob resolved entry files based on the allowed extensions
950+ const resolvedEntryFiles = globEntryFiles . filter ( ( file ) =>
951+ ENTRY_EXTENSIONS_PATTERN . test ( file ) ,
952+ ) ;
953+
954+ if ( resolvedEntryFiles . length === 0 ) {
955+ throw new Error ( `Cannot find ${ resolvedEntryFiles } ` ) ;
956+ }
957+
958+ // Similar to `rootDir` in tsconfig and `outbase` in esbuild.
959+ const lcp = await calcLongestCommonPath ( resolvedEntryFiles ) ;
960+ // Using the longest common path of all non-declaration input files by default.
961+ const outBase = lcp === null ? root : lcp ;
956962
957- // Similar to `rootDir` in tsconfig and `outbase` in esbuild.
958- const lcp = await calcLongestCommonPath ( resolvedEntryFiles ) ;
959- // Using the longest common path of all non-declaration input files by default .
960- const outBase = lcp === null ? root : lcp ;
963+ function getEntryName ( file : string ) {
964+ const { dir , name } = path . parse ( path . relative ( outBase , file ) ) ;
965+ // Entry filename contains nested path to preserve source directory structure .
966+ const entryFileName = path . join ( dir , name ) ;
961967
962- function getEntryName ( file : string ) {
963- const { dir, name } = path . parse ( path . relative ( outBase , file ) ) ;
964- // Entry filename contains nested path to preserve source directory structure.
965- const entryFileName = path . join ( dir , name ) ;
968+ // 1. we mark the global css files (which will generate empty js chunk in cssExtract), and deleteAsset in RemoveCssExtractAssetPlugin
969+ // 2. avoid the same name e.g: `index.ts` and `index.css`
970+ if ( isCssGlobalFile ( file , cssModulesAuto ) ) {
971+ return `${ RSLIB_CSS_ENTRY_FLAG } /${ entryFileName } ` ;
972+ }
966973
967- // 1. we mark the global css files (which will generate empty js chunk in cssExtract), and deleteAsset in RemoveCssExtractAssetPlugin
968- // 2. avoid the same name e.g: `index.ts` and `index.css`
969- if ( isCssGlobalFile ( file , cssModulesAuto ) ) {
970- return `${ RSLIB_CSS_ENTRY_FLAG } /${ entryFileName } ` ;
974+ return entryFileName ;
971975 }
972976
973- return entryFileName ;
977+ for ( const file of resolvedEntryFiles ) {
978+ const entryName = getEntryName ( file ) ;
979+ if ( resolvedEntries [ entryName ] ) {
980+ logger . warn (
981+ `duplicate entry: ${ entryName } , this may lead to the incorrect output, please rename the file` ,
982+ ) ;
983+ }
984+ resolvedEntries [ entryName ] = file ;
985+ }
974986 }
975987
976- for ( const file of resolvedEntryFiles ) {
977- const entryName = getEntryName ( file ) ;
978- if ( resolvedEntries [ entryName ] ) {
979- logger . warn (
980- `duplicate entry: ${ entryName } , this may lead to the incorrect output, please rename the file` ,
981- ) ;
982- }
983- resolvedEntries [ entryName ] = file ;
988+ if ( calcLcp ) {
989+ const lcp = await calcLongestCommonPath ( Object . values ( resolvedEntries ) ) ;
990+ return { resolvedEntries, lcp } ;
984991 }
985- }
992+ return { resolvedEntries, lcp : null } ;
993+ } ;
986994
987- const lcp = await calcLongestCommonPath ( Object . values ( resolvedEntries ) ) ;
995+ // LCP could only be determined at the first time of glob scan.
996+ const { lcp } = await scanGlobEntries ( true ) ;
988997 const entryConfig : EnvironmentConfig = {
989- source : {
990- entry : appendEntryQuery ( resolvedEntries ) ,
998+ tools : {
999+ rspack : {
1000+ entry : async ( ) => {
1001+ const { resolvedEntries } = await scanGlobEntries ( false ) ;
1002+ return appendEntryQuery ( resolvedEntries ) ;
1003+ } ,
1004+ } ,
9911005 } ,
9921006 } ;
9931007
@@ -1342,6 +1356,7 @@ async function composeLibRsbuildConfig(
13421356
13431357 const entryChunkConfig = composeEntryChunkConfig ( {
13441358 enabledImportMetaUrlShim : enabledShims . cjs [ 'import.meta.url' ] ,
1359+ contextToWatch : lcp ,
13451360 } ) ;
13461361 const dtsConfig = await composeDtsConfig ( config , dtsExtension ) ;
13471362 const externalsWarnConfig = composeExternalsWarnConfig (
0 commit comments