1- use rspack_core:: { Compilation , ModuleGraph , ModuleIdentifier } ;
1+ use std:: path:: Path ;
2+
3+ use rspack_core:: { BoxModule , Compilation , ModuleGraph , ModuleIdentifier , NormalModule } ;
24use rspack_util:: fx_hash:: FxHashSet as HashSet ;
35
46use super :: {
@@ -17,17 +19,25 @@ pub fn collect_assets_from_chunk(
1719 let mut css_async = HashSet :: < String > :: default ( ) ;
1820 let chunk = compilation. chunk_by_ukey . expect_get ( chunk_key) ;
1921
22+ for file in chunk. files ( ) {
23+ if file. ends_with ( ".css" ) {
24+ css_sync. insert ( file. clone ( ) ) ;
25+ } else if !is_hot_file ( file) {
26+ js_sync. insert ( file. clone ( ) ) ;
27+ }
28+ }
29+
2030 for cg in chunk. groups ( ) {
2131 let group = compilation. chunk_group_by_ukey . expect_get ( cg) ;
22- if group
23- . name ( )
24- . is_some_and ( |name| !entry_point_names . contains ( name ) )
25- {
26- for file in group . get_files ( & compilation . chunk_by_ukey ) {
27- if file . ends_with ( ".css" ) {
28- css_sync . insert ( file. to_string ( ) ) ;
29- } else if ! is_hot_file ( & file) {
30- js_sync . insert ( file ) ;
32+ if let Some ( name ) = group. name ( ) {
33+ let skip = entry_point_names . contains ( name ) ;
34+ if !skip {
35+ for file in group . get_files ( & compilation . chunk_by_ukey ) {
36+ if file . ends_with ( ".css" ) {
37+ css_sync . insert ( file . to_string ( ) ) ;
38+ } else if ! is_hot_file ( & file) {
39+ js_sync . insert ( file) ;
40+ }
3141 }
3242 }
3343 }
@@ -37,22 +47,22 @@ pub fn collect_assets_from_chunk(
3747 let async_chunk = compilation. chunk_by_ukey . expect_get ( & async_chunk_key) ;
3848 for file in async_chunk. files ( ) {
3949 if file. ends_with ( ".css" ) {
40- css_async. insert ( file. to_string ( ) ) ;
50+ css_async. insert ( file. clone ( ) ) ;
4151 } else if !is_hot_file ( file) {
42- js_async. insert ( file. to_string ( ) ) ;
52+ js_async. insert ( file. clone ( ) ) ;
4353 }
4454 }
4555 for cg in async_chunk. groups ( ) {
4656 let group = compilation. chunk_group_by_ukey . expect_get ( cg) ;
47- if group
48- . name ( )
49- . is_some_and ( |name| !entry_point_names . contains ( name ) )
50- {
51- for file in group . get_files ( & compilation . chunk_by_ukey ) {
52- if file . ends_with ( ".css" ) {
53- css_async . insert ( file. to_string ( ) ) ;
54- } else if ! is_hot_file ( & file) {
55- js_async . insert ( file ) ;
57+ if let Some ( name ) = group. name ( ) {
58+ let skip = entry_point_names . contains ( name ) ;
59+ if !skip {
60+ for file in group . get_files ( & compilation . chunk_by_ukey ) {
61+ if file . ends_with ( ".css" ) {
62+ css_async . insert ( file . to_string ( ) ) ;
63+ } else if ! is_hot_file ( & file) {
64+ js_async . insert ( file) ;
65+ }
5666 }
5767 }
5868 }
@@ -114,24 +124,6 @@ pub fn collect_assets_for_module(
114124 Some ( result)
115125}
116126
117- pub fn remove_assets ( group : & mut StatsAssetsGroup , exclude : & HashSet < String > ) {
118- group. js . sync . retain ( |asset| !exclude. contains ( asset) ) ;
119- group. js . r#async . retain ( |asset| !exclude. contains ( asset) ) ;
120- group. css . sync . retain ( |asset| !exclude. contains ( asset) ) ;
121- group. css . r#async . retain ( |asset| !exclude. contains ( asset) ) ;
122- normalize_assets_group ( group) ;
123- }
124-
125- pub fn promote_primary_assets_to_sync ( group : & mut StatsAssetsGroup ) {
126- if group. js . sync . is_empty ( ) {
127- group. js . sync . append ( & mut group. js . r#async ) ;
128- }
129- if group. css . sync . is_empty ( ) {
130- group. css . sync . append ( & mut group. css . r#async ) ;
131- }
132- normalize_assets_group ( group) ;
133- }
134-
135127pub fn collect_usage_files_for_module (
136128 compilation : & Compilation ,
137129 module_graph : & ModuleGraph ,
@@ -146,6 +138,13 @@ pub fn collect_usage_files_for_module(
146138 let Some ( origin) = origin_identifier else {
147139 continue ;
148140 } ;
141+ if let Some ( path) = module_graph
142+ . module_by_identifier ( & origin)
143+ . and_then ( |module| module_source_path ( module, compilation) )
144+ {
145+ files. insert ( path) ;
146+ continue ;
147+ }
149148 if let Some ( assets) = collect_assets_for_module ( compilation, & origin, entry_point_names) {
150149 files. extend ( assets. js . sync ) ;
151150 files. extend ( assets. js . r#async ) ;
@@ -157,3 +156,62 @@ pub fn collect_usage_files_for_module(
157156 collected. sort ( ) ;
158157 collected
159158}
159+
160+ pub fn module_source_path ( module : & BoxModule , compilation : & Compilation ) -> Option < String > {
161+ if let Some ( normal_module) = module. as_ref ( ) . as_any ( ) . downcast_ref :: < NormalModule > ( )
162+ && let Some ( path) = normal_module. resource_resolved_data ( ) . path ( )
163+ {
164+ let context_path = compilation. options . context . as_path ( ) ;
165+ let relative = Path :: new ( path. as_str ( ) )
166+ . strip_prefix ( context_path)
167+ . unwrap_or_else ( |_| Path :: new ( path. as_str ( ) ) ) ;
168+ let mut display = relative. to_string_lossy ( ) . into_owned ( ) ;
169+ if display. is_empty ( ) {
170+ display = path. as_str ( ) . to_string ( ) ;
171+ }
172+ if display. starts_with ( "./" ) {
173+ display. drain ( ..2 ) ;
174+ } else if display. starts_with ( '/' ) {
175+ display = display. trim_start_matches ( '/' ) . to_string ( ) ;
176+ }
177+ if display. is_empty ( ) {
178+ return None ;
179+ }
180+ let normalized: String = display
181+ . chars ( )
182+ . map ( |c| if c == '\\' { '/' } else { c } )
183+ . collect ( ) ;
184+ if normalized. is_empty ( ) {
185+ return None ;
186+ }
187+ return Some ( normalized) ;
188+ }
189+
190+ let mut identifier = module
191+ . readable_identifier ( & compilation. options . context )
192+ . to_string ( ) ;
193+ if identifier. is_empty ( ) {
194+ return None ;
195+ }
196+ if let Some ( pos) = identifier. rfind ( '!' ) {
197+ identifier = identifier. split_off ( pos + 1 ) ;
198+ }
199+ if let Some ( pos) = identifier. find ( '?' ) {
200+ identifier. truncate ( pos) ;
201+ }
202+ if identifier. starts_with ( "./" ) {
203+ identifier. drain ( ..2 ) ;
204+ }
205+ if identifier. is_empty ( ) {
206+ return None ;
207+ }
208+ let normalized: String = identifier
209+ . chars ( )
210+ . map ( |c| if c == '\\' { '/' } else { c } )
211+ . collect ( ) ;
212+ if normalized. is_empty ( ) {
213+ None
214+ } else {
215+ Some ( normalized)
216+ }
217+ }
0 commit comments