Skip to content

Commit

Permalink
feat(css/minifier): compress alpha in HEX
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 14, 2022
1 parent 863f9f8 commit 92e3f54
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
25 changes: 24 additions & 1 deletion crates/swc_css_minifier/src/compressor/color.rs
@@ -1,4 +1,4 @@
use swc_atoms::js_word;
use swc_atoms::{js_word, JsWord};
use swc_common::DUMMY_SP;
use swc_css_ast::*;
use swc_css_utils::NAMED_COLORS;
Expand All @@ -9,6 +9,27 @@ use super::{
};
use crate::compressor::alpha_value::compress_alpha_value;

fn compress_alpha_in_hex(value: &JsWord) -> Option<&str> {
let length = value.len();

if length == 3 || length == 6 {
return None;
}

let chars = value.as_bytes();

if length == 8
&& (chars[6] == b'f' || chars[6] == b'F')
&& (chars[7] == b'f' || chars[7] == b'F')
{
return Some(&value[0..6]);
} else if length == 4 && chars[3] == b'f' || chars[3] == b'F' {
return Some(&value[0..3]);
}

None
}

fn get_short_hex(v: u32) -> u32 {
((v & 0x0ff00000) >> 12) | ((v & 0x00000ff0) >> 4)
}
Expand Down Expand Up @@ -422,6 +443,8 @@ impl Compressor {
raw: None,
},
));
} else if let Some(new_value) = compress_alpha_in_hex(value) {
*value = new_value.into();
}
}
Color::AbsoluteColorBase(AbsoluteColorBase::Function(Function {
Expand Down
Expand Up @@ -423,4 +423,11 @@

.class-83 {
color: rgba(100, 100, 100, -300%);
}

.alpha {
color: #ffff;
color: #ffffffff;
color: #123f;
color: #123abcff;
}

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

0 comments on commit 92e3f54

Please sign in to comment.