Skip to content

Commit c24fe64

Browse files
authored
fix(css-modules): Do not transform the container name in CSS Modules (parcel-bundler#835)
1 parent 41a07a1 commit c24fe64

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

napi/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ struct CssModulesConfig {
606606
pattern: Option<String>,
607607
dashed_idents: Option<bool>,
608608
animation: Option<bool>,
609+
container: Option<bool>,
609610
grid: Option<bool>,
610611
custom_idents: Option<bool>,
611612
pure: Option<bool>,
@@ -718,6 +719,7 @@ fn compile<'i>(
718719
},
719720
dashed_idents: c.dashed_idents.unwrap_or_default(),
720721
animation: c.animation.unwrap_or(true),
722+
container: c.container.unwrap_or(true),
721723
grid: c.grid.unwrap_or(true),
722724
custom_idents: c.custom_idents.unwrap_or(true),
723725
pure: c.pure.unwrap_or_default(),
@@ -849,6 +851,7 @@ fn compile_bundle<
849851
},
850852
dashed_idents: c.dashed_idents.unwrap_or_default(),
851853
animation: c.animation.unwrap_or(true),
854+
container: c.container.unwrap_or(true),
852855
grid: c.grid.unwrap_or(true),
853856
custom_idents: c.custom_idents.unwrap_or(true),
854857
pure: c.pure.unwrap_or_default(),

src/css_modules.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ pub struct Config<'i> {
4141
/// Whether to scope custom identifiers
4242
/// Default is `true`.
4343
pub custom_idents: bool,
44+
/// Whether to scope container names.
45+
/// Default is `true`.
46+
pub container: bool,
4447
/// Whether to check for pure CSS modules.
4548
pub pure: bool,
4649
}
@@ -52,6 +55,7 @@ impl<'i> Default for Config<'i> {
5255
dashed_idents: Default::default(),
5356
animation: true,
5457
grid: true,
58+
container: true,
5559
custom_idents: true,
5660
pure: false,
5761
}

src/lib.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24415,6 +24415,58 @@ mod tests {
2441524415
},
2441624416
);
2441724417

24418+
css_modules_test(
24419+
r#"
24420+
.box2 {
24421+
@container main (width >= 0) {
24422+
background-color: #90ee90;
24423+
}
24424+
}
24425+
"#,
24426+
indoc! {r#"
24427+
.EgL3uq_box2 {
24428+
@container EgL3uq_main (width >= 0) {
24429+
& {
24430+
background-color: #90ee90;
24431+
}
24432+
}
24433+
}
24434+
"#},
24435+
map! {
24436+
"main" => "EgL3uq_main",
24437+
"box2" => "EgL3uq_box2"
24438+
},
24439+
HashMap::new(),
24440+
crate::css_modules::Config { ..Default::default() },
24441+
);
24442+
24443+
css_modules_test(
24444+
r#"
24445+
.box2 {
24446+
@container main (width >= 0) {
24447+
background-color: #90ee90;
24448+
}
24449+
}
24450+
"#,
24451+
indoc! {r#"
24452+
.EgL3uq_box2 {
24453+
@container main (width >= 0) {
24454+
& {
24455+
background-color: #90ee90;
24456+
}
24457+
}
24458+
}
24459+
"#},
24460+
map! {
24461+
"box2" => "EgL3uq_box2"
24462+
},
24463+
HashMap::new(),
24464+
crate::css_modules::Config {
24465+
container: false,
24466+
..Default::default()
24467+
},
24468+
);
24469+
2441824470
// Stable hashes between project roots.
2441924471
fn test_project_root(project_root: &str, filename: &str, hash: &str) {
2442024472
let stylesheet = StyleSheet::parse(

src/rules/container.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,15 @@ impl<'i> ToCss for ContainerName<'i> {
268268
where
269269
W: std::fmt::Write,
270270
{
271-
self.0.to_css(dest)
271+
// Container name should not be hashed
272+
// https://github.com/vercel/next.js/issues/71233
273+
self.0.to_css_with_options(
274+
dest,
275+
match &dest.css_module {
276+
Some(css_module) => css_module.config.container,
277+
None => false,
278+
},
279+
)
272280
}
273281
}
274282

0 commit comments

Comments
 (0)