Skip to content

Commit

Permalink
feat(css/minifier): Handle zero in at-rules (#6155)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 15, 2022
1 parent 16df442 commit 0f38e38
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 30 deletions.
8 changes: 8 additions & 0 deletions crates/swc_css_minifier/src/compressor/container.rs
Expand Up @@ -59,4 +59,12 @@ impl Compressor {
_ => {}
}
}

pub(super) fn compress_size_feature_value_length(&mut self, n: &mut SizeFeatureValue) {
if let SizeFeatureValue::Dimension(dimension) = n {
if let Some(number) = self.length_to_zero(dimension) {
*n = SizeFeatureValue::Number(number)
}
}
}
}
37 changes: 20 additions & 17 deletions crates/swc_css_minifier/src/compressor/length.rs
Expand Up @@ -98,31 +98,34 @@ impl Compressor {
}

impl Compressor {
pub(super) fn compress_component_value_for_length(
&mut self,
component_value: &mut ComponentValue,
) {
if self.ctx.in_math_function {
return;
}

match &component_value {
ComponentValue::Dimension(Dimension::Length(Length {
pub(super) fn length_to_zero(&mut self, n: &mut Dimension) -> Option<Number> {
match &n {
Dimension::Length(Length {
value:
Number {
value: number_value,
..
},
span,
..
})) if *number_value == 0.0 => {
*component_value = ComponentValue::Number(Number {
span: *span,
value: 0.0,
raw: None,
});
}) if *number_value == 0.0 => Some(Number {
span: *span,
value: 0.0,
raw: None,
}),
_ => None,
}
}

pub(super) fn compress_component_value_for_length(&mut self, n: &mut ComponentValue) {
if self.ctx.in_math_function {
return;
}

if let ComponentValue::Dimension(dimension) = n {
if let Some(number) = self.length_to_zero(dimension) {
*n = ComponentValue::Number(number)
}
_ => {}
}
}

Expand Down
8 changes: 8 additions & 0 deletions crates/swc_css_minifier/src/compressor/media.rs
Expand Up @@ -385,4 +385,12 @@ impl Compressor {
_ => {}
}
}

pub(super) fn compress_media_feature_value_length(&mut self, n: &mut MediaFeatureValue) {
if let MediaFeatureValue::Dimension(dimension) = n {
if let Some(number) = self.length_to_zero(dimension) {
*n = MediaFeatureValue::Number(number)
}
}
}
}
2 changes: 2 additions & 0 deletions crates/swc_css_minifier/src/compressor/mod.rs
Expand Up @@ -173,6 +173,7 @@ impl VisitMut for Compressor {
n.visit_mut_children_with(self);

self.compress_calc_sum_in_media_feature_value(n);
self.compress_media_feature_value_length(n);
}

fn visit_mut_supports_condition(&mut self, n: &mut SupportsCondition) {
Expand All @@ -197,6 +198,7 @@ impl VisitMut for Compressor {
n.visit_mut_children_with(self);

self.compress_calc_sum_in_size_feature_value(n);
self.compress_size_feature_value_length(n);
}

fn visit_mut_keyframe_selector(&mut self, n: &mut KeyframeSelector) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,2 @@
@import url("narrow.css") supports(display: flex) screen and (max-width: calc(0px));
@import url("narrow.css") supports(display: flex) screen and (max-width: calc(100px * 0));
@@ -0,0 +1 @@
@import"narrow.css"supports(display:flex)screen and (max-width:0);@import"narrow.css"supports(display:flex)screen and (max-width:0);
Expand Up @@ -15,3 +15,15 @@
color: red;
}
}

@media (min-width: calc(10px * 0)) {
.class1 {
color: red;
}
}

@media (min-width: calc(0px + 0px)) {
.class1 {
color: red;
}
}
@@ -1 +1 @@
@media(min-width:20px){.class1{color:red}}@media(min-width:calc(20px)){.class1{color:red}}@media(color:1){.class1{color:red}}
@media(min-width:20px){.class1{color:red}}@media(min-width:calc(20px)){.class1{color:red}}@media(color:1){.class1{color:red}}@media(min-width:0){.class1{color:red}}@media(min-width:0){.class1{color:red}}
Expand Up @@ -9,3 +9,15 @@
background: red;
}
}

@supports (width: calc(0px * 100)) {
div {
background: red;
}
}

@supports (width: calc(0px + 0px)) {
div {
background: red;
}
}
@@ -1 +1 @@
@supports(width:calc(200px)){div{background:red}}@supports(width:calc(calc(200px))){div{background:red}}
@supports(width:calc(200px)){div{background:red}}@supports(width:calc(calc(200px))){div{background:red}}@supports(width:calc(0px)){div{background:red}}@supports(width:calc(0px)){div{background:red}}
Expand Up @@ -25,3 +25,11 @@
.class7 {
width: calc((100px - 1em) + (-50px + 1em));
}

.class8 {
width: calc(10px * 0);
}

.class8 {
width: calc(0px + 0px);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 13 additions & 8 deletions crates/swc_css_minifier/tests/fixture/compress-length/input.css
Expand Up @@ -34,8 +34,8 @@
right: calc(max(100vw, 0rem));
}

.class10 {
top: 0.000px;
.class10 {
top: 0.000px;
}

@media (min-width: 0px) {
Expand All @@ -50,8 +50,8 @@
}
}

.class11 {
font: normal normal 400 0px/ 0px cursive;
.class11 {
font: normal normal 400 0px/ 0px cursive;
}

.class12 {
Expand All @@ -63,16 +63,16 @@
margin: 0q;
}

.class14 {
transform: translate(0px);
.class14 {
transform: translate(0px);
}

.class15 {
padding: min(1vw, 0in) max(1vw, 0px) clamp(0em, 1vw, 10px) 0px;
}

.class16 {
padding: 1px 0vmax 2px 3px;
.class16 {
padding: 1px 0vmax 2px 3px;
}

.class17 {
Expand Down Expand Up @@ -286,3 +286,8 @@
.class90 {
width: 0.075pt;
}

@media(min-width:0px){.class1{color:red}}
@media(min-width:0){.class1{color:red}}
@container (inline-size >= 0){h2 {font-size: calc(1.2em + 1cqi)}}
@container (inline-size >= 0px){h2 {font-size: calc(1.2em + 1cqi)}}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 0f38e38

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 0f38e38 Previous: b40d486 Ratio
es/full/minify/libraries/antd 1914444605 ns/iter (± 107549417) 2015562752 ns/iter (± 28005356) 0.95
es/full/minify/libraries/d3 430217137 ns/iter (± 14323961) 384817667 ns/iter (± 23995060) 1.12
es/full/minify/libraries/echarts 1582893392 ns/iter (± 73005719) 1706567840 ns/iter (± 47308913) 0.93
es/full/minify/libraries/jquery 108417826 ns/iter (± 7090934) 114085605 ns/iter (± 3107774) 0.95
es/full/minify/libraries/lodash 115140595 ns/iter (± 4076471) 137797128 ns/iter (± 13707766) 0.84
es/full/minify/libraries/moment 67383821 ns/iter (± 3553303) 74946097 ns/iter (± 15800127) 0.90
es/full/minify/libraries/react 28080496 ns/iter (± 17570352) 25100276 ns/iter (± 1373092) 1.12
es/full/minify/libraries/terser 327031846 ns/iter (± 7704815) 478940130 ns/iter (± 336414217) 0.68
es/full/minify/libraries/three 568710094 ns/iter (± 16681015) 627116485 ns/iter (± 24200158) 0.91
es/full/minify/libraries/typescript 3608956218 ns/iter (± 148304921) 4218395148 ns/iter (± 204695328) 0.86
es/full/minify/libraries/victory 858509768 ns/iter (± 9164333) 926313940 ns/iter (± 87392427) 0.93
es/full/minify/libraries/vue 169582844 ns/iter (± 14947793) 174570347 ns/iter (± 8157904) 0.97
es/full/codegen/es3 35854 ns/iter (± 2544) 41927 ns/iter (± 1199) 0.86
es/full/codegen/es5 35456 ns/iter (± 3674) 41501 ns/iter (± 1364) 0.85
es/full/codegen/es2015 34893 ns/iter (± 1385) 41220 ns/iter (± 2111) 0.85
es/full/codegen/es2016 35192 ns/iter (± 1310) 41122 ns/iter (± 2960) 0.86
es/full/codegen/es2017 35378 ns/iter (± 2322) 40630 ns/iter (± 2716) 0.87
es/full/codegen/es2018 34661 ns/iter (± 841) 41199 ns/iter (± 4842) 0.84
es/full/codegen/es2019 34990 ns/iter (± 1626) 41724 ns/iter (± 27729) 0.84
es/full/codegen/es2020 34756 ns/iter (± 1223) 41720 ns/iter (± 13576) 0.83
es/full/all/es3 225981851 ns/iter (± 23718760) 239405001 ns/iter (± 23327302) 0.94
es/full/all/es5 220831314 ns/iter (± 27960088) 226843921 ns/iter (± 21595910) 0.97
es/full/all/es2015 193600112 ns/iter (± 22578234) 183606446 ns/iter (± 17213743) 1.05
es/full/all/es2016 177571546 ns/iter (± 14406823) 182694742 ns/iter (± 14969074) 0.97
es/full/all/es2017 176926235 ns/iter (± 20711515) 183324645 ns/iter (± 13771465) 0.97
es/full/all/es2018 175353587 ns/iter (± 20991811) 176973367 ns/iter (± 11613859) 0.99
es/full/all/es2019 172146189 ns/iter (± 17618816) 179108311 ns/iter (± 16975959) 0.96
es/full/all/es2020 162769040 ns/iter (± 10103792) 174247112 ns/iter (± 20964067) 0.93
es/full/parser 752268 ns/iter (± 48247) 885301 ns/iter (± 76033) 0.85
es/full/base/fixer 26986 ns/iter (± 1786) 33082 ns/iter (± 1310) 0.82
es/full/base/resolver_and_hygiene 95519 ns/iter (± 5639) 120100 ns/iter (± 11597) 0.80
serialization of ast node 208 ns/iter (± 8) 253 ns/iter (± 14) 0.82
serialization of serde 229 ns/iter (± 9) 256 ns/iter (± 15) 0.89

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.