Skip to content
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

allow to define the prefix for generated names #225

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silver-hairs-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"yak-swc": minor
---

allow to define the prefix for generated names
jantimon marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 11 additions & 2 deletions packages/yak-swc/yak_swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub struct Config {
/// To ensure that the hash is consistent accross multiple systems the relative path
/// from the base dir to the source file is used.
pub base_path: String,
/// Prefix for the generated css identifier
pub prefix: Option<String>,
}

pub struct TransformVisitor<GenericComments>
Expand Down Expand Up @@ -106,7 +108,12 @@ impl<GenericComments> TransformVisitor<GenericComments>
where
GenericComments: Comments,
{
pub fn new(comments: Option<GenericComments>, filename: String, dev_mode: bool) -> Self {
pub fn new(
comments: Option<GenericComments>,
filename: String,
dev_mode: bool,
prefix: Option<String>,
) -> Self {
Self {
current_css_state: None,
current_declaration: vec![],
Expand All @@ -115,7 +122,7 @@ where
current_exported: false,
variables: VariableVisitor::new(),
yak_library_imports: YakImportVisitor::new(),
naming_convention: NamingConvention::new(filename.clone(), dev_mode),
naming_convention: NamingConvention::new(filename.clone(), dev_mode, prefix),
variable_name_selector_mapping: FxHashMap::default(),
expression_replacement: None,
css_module_identifier: None,
Expand Down Expand Up @@ -959,6 +966,7 @@ pub fn process_transform(program: Program, metadata: TransformPluginProgramMetad
metadata.comments,
deterministic_path,
config.dev_mode,
config.prefix,
)))
}

Expand Down Expand Up @@ -997,6 +1005,7 @@ mod tests {
Some(tester.comments.clone()),
"path/input.tsx".to_string(),
true,
None,
))
},
&input,
Expand Down
30 changes: 22 additions & 8 deletions packages/yak-swc/yak_swc/src/naming_convention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ pub struct NamingConvention {
file_name: String,
file_name_hash: Option<String>,
dev_mode: bool,
prefix: String,
}

/// A naming convention that generates safe unique names for CSS variables, classes, and other identifiers.
/// Use the `generate_unique_name` method to generate a unique name based on a base name.
/// e.g. `generate_unique_name("foo bar")` might return `"foo_bar-01"`, `"foo_bar-02"`, etc.
impl NamingConvention {
pub fn new(file_name: String, dev_mode: bool) -> Self {
pub fn new(file_name: String, dev_mode: bool, prefix: Option<String>) -> Self {
Self {
postfix_counters: FxHashMap::default(),
file_name,
file_name_hash: None,
dev_mode,
prefix: prefix.unwrap_or_else(|| {
if dev_mode {
// In dev mode we don't prefix by default as it already uses long unique names
String::from("")
} else {
String::from("y")
}
}),
}
}

Expand Down Expand Up @@ -69,14 +78,19 @@ impl NamingConvention {
pub fn get_css_variable_name(&mut self, base_name: &str) -> String {
let name: String = if self.dev_mode {
if base_name.is_empty() {
String::from("yak_")
String::from("var_")
} else {
format!("{}_", base_name)
}
} else {
String::from("y")
"".to_string()
};
let css_variable_name = format!("{}{}", name, self.get_file_name_hash());
let css_variable_name = format!(
"{}{}{}",
self.prefix.clone(),
name,
self.get_file_name_hash()
);
self.generate_unique_name(&css_variable_name)
}
}
Expand Down Expand Up @@ -142,25 +156,25 @@ mod tests {

#[test]
fn css_naming_convention() {
let mut convention = NamingConvention::new("file.css".into(), true);
let mut convention = NamingConvention::new("file.css".into(), true, None);
assert_eq!(convention.generate_unique_name("foo"), "foo");
assert_eq!(convention.generate_unique_name("foo"), "foo-01");
assert_eq!(convention.generate_unique_name("foo"), "foo-02");
}

#[test]
fn css_variable_name() {
let mut convention = NamingConvention::new("file.css".into(), false);
let mut convention = NamingConvention::new("file.css".into(), false, None);
assert_eq!(convention.get_css_variable_name("foo"), "yoPBkbU");
assert_eq!(convention.get_css_variable_name("foo"), "yoPBkbU1");
assert_eq!(convention.get_css_variable_name(""), "yoPBkbU2");
}

#[test]
fn css_variable_name_dev_mode() {
let mut convention = NamingConvention::new("file.css".into(), true);
let mut convention = NamingConvention::new("file.css".into(), true, None);
assert_eq!(convention.get_css_variable_name("foo"), "foo_oPBkbU");
assert_eq!(convention.get_css_variable_name("foo"), "foo_oPBkbU-01");
assert_eq!(convention.get_css_variable_name(""), "yak_oPBkbU");
assert_eq!(convention.get_css_variable_name(""), "var_oPBkbU");
}
}
Loading