Skip to content

Commit

Permalink
fix: removal of unecessary underscore logic in escape job #8735
Browse files Browse the repository at this point in the history
  • Loading branch information
daveskybet committed Jan 7, 2025
1 parent eb7595d commit 116cba0
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 133 deletions.
7 changes: 1 addition & 6 deletions crates/rspack_plugin_css/src/dependency/local_ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ impl DependencyTemplate for CssLocalIdentDependency {
source: &mut TemplateReplaceSource,
_code_generatable_context: &mut TemplateContext,
) {
source.replace(
self.start,
self.end,
&escape_css(&self.local_ident, false),
None,
);
source.replace(self.start, self.end, &escape_css(&self.local_ident), None);
}

fn dependency_id(&self) -> Option<DependencyId> {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_css/src/dependency/self_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl DependencyTemplate for CssSelfReferenceLocalIdentDependency {
source.replace(
replace.start,
replace.end,
&escape_css(&replace.local_ident, false),
&escape_css(&replace.local_ident),
None,
);
}
Expand Down
14 changes: 3 additions & 11 deletions crates/rspack_plugin_css/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ use crate::parser_and_generator::CssExport;
pub const AUTO_PUBLIC_PATH_PLACEHOLDER: &str = "__RSPACK_PLUGIN_CSS_AUTO_PUBLIC_PATH__";
pub static LEADING_DIGIT_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^((-?[0-9])|--)").expect("Invalid regexp"));
pub static PREFIX_UNDERSCORE_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^[0-9_-]").expect("Invalid regexp"));

#[derive(Debug, Clone)]
pub struct LocalIdentOptions<'a> {
Expand Down Expand Up @@ -116,16 +114,10 @@ impl LocalIdentNameRenderOptions<'_> {
static UNESCAPE_CSS_IDENT_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"([^a-zA-Z0-9_\u0081-\uffff-])").expect("invalid regex"));

pub fn escape_css(s: &str, omit_optional_underscore: bool) -> Cow<str> {
pub fn escape_css(s: &str) -> Cow<str> {
let escaped = UNESCAPE_CSS_IDENT_REGEX.replace_all(s, |s: &Captures| format!("\\{}", &s[0]));
if !omit_optional_underscore
&& !escaped.starts_with("--")
&& PREFIX_UNDERSCORE_REGEX.is_match(&escaped)
{
format!("_{}", escaped).into()
} else {
escaped
}

escaped
}

pub(crate) fn export_locals_convention(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`config config/builtin-lightningcss-loader/basic-include exported tests should transform css correct 1`] = `
body {
& ._-_6ec1c834ac8cc99e-used {
& .-_6ec1c834ac8cc99e-used {
color: #00f;
}
}
Expand Down
Loading

0 comments on commit 116cba0

Please sign in to comment.