diff --git a/crates/swc_css_modules/src/lib.rs b/crates/swc_css_modules/src/lib.rs index 39160b642fe..f2ffcf85a20 100644 --- a/crates/swc_css_modules/src/lib.rs +++ b/crates/swc_css_modules/src/lib.rs @@ -84,6 +84,8 @@ struct Data { renamed_to_orig: FxHashMap, orig_to_renamed: FxHashMap, + + is_global_mode: bool, } impl VisitMut for Compiler @@ -258,6 +260,8 @@ where let mut new_children = Vec::with_capacity(n.children.len()); + let old_is_global_mode = self.data.is_global_mode; + self.data.is_global_mode = false; 'complex: for mut n in n.children.take() { match &mut n { ComplexSelectorChildren::CompoundSelector(sel) => { @@ -266,13 +270,15 @@ where for sel in &mut sel.subclass_selectors { match sel { SubclassSelector::Class(..) | SubclassSelector::Id(..) => { - process_local( - &mut self.config, - &mut self.result, - &mut self.data.orig_to_renamed, - &mut self.data.renamed_to_orig, - sel, - ); + if !self.data.is_global_mode { + process_local( + &mut self.config, + &mut self.result, + &mut self.data.orig_to_renamed, + &mut self.data.renamed_to_orig, + sel, + ); + } } SubclassSelector::PseudoClass(class_sel) => { match &*class_sel.name.value { @@ -292,7 +298,9 @@ where sel.visit_mut_with(self); new_children.extend(sel.children); - + continue 'complex; + } else { + self.data.is_global_mode = false; continue 'complex; } } @@ -311,6 +319,9 @@ where new_children.extend(sel.children); + continue 'complex; + } else { + self.data.is_global_mode = true; continue 'complex; } } @@ -331,6 +342,7 @@ where } n.children = new_children; + self.data.is_global_mode = old_is_global_mode; } fn visit_mut_complex_selectors(&mut self, n: &mut Vec) { diff --git a/crates/swc_css_modules/tests/fixture/modules/tests-cases/leak-scope/source.compiled.css b/crates/swc_css_modules/tests/fixture/modules/tests-cases/leak-scope/source.compiled.css index 2c1d10a75e0..82739b369c1 100644 --- a/crates/swc_css_modules/tests/fixture/modules/tests-cases/leak-scope/source.compiled.css +++ b/crates/swc_css_modules/tests/fixture/modules/tests-cases/leak-scope/source.compiled.css @@ -33,7 +33,7 @@ left: 20px; } } -:global .__local__d1 { + .d1 { animation: __local__d1; animation: __local__d2, __local__d3, __local__d4; } diff --git a/crates/swc_css_modules/tests/fixture/modules/tests-cases/local/source.compiled.css b/crates/swc_css_modules/tests/fixture/modules/tests-cases/local/source.compiled.css index 03c61ecb98f..cd9b3df3c53 100644 --- a/crates/swc_css_modules/tests/fixture/modules/tests-cases/local/source.compiled.css +++ b/crates/swc_css_modules/tests/fixture/modules/tests-cases/local/source.compiled.css @@ -1,6 +1,6 @@ .__local__abc .__local__def { color: red; } -:local .__local__ghi .__local__jkl { + .__local__ghi .__local__jkl { color: blue; } diff --git a/crates/swc_css_modules/tests/fixture/modules/tests-cases/mode-switching/source.compiled.css b/crates/swc_css_modules/tests/fixture/modules/tests-cases/mode-switching/source.compiled.css index 638c582b95b..a395f7ce4db 100644 --- a/crates/swc_css_modules/tests/fixture/modules/tests-cases/mode-switching/source.compiled.css +++ b/crates/swc_css_modules/tests/fixture/modules/tests-cases/mode-switching/source.compiled.css @@ -1,5 +1,5 @@ -.__local__c1 :local .__local__c2 .__local__c3 :global .__local__c4 :local .__local__c5, -.__local__c6 :local .__local__c7 { +.__local__c1 .__local__c2 .__local__c3 .c4 .__local__c5, +.__local__c6 .__local__c7 { background: red; } .__local__c8 { diff --git a/crates/swc_css_modules/tests/fixture/modules/tests-cases/mode-switching/source.transform.json b/crates/swc_css_modules/tests/fixture/modules/tests-cases/mode-switching/source.transform.json index 7750066cfda..27d2aa433e6 100644 --- a/crates/swc_css_modules/tests/fixture/modules/tests-cases/mode-switching/source.transform.json +++ b/crates/swc_css_modules/tests/fixture/modules/tests-cases/mode-switching/source.transform.json @@ -1,10 +1,4 @@ { - "c4": [ - { - "type": "local", - "name": "__local__c4" - } - ], "c5": [ { "type": "local", @@ -23,6 +17,12 @@ "name": "__local__c7" } ], + "c8": [ + { + "type": "local", + "name": "__local__c8" + } + ], "c1": [ { "type": "local", @@ -40,11 +40,5 @@ "type": "local", "name": "__local__c3" } - ], - "c8": [ - { - "type": "local", - "name": "__local__c8" - } ] }