breaking: gate 'styly!(const scope...' behind the const-scoping feature.\nFor some reason it did only become a problem recently, but it appears that having lightningcss/grass unconditionally will break WASM builds by having getrandom dependency

This commit is contained in:
yiffyrusdev 2025-05-08 19:42:06 +05:00
commit 80890500c9
No known key found for this signature in database
GPG key ID: FB83DFEAFB3F4709
5 changed files with 150 additions and 97 deletions

View file

@ -1,16 +1,8 @@
use std::fmt::Debug;
use lightningcss::{
error::{MinifyErrorKind, PrinterErrorKind},
printer::PrinterOptions,
stylesheet::MinifyOptions,
targets::Targets,
};
use crate::{config::SabryConfig, syntax::ostrta::OneSyntaxToRuleThemAll};
use crate::config::SabryConfig;
/// Convenience wrapper on Grass and Lightningcss
pub struct CompilerAdapter {
#[allow(dead_code)] // without 'compile' feature, still want to set this
config: SabryConfig,
}
@ -21,9 +13,10 @@ impl CompilerAdapter {
/// Compile given SASS/SCSS into CSS
/// with respect to self.config
#[cfg(feature = "compile")]
pub fn compile_module(
&self,
syntax: OneSyntaxToRuleThemAll,
syntax: crate::syntax::ostrta::OneSyntaxToRuleThemAll,
code: &str,
) -> Result<String, SabryCompilerError> {
let options = grass::Options::from(&self.config).input_syntax(syntax.into());
@ -35,7 +28,10 @@ impl CompilerAdapter {
/// Perform lightningcss transformations on given css
/// with respect to self.config.css.minify on minification
#[cfg(feature = "compile")]
pub fn lightningcss(&self, css: &str) -> Result<String, SabryCompilerError> {
use lightningcss::{printer::PrinterOptions, stylesheet::MinifyOptions, targets::Targets};
let mut lightsheet = match lightningcss::stylesheet::StyleSheet::parse(
css,
lightningcss::stylesheet::ParserOptions::default(),
@ -81,6 +77,7 @@ impl CompilerAdapter {
}
}
#[cfg(feature = "compile")]
impl From<&SabryConfig> for grass::Options<'_> {
fn from(config: &SabryConfig) -> Self {
let mut options = grass::Options::default();
@ -95,14 +92,15 @@ impl From<&SabryConfig> for grass::Options<'_> {
}
}
#[cfg(feature = "compile")]
#[derive(thiserror::Error)]
pub enum SabryCompilerError {
#[error("Could not compile into CSS with grass")]
GrassCompile(#[from] Box<grass::Error>),
#[error("Could not print compiled CSS with lightningcss")]
LightPrint(#[from] lightningcss::error::Error<PrinterErrorKind>),
LightPrint(#[from] lightningcss::error::Error<lightningcss::error::PrinterErrorKind>),
#[error("Could not minify CSS with lightningcss")]
LightMinify(#[from] lightningcss::error::Error<MinifyErrorKind>),
LightMinify(#[from] lightningcss::error::Error<lightningcss::error::MinifyErrorKind>),
#[error("Could not parse CSS with lightningcss")]
LightParse {
kind: String,
@ -110,7 +108,8 @@ pub enum SabryCompilerError {
},
}
impl Debug for SabryCompilerError {
#[cfg(feature = "compile")]
impl std::fmt::Debug for SabryCompilerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let explain = match self {
Self::LightPrint(e) => format!(

View file

@ -199,6 +199,7 @@ impl SabryLightTargets {
}
}
#[cfg(feature = "compile")]
impl From<SabryLightTargets> for lightningcss::targets::Browsers {
fn from(value: SabryLightTargets) -> Self {
Self {

View file

@ -1,15 +1,14 @@
use base64::Engine;
use crate::config::SabryHashConfig;
use super::{apply_basic_rusty_member_gen_rules, ArbitraryScope};
/// Convenience wrapper for String-being-a-hash
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ScopeHash(String);
impl ScopeHash {
pub fn new(scope: &ArbitraryScope, config: &SabryHashConfig) -> Self {
#[cfg(feature = "scope")]
pub fn new(scope: &super::ArbitraryScope, config: &crate::config::SabryHashConfig) -> Self {
use base64::Engine;
use crate::scoper::apply_basic_rusty_member_gen_rules;
let mut hasher = blake3::Hasher::new();
if config.use_scope_name {

View file

@ -3,12 +3,8 @@ use std::fmt::Debug;
use cfg_if::cfg_if;
use hash::ScopeHash;
use raffia::{Span, Spanned};
use regex::Regex;
use crate::{
config::SabryHashConfig,
syntax::{ostrta::OneSyntaxToRuleThemAll, StylesheetAdapter},
};
use crate::syntax::{ostrta::OneSyntaxToRuleThemAll, StylesheetAdapter};
pub mod hash;
@ -56,7 +52,11 @@ impl<'s> ArbitraryScope<'s> {
/// Consume arbitrary scope and create a [HashedScope] in its basis
///
/// This function calls [HashedScope::new] under the hood
pub fn hashed(self, config: &SabryHashConfig) -> Result<HashedScope<'s>, ScopeError> {
#[cfg(feature = "scope")]
pub fn hashed(
self,
config: &crate::config::SabryHashConfig,
) -> Result<HashedScope<'s>, ScopeError> {
let hash = ScopeHash::new(&self, config);
HashedScope::new(hash, self)
}
@ -332,6 +332,7 @@ impl ScopedSelector {
}
/// Generate rusty member ident based on selector type and CSS ident
#[cfg(feature = "scope")]
pub fn gen_rusty_ident(&self) -> Option<syn::Ident> {
let arb = &self.as_arbitrary().ident;
let basic = apply_basic_rusty_member_gen_rules(arb);
@ -401,8 +402,9 @@ impl ScopedSelector {
}
}
#[cfg(feature = "scope")]
pub fn apply_basic_rusty_member_gen_rules(source: &str) -> String {
let omit_regex = Regex::new(r"(^\-)|(\-$)|[^a-zA-Z0-9\-\_]")
let omit_regex = regex::Regex::new(r"(^\-)|(\-$)|[^a-zA-Z0-9\-\_]")
.expect("BUG: can not build omition regex for rusty member generation");
// omit forbidden chars

View file

@ -34,6 +34,7 @@ impl TryFrom<raffia::Syntax> for OneSyntaxToRuleThemAll {
}
}
#[cfg(feature = "compile")]
impl TryFrom<grass::InputSyntax> for OneSyntaxToRuleThemAll {
type Error = ();
fn try_from(value: grass::InputSyntax) -> Result<Self, Self::Error> {
@ -54,6 +55,7 @@ impl From<OneSyntaxToRuleThemAll> for raffia::Syntax {
}
}
#[cfg(feature = "compile")]
impl From<OneSyntaxToRuleThemAll> for grass::InputSyntax {
fn from(value: OneSyntaxToRuleThemAll) -> Self {
match value {