Skip to content

Commit

Permalink
feat(css/ast): Support @custom-media at-rule (#6152)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 17, 2022
1 parent 21b7605 commit d46a19b
Show file tree
Hide file tree
Showing 10 changed files with 20,401 additions and 17 deletions.
36 changes: 36 additions & 0 deletions crates/swc_css_ast/src/at_rule.rs
@@ -1,5 +1,6 @@
use is_macro::Is;
use string_enum::StringEnum;
use swc_atoms::JsWord;
use swc_common::{ast_node, EqIgnoreSpan, Span};

use crate::{
Expand Down Expand Up @@ -63,6 +64,8 @@ pub enum AtRulePrelude {
LayerPrelude(LayerPrelude),
#[tag("ContainerCondition")]
ContainerPrelude(ContainerCondition),
#[tag("CustomMedia")]
CustomMediaPrelude(CustomMediaQuery),
}

#[ast_node]
Expand Down Expand Up @@ -801,3 +804,36 @@ pub enum SizeFeatureName {
#[tag("Ident")]
Ident(Ident),
}

#[ast_node("ExtensionName")]
#[derive(Eq, Hash)]
pub struct ExtensionName {
pub span: Span,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
pub value: JsWord,
#[cfg_attr(feature = "rkyv", with(swc_atoms::EncodeJsWord))]
pub raw: Option<JsWord>,
}

impl EqIgnoreSpan for ExtensionName {
fn eq_ignore_span(&self, other: &Self) -> bool {
self.value == other.value
}
}

#[ast_node("CustomMedia")]
#[derive(Eq, Hash, EqIgnoreSpan)]
pub struct CustomMediaQuery {
pub span: Span,
pub name: ExtensionName,
pub media: CustomMediaQueryMediaType,
}

#[ast_node]
#[derive(Eq, Hash, Is, EqIgnoreSpan)]
pub enum CustomMediaQueryMediaType {
#[tag("Ident")]
Ident(Ident),
#[tag("MediaQueryList")]
MediaQueryList(MediaQueryList),
}
68 changes: 51 additions & 17 deletions crates/swc_css_codegen/src/lib.rs
Expand Up @@ -138,27 +138,27 @@ where
AtRulePrelude::ListOfComponentValues(n) => emit!(self, n),
AtRulePrelude::CharsetPrelude(n) => {
space!(self);
emit!(self, n)
emit!(self, n);
}
AtRulePrelude::PropertyPrelude(n) => {
space!(self);
emit!(self, n)
emit!(self, n);
}
AtRulePrelude::CounterStylePrelude(n) => {
space!(self);
emit!(self, n)
emit!(self, n);
}
AtRulePrelude::ColorProfilePrelude(n) => {
space!(self);
emit!(self, n);
}
AtRulePrelude::DocumentPrelude(n) => {
space!(self);
emit!(self, n)
emit!(self, n);
}
AtRulePrelude::FontPaletteValuesPrelude(n) => {
space!(self);
emit!(self, n)
emit!(self, n);
}
AtRulePrelude::FontFeatureValuesPrelude(n) => {
let need_space = !matches!(n.font_family.get(0), Some(FamilyName::Str(_)));
Expand All @@ -169,11 +169,11 @@ where
formatting_space!(self);
}

emit!(self, n)
emit!(self, n);
}
AtRulePrelude::NestPrelude(n) => {
space!(self);
emit!(self, n)
emit!(self, n);
}
AtRulePrelude::KeyframesPrelude(n) => {
match n {
Expand All @@ -187,7 +187,7 @@ where
}
}

emit!(self, n)
emit!(self, n);
}
AtRulePrelude::ImportPrelude(n) => {
match &*n.href {
Expand All @@ -199,7 +199,7 @@ where
}
}

emit!(self, n)
emit!(self, n);
}
AtRulePrelude::NamespacePrelude(n) => emit!(self, n),
AtRulePrelude::MediaPrelude(n) => {
Expand Down Expand Up @@ -230,7 +230,7 @@ where
formatting_space!(self);
}

emit!(self, n)
emit!(self, n);
}
AtRulePrelude::SupportsPrelude(n) => {
let need_space = !matches!(
Expand Down Expand Up @@ -262,11 +262,11 @@ where
}
}

emit!(self, n)
emit!(self, n);
}
AtRulePrelude::LayerPrelude(n) => {
space!(self);
emit!(self, n)
emit!(self, n);
}
AtRulePrelude::ContainerPrelude(n) => {
let need_space = match n.name {
Expand All @@ -289,7 +289,11 @@ where
formatting_space!(self);
}

emit!(self, n)
emit!(self, n);
}
AtRulePrelude::CustomMediaPrelude(n) => {
space!(self);
emit!(self, n);
}
}
}
Expand Down Expand Up @@ -1088,6 +1092,21 @@ where
emit!(self, n.right);
}

#[emitter]
fn emit_custom_media_query(&mut self, n: &CustomMediaQuery) -> Result {
emit!(self, n.name);
space!(self);
emit!(self, n.media);
}

#[emitter]
fn emit_custom_media_query_media_type(&mut self, n: &CustomMediaQueryMediaType) -> Result {
match n {
CustomMediaQueryMediaType::MediaQueryList(n) => emit!(self, n),
CustomMediaQueryMediaType::Ident(n) => emit!(self, n),
}
}

fn emit_list_of_component_values_inner(
&mut self,
nodes: &[ComponentValue],
Expand Down Expand Up @@ -1473,22 +1492,22 @@ where
}

#[emitter]
fn emit_custom_highlight_name(&mut self, n: &CustomHighlightName) -> Result {
fn emit_custom_ident(&mut self, n: &CustomIdent) -> Result {
if self.config.minify {
let serialized = serialize_ident(&n.value, n.raw.as_deref(), true);

write_raw!(self, n.span, &serialized);
} else if let Some(raw) = &n.raw {
write_raw!(self, n.span, raw);
} else {
let serialized = serialize_ident(&n.value, n.raw.as_deref(), true);
let serialized = serialize_ident(&n.value, n.raw.as_deref(), false);

write_raw!(self, n.span, &serialized);
}
}

#[emitter]
fn emit_custom_ident(&mut self, n: &CustomIdent) -> Result {
fn emit_dashed_ident(&mut self, n: &DashedIdent) -> Result {
if self.config.minify {
let serialized = serialize_ident(&n.value, n.raw.as_deref(), true);

Expand All @@ -1503,7 +1522,7 @@ where
}

#[emitter]
fn emit_dashed_ident(&mut self, n: &DashedIdent) -> Result {
fn emit_extension_name(&mut self, n: &ExtensionName) -> Result {
if self.config.minify {
let serialized = serialize_ident(&n.value, n.raw.as_deref(), true);

Expand All @@ -1517,6 +1536,21 @@ where
}
}

#[emitter]
fn emit_custom_highlight_name(&mut self, n: &CustomHighlightName) -> Result {
if self.config.minify {
let serialized = serialize_ident(&n.value, n.raw.as_deref(), true);

write_raw!(self, n.span, &serialized);
} else if let Some(raw) = &n.raw {
write_raw!(self, n.span, raw);
} else {
let serialized = serialize_ident(&n.value, n.raw.as_deref(), true);

write_raw!(self, n.span, &serialized);
}
}

#[emitter]
fn emit_custom_property_name(&mut self, n: &CustomPropertyName) -> Result {
if let Some(raw) = &n.raw {
Expand Down

1 comment on commit d46a19b

@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: d46a19b Previous: d4a733d Ratio
es/full/minify/libraries/antd 1959122788 ns/iter (± 114346357) 1948312900 ns/iter (± 55587654) 1.01
es/full/minify/libraries/d3 426823348 ns/iter (± 15283137) 451194529 ns/iter (± 27518322) 0.95
es/full/minify/libraries/echarts 1629225489 ns/iter (± 77931279) 1631970074 ns/iter (± 155478946) 1.00
es/full/minify/libraries/jquery 121351301 ns/iter (± 5405295) 125248562 ns/iter (± 10443973) 0.97
es/full/minify/libraries/lodash 128926865 ns/iter (± 6642282) 136136662 ns/iter (± 7438137) 0.95
es/full/minify/libraries/moment 65361811 ns/iter (± 3795864) 75300997 ns/iter (± 6765838) 0.87
es/full/minify/libraries/react 24593078 ns/iter (± 960018) 30185866 ns/iter (± 15827278) 0.81
es/full/minify/libraries/terser 364703586 ns/iter (± 25018400) 412431577 ns/iter (± 21139102) 0.88
es/full/minify/libraries/three 623310520 ns/iter (± 40559729) 756644996 ns/iter (± 114680812) 0.82
es/full/minify/libraries/typescript 3808806250 ns/iter (± 71778871) 4217164183 ns/iter (± 277853843) 0.90
es/full/minify/libraries/victory 908073823 ns/iter (± 34704156) 967423936 ns/iter (± 84020217) 0.94
es/full/minify/libraries/vue 182892907 ns/iter (± 13043894) 198111721 ns/iter (± 30205419) 0.92
es/full/codegen/es3 34380 ns/iter (± 2034) 36116 ns/iter (± 3526) 0.95
es/full/codegen/es5 34019 ns/iter (± 1681) 35017 ns/iter (± 2650) 0.97
es/full/codegen/es2015 34098 ns/iter (± 1503) 35781 ns/iter (± 4396) 0.95
es/full/codegen/es2016 34156 ns/iter (± 2289) 34798 ns/iter (± 2255) 0.98
es/full/codegen/es2017 34141 ns/iter (± 2175) 46452 ns/iter (± 26296) 0.73
es/full/codegen/es2018 34570 ns/iter (± 2738) 35200 ns/iter (± 3169) 0.98
es/full/codegen/es2019 34356 ns/iter (± 2413) 35464 ns/iter (± 5451) 0.97
es/full/codegen/es2020 34222 ns/iter (± 1818) 35526 ns/iter (± 2494) 0.96
es/full/all/es3 243905038 ns/iter (± 15957710) 299051875 ns/iter (± 119545602) 0.82
es/full/all/es5 230725667 ns/iter (± 16424476) 272207182 ns/iter (± 86506413) 0.85
es/full/all/es2015 182181842 ns/iter (± 14133133) 156521306 ns/iter (± 26821526) 1.16
es/full/all/es2016 183705985 ns/iter (± 19873628) 214759228 ns/iter (± 21076519) 0.86
es/full/all/es2017 155196707 ns/iter (± 8772843) 224515970 ns/iter (± 26972682) 0.69
es/full/all/es2018 151255049 ns/iter (± 8137118) 194888486 ns/iter (± 20419326) 0.78
es/full/all/es2019 151914465 ns/iter (± 10700931) 199450236 ns/iter (± 26156647) 0.76
es/full/all/es2020 148285241 ns/iter (± 10793044) 177148780 ns/iter (± 19240166) 0.84
es/full/parser 759598 ns/iter (± 37437) 789872 ns/iter (± 92710) 0.96
es/full/base/fixer 27075 ns/iter (± 1262) 27572 ns/iter (± 2347) 0.98
es/full/base/resolver_and_hygiene 95723 ns/iter (± 6090) 97912 ns/iter (± 6015) 0.98
serialization of ast node 210 ns/iter (± 8) 213 ns/iter (± 14) 0.99
serialization of serde 227 ns/iter (± 5) 213 ns/iter (± 15) 1.07

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

Please sign in to comment.