feat(css/minifier): Compress contents of @keyframes at-rule (#3579)

This commit is contained in:
Alexander Akait 2022-02-16 08:50:53 +03:00 committed by GitHub
parent dfb4da259f
commit 803499c5be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 152 additions and 2 deletions

View File

@ -0,0 +1,56 @@
use swc_common::DUMMY_SP;
use swc_css_ast::*;
use swc_css_visit::{VisitMut, VisitMutWith};
pub fn compress_keyframes() -> impl VisitMut {
CompressKeyframes {}
}
struct CompressKeyframes {}
impl VisitMut for CompressKeyframes {
fn visit_mut_keyframes_rule(&mut self, keyframes_rule: &mut KeyframesRule) {
keyframes_rule.visit_mut_children_with(self);
match &keyframes_rule.name {
KeyframesName::Str(i)
if !matches!(
&*i.value.to_lowercase(),
"initial" | "inherit" | "unset" | "revert" | "default"
) =>
{
keyframes_rule.name = KeyframesName::CustomIdent(CustomIdent {
span: i.span,
value: i.value.to_string().into(),
raw: i.raw[1..i.raw.len() - 1].to_string().into(),
})
}
_ => {}
}
}
fn visit_mut_keyframe_selector(&mut self, keyframe_selector: &mut KeyframeSelector) {
keyframe_selector.visit_mut_children_with(self);
match keyframe_selector {
KeyframeSelector::Ident(i) if &*i.value.to_lowercase() == "from" => {
*keyframe_selector = KeyframeSelector::Percentage(Percentage {
span: i.span,
value: Number {
span: DUMMY_SP,
value: 0.0,
raw: "0%".into(),
},
})
}
KeyframeSelector::Percentage(i) if i.value.value == 100.0 => {
*keyframe_selector = KeyframeSelector::Ident(Ident {
span: i.span,
value: "to".into(),
raw: "to".into(),
})
}
_ => {}
}
}
}

View File

@ -1,3 +1,4 @@
pub mod angle;
pub mod empty_qualified_rule;
pub mod keyframes;
pub mod time;

View File

@ -2,12 +2,14 @@ use swc_css_ast::*;
use swc_css_visit::VisitMutWith;
use self::compress::{
angle::compress_angle, empty_qualified_rule::compress_empty_qualified_rule, time::compress_time,
angle::compress_angle, empty_qualified_rule::compress_empty_qualified_rule,
keyframes::compress_keyframes, time::compress_time,
};
mod compress;
pub fn minify(stylesheet: &mut Stylesheet) {
stylesheet.visit_mut_with(&mut compress_keyframes());
stylesheet.visit_mut_with(&mut compress_empty_qualified_rule());
stylesheet.visit_mut_with(&mut compress_time());
stylesheet.visit_mut_with(&mut compress_angle());

View File

@ -0,0 +1,90 @@
@keyframes slide-right {
from {
margin-left: 0px;
}
50% {
margin-left: 110px;
opacity: 1;
}
50% {
opacity: 0.9;
}
to {
margin-left: 200px;
}
}
@keyframes slide-right-upper-case {
FROM {
margin-left: 0px;
}
50% {
margin-left: 110px;
opacity: 1;
}
50% {
opacity: 0.9;
}
TO {
margin-left: 200px;
}
}
@keyframes slide-right-one {
0% {
margin-left: 0px;
}
50% {
margin-left: 110px;
opacity: 1;
}
50% {
opacity: 0.9;
}
100% {
margin-left: 200px;
}
}
@keyframes slide-right-two {
0% {
margin-left: 0px;
}
50% {
margin-left: 110px;
opacity: 1;
}
50% {
opacity: 0.9;
}
100.0000% {
margin-left: 200px;
}
}
@keyframes slide-right-three {
0%, 50% {
margin-left: 0px;
}
60%, 100% {
margin-left: 200px;
}
}
@keyframes "foo" {
0% {
margin-left: 0px;
}
100% {
margin-left: 200px;
}
}
@keyframes "initial" {
0% {
margin-left: 0px;
}
100% {
margin-left: 200px;
}
}

View File

@ -0,0 +1 @@
@keyframes slide-right{0%{margin-left:0px}50%{margin-left:110px;opacity:1}50%{opacity:.9}to{margin-left:200px}}@keyframes slide-right-upper-case{0%{margin-left:0px}50%{margin-left:110px;opacity:1}50%{opacity:.9}TO{margin-left:200px}}@keyframes slide-right-one{0%{margin-left:0px}50%{margin-left:110px;opacity:1}50%{opacity:.9}to{margin-left:200px}}@keyframes slide-right-two{0%{margin-left:0px}50%{margin-left:110px;opacity:1}50%{opacity:.9}to{margin-left:200px}}@keyframes slide-right-three{0%,50%{margin-left:0px}60%,to{margin-left:200px}}@keyframes foo{0%{margin-left:0px}to{margin-left:200px}}@keyframes"initial"{0%{margin-left:0px}to{margin-left:200px}}

File diff suppressed because one or more lines are too long