@@ -5,14 +5,57 @@ use std::{
5
5
6
6
use derive_more:: Debug ;
7
7
use futures:: future:: BoxFuture ;
8
- use rspack_collections:: IdentifierMap ;
8
+ use rspack_collections:: { IdentifierMap , UkeySet } ;
9
9
use rspack_core:: { ChunkUkey , Compilation , Module , SourceType } ;
10
10
use rspack_error:: Result ;
11
11
use rspack_regex:: RspackRegex ;
12
12
use rustc_hash:: { FxHashMap , FxHashSet } ;
13
13
14
- pub type ChunkFilter =
14
+ pub type ChunkFilterFunc =
15
15
Arc < dyn Fn ( & ChunkUkey , & Compilation ) -> BoxFuture < ' static , Result < bool > > + Sync + Send > ;
16
+
17
+ #[ derive( Clone ) ]
18
+ pub enum ChunkFilter {
19
+ Func ( ChunkFilterFunc ) ,
20
+ All ,
21
+ Regex ( RspackRegex ) ,
22
+ Async ,
23
+ Initial ,
24
+ }
25
+
26
+ impl ChunkFilter {
27
+ pub fn is_func ( & self ) -> bool {
28
+ matches ! ( self , ChunkFilter :: Func ( _) )
29
+ }
30
+
31
+ pub async fn test_func ( & self , chunk_ukey : & ChunkUkey , compilation : & Compilation ) -> Result < bool > {
32
+ if let ChunkFilter :: Func ( func) = self {
33
+ func ( chunk_ukey, compilation) . await
34
+ } else {
35
+ panic ! ( "ChunkFilter is not a function" ) ;
36
+ }
37
+ }
38
+
39
+ pub fn test_internal ( & self , chunk_ukey : & ChunkUkey , compilation : & Compilation ) -> bool {
40
+ match self {
41
+ ChunkFilter :: Func ( _) => panic ! ( "ChunkFilter is a function" ) ,
42
+ ChunkFilter :: All => true ,
43
+ ChunkFilter :: Regex ( re) => {
44
+ let chunk = compilation. chunk_by_ukey . expect_get ( chunk_ukey) ;
45
+ chunk. name ( ) . is_some_and ( |name| re. test ( name) )
46
+ }
47
+ ChunkFilter :: Async => {
48
+ let chunk = compilation. chunk_by_ukey . expect_get ( chunk_ukey) ;
49
+ !chunk. can_be_initial ( & compilation. chunk_group_by_ukey )
50
+ }
51
+ ChunkFilter :: Initial => {
52
+ let chunk = compilation. chunk_by_ukey . expect_get ( chunk_ukey) ;
53
+ chunk. can_be_initial ( & compilation. chunk_group_by_ukey )
54
+ }
55
+ }
56
+ }
57
+ }
58
+
16
59
pub type ModuleTypeFilter = Arc < dyn Fn ( & dyn Module ) -> bool + Send + Sync > ;
17
60
pub type ModuleLayerFilter =
18
61
Arc < dyn Fn ( Option < String > ) -> BoxFuture < ' static , Result < bool > > + Send + Sync > ;
@@ -26,23 +69,15 @@ pub fn create_default_module_layer_filter() -> ModuleLayerFilter {
26
69
}
27
70
28
71
pub fn create_async_chunk_filter ( ) -> ChunkFilter {
29
- Arc :: new ( |chunk_ukey, compilation| {
30
- let chunk = compilation. chunk_by_ukey . expect_get ( chunk_ukey) ;
31
- let can_be_initial = chunk. can_be_initial ( & compilation. chunk_group_by_ukey ) ;
32
- Box :: pin ( async move { Ok ( !can_be_initial) } )
33
- } )
72
+ ChunkFilter :: Async
34
73
}
35
74
36
75
pub fn create_initial_chunk_filter ( ) -> ChunkFilter {
37
- Arc :: new ( |chunk_ukey, compilation| {
38
- let chunk = compilation. chunk_by_ukey . expect_get ( chunk_ukey) ;
39
- let can_be_initial = chunk. can_be_initial ( & compilation. chunk_group_by_ukey ) ;
40
- Box :: pin ( async move { Ok ( can_be_initial) } )
41
- } )
76
+ ChunkFilter :: Initial
42
77
}
43
78
44
79
pub fn create_all_chunk_filter ( ) -> ChunkFilter {
45
- Arc :: new ( |_chunk , _compilation| Box :: pin ( async move { Ok ( true ) } ) )
80
+ ChunkFilter :: All
46
81
}
47
82
48
83
pub fn create_chunk_filter_from_str ( chunks : & str ) -> ChunkFilter {
@@ -55,11 +90,7 @@ pub fn create_chunk_filter_from_str(chunks: &str) -> ChunkFilter {
55
90
}
56
91
57
92
pub fn create_regex_chunk_filter_from_str ( re : RspackRegex ) -> ChunkFilter {
58
- Arc :: new ( move |chunk_ukey, compilation| {
59
- let chunk = compilation. chunk_by_ukey . expect_get ( chunk_ukey) ;
60
- let res = chunk. name ( ) . is_some_and ( |name| re. test ( name) ) ;
61
- Box :: pin ( async move { Ok ( res) } )
62
- } )
93
+ ChunkFilter :: Regex ( re)
63
94
}
64
95
65
96
#[ derive( Debug , Default , Clone ) ]
@@ -171,3 +202,4 @@ pub struct FallbackCacheGroup {
171
202
}
172
203
173
204
pub ( crate ) type ModuleSizes = IdentifierMap < FxHashMap < SourceType , f64 > > ;
205
+ pub ( crate ) type ModuleChunks = IdentifierMap < UkeySet < ChunkUkey > > ;
0 commit comments