-
Notifications
You must be signed in to change notification settings - Fork 195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Skip generating unnecessary @supports
#878
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,11 @@ | |
|
||
#![allow(missing_docs)] | ||
|
||
use std::borrow::Borrow; | ||
|
||
use crate::vendor_prefix::VendorPrefix; | ||
use bitflags::bitflags; | ||
use indexmap::IndexSet; | ||
#[cfg(any(feature = "serde", feature = "nodejs"))] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
|
@@ -136,7 +139,7 @@ fn parse_version(version: &str) -> Option<u32> { | |
|
||
bitflags! { | ||
/// Features to explicitly enable or disable. | ||
#[derive(Debug, Default, Clone, Copy)] | ||
#[derive(Debug, Default, Clone, Copy, Hash, Eq, PartialEq)] | ||
pub struct Features: u32 { | ||
const Nesting = 1 << 0; | ||
const NotSelectorList = 1 << 1; | ||
|
@@ -165,6 +168,26 @@ bitflags! { | |
} | ||
} | ||
|
||
pub(crate) trait FeaturesIterator: Sized + Iterator { | ||
fn union_all<T>(self) -> Features | ||
where | ||
Self: Iterator<Item = T>, | ||
T: Borrow<Features>, | ||
{ | ||
self.fold(Features::empty(), |a, b| a | *b.borrow()) | ||
} | ||
|
||
fn try_union_all<T>(&mut self) -> Option<Features> | ||
where | ||
Self: Iterator<Item = Option<T>>, | ||
T: Borrow<Features>, | ||
{ | ||
self.try_fold(Features::empty(), |a, b| b.map(|b| a | *b.borrow())) | ||
} | ||
} | ||
|
||
impl<I> FeaturesIterator for I where I: Iterator {} | ||
|
||
/// Target browsers and features to compile. | ||
#[derive(Debug, Clone, Copy, Default)] | ||
pub struct Targets { | ||
|
@@ -225,6 +248,43 @@ impl Targets { | |
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct TargetsWithSupportsScope { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this just be part of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean putting the fields in |
||
input_targets: Targets, | ||
supports_scope_features: IndexSet<Features>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could probably get away with just a Vec here. Since fn enter_supports(&mut self, features: Features) -> bool {
if features.is_empty() || self.current.exclude.contains(features) {
// Already excluding all features
return false;
}
self.stack.push(supported);
self.current.exclude.insert(features);
true
}
fn exit_supports(&mut self) {
if let Some(last) = self.stack.pop() {
self.current.exclude.remove(last);
}
} Then you wouldn't need to iterate over the stack each time and could avoid the hashing cost of IndexSet. Do you think this would work or am I missing something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true! I updated the code to use |
||
pub(crate) current: Targets, | ||
} | ||
|
||
impl TargetsWithSupportsScope { | ||
pub fn new(targets: Targets) -> Self { | ||
Self { | ||
input_targets: targets, | ||
supports_scope_features: IndexSet::new(), | ||
current: targets, | ||
} | ||
} | ||
|
||
fn recalculate(&mut self) { | ||
self.current.exclude = self.input_targets.exclude | self.supports_scope_features.iter().union_all(); | ||
} | ||
|
||
/// Returns true if inserted | ||
pub fn enter_supports(&mut self, supported_features: Features) -> bool { | ||
if supported_features.is_empty() { | ||
return false; | ||
} | ||
let inserted = self.supports_scope_features.insert(supported_features); | ||
self.recalculate(); | ||
inserted | ||
} | ||
|
||
/// Should be only called if inserted | ||
pub fn exit_supports(&mut self) { | ||
self.supports_scope_features.pop(); | ||
self.recalculate(); | ||
} | ||
} | ||
|
||
macro_rules! should_compile { | ||
($targets: expr, $feature: ident) => { | ||
$targets.should_compile( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would only work if the value is an exact match. Perhaps a future enhancement would be to attempt to parse the value, and extract the features from that. For example, if any
lab()
color was used, set that feature rather than onlylab(0% 0 0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it would be nice to expand this detection in future.