@@ -488,7 +488,80 @@ impl Chunk {
488488 }
489489
490490 pub fn has_async_chunks ( & self , chunk_group_by_ukey : & ChunkGroupByUkey ) -> bool {
491- !self . get_all_async_chunks ( chunk_group_by_ukey) . is_empty ( )
491+ // The reason why we don't just return !self.get_all_async_chunks(chunk_group_by_ukey).is_empty() here is
492+ // get_all_async_chunks will check the whether the chunk is inside Entrypoint, which cause the chunk not return
493+ // as a async chunk, but has_async_chunks is used to check whether need to add the chunk loading runtime, which
494+ // is about loading the async chunks, so even the chunk is inside Entrypoint but loading it indeed need the
495+ // chunk loading runtime.
496+ // For a real case checkout the test: `rspack-test-tools/configCases/chunk-loading/depend-on-with-chunk-load`
497+ let mut queue = UkeyIndexSet :: default ( ) ;
498+
499+ let initial_chunks = self
500+ . groups
501+ . iter ( )
502+ . map ( |chunk_group| chunk_group_by_ukey. expect_get ( chunk_group) )
503+ . map ( |group| group. chunks . iter ( ) . copied ( ) . collect :: < UkeySet < _ > > ( ) )
504+ . reduce ( |acc, prev| acc. intersection ( & prev) . copied ( ) . collect :: < UkeySet < _ > > ( ) )
505+ . unwrap_or_default ( ) ;
506+
507+ let mut visit_chunk_groups = UkeySet :: default ( ) ;
508+
509+ for chunk_group_ukey in self . get_sorted_groups_iter ( chunk_group_by_ukey) {
510+ if let Some ( chunk_group) = chunk_group_by_ukey. get ( chunk_group_ukey) {
511+ for child_ukey in chunk_group
512+ . children
513+ . iter ( )
514+ . sorted_by ( |a, b| sort_group_by_index ( a, b, chunk_group_by_ukey) )
515+ {
516+ if let Some ( chunk_group) = chunk_group_by_ukey. get ( child_ukey) {
517+ queue. insert ( chunk_group. ukey ) ;
518+ }
519+ }
520+ }
521+ }
522+
523+ fn check_chunks (
524+ chunk_group_by_ukey : & ChunkGroupByUkey ,
525+ initial_chunks : & UkeySet < ChunkUkey > ,
526+ chunk_group_ukey : & ChunkGroupUkey ,
527+ visit_chunk_groups : & mut UkeySet < ChunkGroupUkey > ,
528+ ) -> bool {
529+ let Some ( chunk_group) = chunk_group_by_ukey. get ( chunk_group_ukey) else {
530+ return false ;
531+ } ;
532+ for chunk_ukey in chunk_group. chunks . iter ( ) {
533+ if !initial_chunks. contains ( chunk_ukey) {
534+ return true ;
535+ }
536+ }
537+ for group_ukey in chunk_group. children . iter ( ) {
538+ if !visit_chunk_groups. contains ( group_ukey) {
539+ visit_chunk_groups. insert ( * group_ukey) ;
540+ if check_chunks (
541+ chunk_group_by_ukey,
542+ initial_chunks,
543+ group_ukey,
544+ visit_chunk_groups,
545+ ) {
546+ return true ;
547+ }
548+ }
549+ }
550+ false
551+ }
552+
553+ for group_ukey in queue. iter ( ) {
554+ if check_chunks (
555+ chunk_group_by_ukey,
556+ & initial_chunks,
557+ group_ukey,
558+ & mut visit_chunk_groups,
559+ ) {
560+ return true ;
561+ }
562+ }
563+
564+ false
492565 }
493566
494567 pub fn get_all_async_chunks (
0 commit comments