feat(css/minifier): Compress @import at-rule (#3850)

This commit is contained in:
Alexander Akait 2022-03-04 15:33:39 +03:00 committed by GitHub
parent c5ef673921
commit ccfb4bfc0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 5 deletions

View File

@ -0,0 +1,39 @@
use swc_css_ast::*;
use swc_css_visit::{VisitMut, VisitMutWith};
pub fn compress_at_rule() -> impl VisitMut {
CompressAtRule {}
}
struct CompressAtRule {}
impl VisitMut for CompressAtRule {
fn visit_mut_import_href(&mut self, import_href: &mut ImportHref) {
import_href.visit_mut_children_with(self);
if let ImportHref::Url(Url {
value: Some(value),
modifiers,
span,
..
}) = import_href
{
if let Some(modifiers) = modifiers {
if !modifiers.is_empty() {
return;
}
}
let new_value = match value {
UrlValue::Str(Str { value, .. }) => value,
UrlValue::Raw(UrlValueRaw { value, .. }) => value,
};
*import_href = ImportHref::Str(Str {
span: *span,
value: (&*new_value).into(),
raw: (&*new_value).into(),
});
}
}
}

View File

@ -1,5 +1,6 @@
pub mod alpha_value;
pub mod angle;
pub mod at_rule;
pub mod declaration;
pub mod easing_function;
pub mod empty;

View File

@ -4,10 +4,10 @@ use swc_css_ast::*;
use swc_css_visit::VisitMutWith;
use self::compress::{
alpha_value::compress_alpha_value, angle::compress_angle, declaration::compress_declaration,
easing_function::compress_easing_function, empty::compress_empty,
frequency::compress_frequency, keyframes::compress_keyframes, length::compress_length,
selector::compress_selector, time::compress_time,
alpha_value::compress_alpha_value, angle::compress_angle, at_rule::compress_at_rule,
declaration::compress_declaration, easing_function::compress_easing_function,
empty::compress_empty, frequency::compress_frequency, keyframes::compress_keyframes,
length::compress_length, selector::compress_selector, time::compress_time,
transform_function::compress_transform_function, unicore_range::compress_unicode_range,
url::compress_url,
};
@ -28,5 +28,5 @@ pub fn minify(stylesheet: &mut Stylesheet) {
stylesheet.visit_mut_with(&mut compress_declaration());
stylesheet.visit_mut_with(&mut compress_selector());
stylesheet.visit_mut_with(&mut compress_keyframes());
stylesheet.visit_mut_with(&mut compress_selector());
stylesheet.visit_mut_with(&mut compress_at_rule());
}

View File

@ -0,0 +1,10 @@
@import 'custom.css';
@import url(custom.css);
@import url( custom.css );
@import url('custom.css');
@import url("custom.css");
@import url('custo(m.css');
@import url('custom.css') screen and (orientation:landscape);
@import url("fallback-layout.css") supports(not (display: flex));
@import "mystyle.css" supports(display: flex);
@import "mystyle.css" supports((display: flex));

View File

@ -0,0 +1 @@
@import"custom.css";@import"custom.css";@import"custom.css";@import"custom.css";@import"custom.css";@import"custo\\(m.css";@import"custom.css"screen and (orientation:landscape);@import"fallback-layout.css"supports(not (display:flex));@import"mystyle.css"supports(display:flex);@import"mystyle.css"supports((display:flex));