@@ -24,7 +24,7 @@ use rspack_error::{
2424 miette:: Diagnostic , IntoTWithDiagnosticArray , Result , RspackSeverity , TWithDiagnosticArray ,
2525} ;
2626use rspack_util:: ext:: DynHash ;
27- use rustc_hash:: FxHashSet ;
27+ use rustc_hash:: { FxHashMap , FxHashSet } ;
2828
2929use crate :: {
3030 dependency:: CssSelfReferenceLocalIdentDependency ,
@@ -87,6 +87,7 @@ pub struct CssParserAndGenerator {
8787 pub es_module : bool ,
8888 #[ cacheable( with=AsOption <AsMap <AsCacheable , AsVec >>) ]
8989 pub exports : Option < CssExports > ,
90+ pub local_names : Option < FxHashMap < String , String > > ,
9091 pub hot : bool ,
9192}
9293
@@ -243,6 +244,10 @@ impl ParserAndGenerator for CssParserAndGenerator {
243244 } ,
244245 ) ;
245246 }
247+
248+ let local_names = self . local_names . get_or_insert_default ( ) ;
249+ local_names. insert ( name. into_owned ( ) , local_ident. clone ( ) ) ;
250+
246251 dependencies. push ( Box :: new ( CssLocalIdentDependency :: new (
247252 local_ident,
248253 convention_names,
@@ -315,6 +320,10 @@ impl ParserAndGenerator for CssParserAndGenerator {
315320 } ,
316321 ) ;
317322 }
323+
324+ let local_names = self . local_names . get_or_insert_default ( ) ;
325+ local_names. insert ( name. into_owned ( ) , local_ident. clone ( ) ) ;
326+
318327 dependencies. push ( Box :: new ( CssLocalIdentDependency :: new (
319328 local_ident. clone ( ) ,
320329 convention_names,
@@ -355,6 +364,7 @@ impl ParserAndGenerator for CssParserAndGenerator {
355364 vec ! [ ] ,
356365 ) ) ) ;
357366 }
367+
358368 let exports = self . exports . get_or_insert_default ( ) ;
359369 for name in names {
360370 for local_class in local_classes. iter ( ) {
@@ -494,8 +504,16 @@ impl ParserAndGenerator for CssParserAndGenerator {
494504 let mut concate_source = ConcatSource :: default ( ) ;
495505 if let Some ( ref exports) = self . exports {
496506 let mg = generate_context. compilation . get_module_graph ( ) ;
497- let unused_exports =
498- get_unused_local_ident ( exports, module. identifier ( ) , generate_context. runtime , & mg) ;
507+ let unused_exports = get_unused_local_ident (
508+ exports,
509+ self
510+ . local_names
511+ . as_ref ( )
512+ . expect ( "local names must be set when self.exports is set" ) ,
513+ module. identifier ( ) ,
514+ generate_context. runtime ,
515+ & mg,
516+ ) ;
499517 generate_context. data . insert ( unused_exports) ;
500518 let exports =
501519 get_used_exports ( exports, module. identifier ( ) , generate_context. runtime , & mg) ;
@@ -522,8 +540,16 @@ impl ParserAndGenerator for CssParserAndGenerator {
522540 ( "" , "" , "" )
523541 } ;
524542 if let Some ( exports) = & self . exports {
525- let unused_exports =
526- get_unused_local_ident ( exports, module. identifier ( ) , generate_context. runtime , & mg) ;
543+ let unused_exports = get_unused_local_ident (
544+ exports,
545+ self
546+ . local_names
547+ . as_ref ( )
548+ . expect ( "local names must be set when self.exports is set" ) ,
549+ module. identifier ( ) ,
550+ generate_context. runtime ,
551+ & mg,
552+ ) ;
527553 generate_context. data . insert ( unused_exports) ;
528554
529555 let exports =
@@ -625,6 +651,7 @@ pub struct CodeGenerationDataUnusedLocalIdent {
625651
626652fn get_unused_local_ident (
627653 exports : & CssExports ,
654+ local_names : & FxHashMap < String , String > ,
628655 identifier : ModuleIdentifier ,
629656 runtime : Option < & RuntimeSpec > ,
630657 mg : & ModuleGraph ,
@@ -641,11 +668,7 @@ fn get_unused_local_ident(
641668 false
642669 }
643670 } )
644- . flat_map ( |( _, exports) | {
645- exports
646- . iter ( )
647- . map ( |export| unescape ( & export. ident ) . into_owned ( ) )
648- } )
671+ . filter_map ( |( export_name, _) | local_names. get ( export_name) . cloned ( ) )
649672 . collect ( ) ,
650673 }
651674}
0 commit comments