Skip to content

Commit

Permalink
Introduce "visitor" feature flag (#367)
Browse files Browse the repository at this point in the history
* Introduce "visitor" feature flag

This commit adds a "visitor" feature flag to the lightningcss crate.

This feature flag controls whether or not the `Visitor` trait gets
compiled. The "visitor" flag is disabled by default.

Disabling the "visitor" feature disables the `lightningcss-derive`
dependency.

This commit on its own reduces the lightningcss crate's build time by
about 8% (as measured by comparing `cargo build --timings` with and
without the "visitor" feature enabled.

When combined with some of the other soon to land commits, this commit
will make it possible to remove `syn` from the dependency tree when you
don't need it, which should lead to substantial compile time savings.

Related: #357

* Specify required features for examples

Co-authored-by: Devon Govett <devongovett@gmail.com>
  • Loading branch information
chinedufn and devongovett authored Dec 21, 2022
1 parent ca3e406 commit 560b784
Show file tree
Hide file tree
Showing 77 changed files with 552 additions and 270 deletions.
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cli = ["clap", "serde_json", "browserslist", "jemallocator"]
grid = []
nodejs = ["dep:serde"]
serde = ["dep:serde", "smallvec/serde", "cssparser/serde"]
visitor = ["lightningcss-derive"]

[dependencies]
serde = { version = "1.0.123", features = ["derive"], optional = true }
Expand All @@ -53,7 +54,7 @@ browserslist-rs = { version = "0.7.0", optional = true }
rayon = { version = "1.5.1", optional = true }
dashmap = { version = "5.0.0", optional = true }
serde_json = { version = "1.0.78", optional = true }
lightningcss-derive = { version = "1.0.0-alpha.35", path = "./derive" }
lightningcss-derive = { version = "1.0.0-alpha.35", path = "./derive", optional = true }

[target.'cfg(target_os = "macos")'.dependencies]
jemallocator = { version = "0.3.2", features = ["disable_initial_exec_tls"], optional = true }
Expand All @@ -70,6 +71,14 @@ name = "cli_integration_tests"
path = "tests/cli_integration_tests.rs"
required-features = ["cli"]

[[example]]
name = "custom_at_rule"
required-features = ["visitor"]

[[example]]
name = "serialize"
required-features = ["serde"]

[profile.release]
lto = true
codegen-units = 1
Expand Down
1 change: 1 addition & 0 deletions examples/custom_at_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl<'a, 'i> Visitor<'i, AtRule> for ApplyVisitor<'a, 'i> {
}
}

#[cfg(feature = "visitor")]
impl<'i, V: Visitor<'i, AtRule>> Visit<'i, AtRule, V> for AtRule {
const CHILD_TYPES: VisitTypes = VisitTypes::empty();

Expand Down
4 changes: 3 additions & 1 deletion src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use crate::properties::{Property, PropertyId};
use crate::targets::Browsers;
use crate::traits::{PropertyHandler, ToCss};
use crate::values::string::CowArcStr;
#[cfg(feature = "visitor")]
use crate::visitor::Visit;
use cssparser::*;

Expand All @@ -42,7 +43,8 @@ use cssparser::*;
/// Properties are separated into a list of `!important` declararations,
/// and a list of normal declarations. This reduces memory usage compared
/// with storing a boolean along with each property.
#[derive(Debug, PartialEq, Clone, Visit)]
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DeclarationBlock<'i> {
/// A list of `!important` declarations in the block.
Expand Down
4 changes: 3 additions & 1 deletion src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::printer::PrinterOptions;
use crate::rules::import::ImportRule;
use crate::traits::ToCss;
use crate::values::url::Url;
#[cfg(feature = "visitor")]
use crate::visitor::Visit;
use cssparser::SourceLocation;
#[cfg(any(feature = "serde", feature = "nodejs"))]
Expand Down Expand Up @@ -127,7 +128,8 @@ pub struct SourceRange {
}

/// A line and column position within a source file.
#[derive(Debug, Clone, Copy, PartialEq, Visit)]
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(any(feature = "serde", feature = "nodejs"), derive(serde::Serialize))]
#[cfg_attr(any(feature = "serde"), derive(serde::Deserialize))]
pub struct Location {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub mod targets;
pub mod traits;
pub mod values;
pub mod vendor_prefix;
#[cfg(feature = "visitor")]
pub mod visitor;

#[cfg(test)]
Expand Down
11 changes: 7 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ macro_rules! enum_property {
}
) => {
$(#[$outer])*
#[derive(Debug, Clone, Copy, PartialEq, Visit)]
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(rename_all = "lowercase"))]
$vis enum $name {
$(
Expand Down Expand Up @@ -66,7 +67,7 @@ macro_rules! enum_property {
}
) => {
$(#[$outer])*
#[derive(Debug, Clone, Copy, PartialEq, Visit)]
#[derive(Debug, Clone, Copy, PartialEq)] #[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
$vis enum $name {
$(
Expand Down Expand Up @@ -339,7 +340,8 @@ macro_rules! define_shorthand {
}
) => {
$(#[$outer])*
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct $name$(<$l>)? {
$(
Expand Down Expand Up @@ -553,7 +555,8 @@ macro_rules! define_list_shorthand {
}
) => {
$(#[$outer])*
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct $name$(<$l>)? {
$(
Expand Down
28 changes: 18 additions & 10 deletions src/media_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ use crate::values::ident::Ident;
use crate::values::number::CSSNumber;
use crate::values::string::CowArcStr;
use crate::values::{length::Length, ratio::Ratio, resolution::Resolution};
#[cfg(feature = "visitor")]
use crate::visitor::Visit;
use cssparser::*;
use std::collections::{HashMap, HashSet};

/// A [media query list](https://drafts.csswg.org/mediaqueries/#mq-list).
#[derive(Clone, Debug, PartialEq, Visit)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MediaList<'i> {
/// The list of media queries.
Expand Down Expand Up @@ -144,7 +146,8 @@ enum_property! {
}

/// A [media type](https://drafts.csswg.org/mediaqueries/#media-types) within a media query.
#[derive(Clone, Debug, PartialEq, Visit)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -176,8 +179,9 @@ impl<'i> Parse<'i> for MediaType<'i> {
}

/// A [media query](https://drafts.csswg.org/mediaqueries/#media).
#[derive(Clone, Debug, PartialEq, Visit)]
#[visit(visit_media_query, MEDIA_QUERIES)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "visitor", visit(visit_media_query, MEDIA_QUERIES))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MediaQuery<'i> {
/// The qualifier for this query.
Expand Down Expand Up @@ -360,7 +364,8 @@ enum_property! {
}

/// Represents a media condition.
#[derive(Clone, Debug, PartialEq, Visit)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand All @@ -371,10 +376,10 @@ pub enum MediaCondition<'i> {
#[cfg_attr(feature = "serde", serde(borrow))]
Feature(MediaFeature<'i>),
/// A negation of a condition.
#[skip_type]
#[cfg_attr(feature = "visitor", skip_type)]
Not(Box<MediaCondition<'i>>),
/// A set of joint operations.
#[skip_type]
#[cfg_attr(feature = "visitor", skip_type)]
Operation(Vec<MediaCondition<'i>>, Operator),
}

Expand Down Expand Up @@ -495,7 +500,8 @@ impl<'i> ToCss for MediaCondition<'i> {
}

/// A [comparator](https://drafts.csswg.org/mediaqueries/#typedef-mf-comparison) within a media query.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Visit)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -551,7 +557,8 @@ impl MediaFeatureComparison {
}

/// A [media feature](https://drafts.csswg.org/mediaqueries/#typedef-media-feature)
#[derive(Clone, Debug, PartialEq, Visit)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -753,7 +760,8 @@ where
/// [media feature value](https://drafts.csswg.org/mediaqueries/#typedef-mf-value) within a media query.
///
/// See [MediaFeature](MediaFeature).
#[derive(Clone, Debug, PartialEq, Visit)]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down
2 changes: 2 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::traits::Parse;
use crate::values::ident::{CustomIdent, DashedIdent};
use crate::values::string::CowArcStr;
use crate::vendor_prefix::VendorPrefix;
#[cfg(feature = "visitor")]
use crate::visitor::{Visit, VisitTypes, Visitor};
use cssparser::*;
use parcel_selectors::parser::NestingRequirement;
Expand Down Expand Up @@ -110,6 +111,7 @@ impl crate::traits::ToCss for DefaultAtRule {
}
}

#[cfg(feature = "visitor")]
impl<'i, V: Visitor<'i, DefaultAtRule>> Visit<'i, DefaultAtRule, V> for DefaultAtRule {
const CHILD_TYPES: VisitTypes = VisitTypes::empty();
fn visit_children(&mut self, _: &mut V) {}
Expand Down
28 changes: 19 additions & 9 deletions src/properties/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ use crate::targets::Browsers;
use crate::traits::{FromStandard, Parse, PropertyHandler, Shorthand, ToCss};
use crate::values::length::LengthPercentage;
use crate::vendor_prefix::VendorPrefix;
#[cfg(feature = "visitor")]
use crate::visitor::Visit;
use cssparser::*;

/// A [`<baseline-position>`](https://www.w3.org/TR/css-align-3/#typedef-baseline-position) value,
/// as used in the alignment properties.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -107,7 +109,8 @@ enum_property! {
}

/// A value for the [align-content](https://www.w3.org/TR/css-align-3/#propdef-align-content) property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -166,7 +169,8 @@ impl ToCss for AlignContent {
}

/// A value for the [justify-content](https://www.w3.org/TR/css-align-3/#propdef-justify-content) property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -327,7 +331,8 @@ enum_property! {
}

/// A value for the [align-self](https://www.w3.org/TR/css-align-3/#align-self-property) property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -393,7 +398,8 @@ impl ToCss for AlignSelf {
}

/// A value for the [justify-self](https://www.w3.org/TR/css-align-3/#justify-self-property) property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -549,7 +555,8 @@ impl ToCss for PlaceSelf {
}

/// A value for the [align-items](https://www.w3.org/TR/css-align-3/#align-items-property) property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -608,7 +615,8 @@ impl ToCss for AlignItems {
}

/// A legacy justification keyword, as used in the `justify-items` property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -674,7 +682,8 @@ impl ToCss for LegacyJustify {
}

/// A value for the [justify-items](https://www.w3.org/TR/css-align-3/#justify-items-property) property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -829,7 +838,8 @@ impl ToCss for PlaceItems {

/// A [gap](https://www.w3.org/TR/css-align-3/#column-row-gap) value, as used in the
/// `column-gap` and `row-gap` properties.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down
7 changes: 5 additions & 2 deletions src/properties/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use crate::traits::{Parse, PropertyHandler, Shorthand, ToCss, Zero};
use crate::values::number::CSSNumber;
use crate::values::string::CowArcStr;
use crate::values::{easing::EasingFunction, ident::CustomIdent, time::Time};
#[cfg(feature = "visitor")]
use crate::visitor::Visit;
use cssparser::*;
use itertools::izip;
use smallvec::SmallVec;

/// A value for the [animation-name](https://drafts.csswg.org/css-animations/#animation-name) property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -87,7 +89,8 @@ impl<'i> ToCss for AnimationName<'i> {
pub type AnimationNameList<'i> = SmallVec<[AnimationName<'i>; 1]>;

/// A value for the [animation-iteration-count](https://drafts.csswg.org/css-animations/#animation-iteration-count) property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down
10 changes: 7 additions & 3 deletions src/properties/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use crate::traits::{FallbackValues, Parse, PropertyHandler, Shorthand, ToCss};
use crate::values::color::ColorFallbackKind;
use crate::values::image::ImageFallback;
use crate::values::{color::CssColor, image::Image, length::LengthPercentageOrAuto, position::*};
#[cfg(feature = "visitor")]
use crate::visitor::Visit;
use cssparser::*;
use itertools::izip;
use smallvec::SmallVec;

/// A value for the [background-size](https://www.w3.org/TR/css-backgrounds-3/#background-size) property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
Expand Down Expand Up @@ -109,7 +111,8 @@ enum_property! {
}

/// A value for the [background-repeat](https://www.w3.org/TR/css-backgrounds-3/#background-repeat) property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BackgroundRepeat {
/// A repeat style for the x direction.
Expand Down Expand Up @@ -299,7 +302,8 @@ impl ToCss for BackgroundPosition {
}

/// A value for the [background](https://www.w3.org/TR/css-backgrounds-3/#background) shorthand property.
#[derive(Debug, Clone, PartialEq, Visit)]
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "visitor", derive(Visit))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Background<'i> {
/// The background image.
Expand Down
Loading

0 comments on commit 560b784

Please sign in to comment.