### Install and run `stylance-cli` for one-time CSS bundling Source: https://context7.com/basro/stylance-rs/llms.txt Install the `stylance-cli` using Cargo. Use it to discover, rename classes with hashes, and bundle `.module.css`/`.module.scss` files into a single output file or directory. Supports multiple crate paths. ```bash # Install car go install stylance-cli # Single crate → single bundled output file stylance ./my-crate --output-file ./styles/bundle.css # Single crate → output directory (better for SASS @use imports) stylance ./my-crate --output-dir ./styles/ # Creates: ./styles/stylance/_index.scss (imports all modules) # ./styles/stylance/card.module-f45126d.scss # ./styles/stylance/nav.module-aa2341b.scss # Multiple crates with a shared output file (outputs are concatenated, not overwritten) stylance . ./components/crate1 ./components/crate2 --output-file ./dist/bundle.css # Override search folders via CLI flag (can be repeated) stylance ./my-crate --output-file ./out.css --folder src/ --folder extra-styles/ ``` -------------------------------- ### Install Stylance CLI Source: https://github.com/basro/stylance-rs/blob/main/README.md Install the stylance command-line interface tool globally using Cargo. ```cli cargo install stylance-cli ``` -------------------------------- ### Workspace Configuration Inheritance Example Source: https://context7.com/basro/stylance-rs/llms.txt Demonstrates how a crate can inherit and override settings from a workspace's `[workspace.metadata.stylance]` configuration. Package-level settings take precedence over workspace settings. ```toml # /my_project/Cargo.toml (workspace root) [workspace.metadata.stylance] extensions = [".module.scss", ".module.css"] output_file = "./dist/bundle.css" hash_len = 8 # /my_project/crate1/Cargo.toml [package.metadata.stylance] workspace = true # Override only extensions; output_file and hash_len are inherited from workspace. extensions = [".module.css"] # Effective config for crate1: # extensions = [".module.css"] (crate1 overrides workspace) # output_file = "/my_project/dist/bundle.css" (from workspace, resolved against workspace root) # hash_len = 8 (inherited from workspace) # hash_root_path = "/my_project/" (defaults to workspace root when workspace = true) ``` -------------------------------- ### Stylance Configuration Example Source: https://github.com/basro/stylance-rs/blob/main/README.md Example of stylance configuration settings within a Cargo.toml file. These settings control file extensions, output paths, and hash lengths for CSS processing. ```toml [workspace.metadata.stylance] extensions = [".module.scss", ".module.css"] output_file = "./out.css" hash_len = 8 ``` -------------------------------- ### Stylance Workspace Configuration Inheritance Example Source: https://github.com/basro/stylance-rs/blob/main/README.md Enable workspace configuration inheritance by setting `workspace = true` in the package's metadata. This allows merging configurations from the workspace's Cargo.toml, with package settings taking priority. ```toml # ... [package.metadata.stylance] workspace = true extensions = [".module.css"] ``` -------------------------------- ### Run `stylance-cli --watch` to rebuild on file changes Source: https://context7.com/basro/stylance-rs/llms.txt Execute `stylance` in watch mode to automatically rebuild CSS modules when `.module.css`/`.module.scss` files or `Cargo.toml` change. Changes are debounced with a 50 ms window, and configuration is reloaded automatically. ```bash # Watch a single crate stylance --watch ./my-crate --output-file ./styles/bundle.css # Watch multiple crates simultaneously with a shared output stylance --watch . ./components/button ./components/card --output-file ./dist/all.css # Watch with output directory stylance --watch ./my-crate --output-dir ./styles/ ``` -------------------------------- ### Run Stylance CLI with Output Directory Source: https://github.com/basro/stylance-rs/blob/main/README.md Configure stylance to output transformed CSS modules into a specified directory, preserving individual files and including an `_index.scss` for SASS project integration. ```bash stylance ./path/to/crate/dir/ --output-dir ./styles/ ``` -------------------------------- ### Run Stylance CLI to Bundle CSS Source: https://github.com/basro/stylance-rs/blob/main/README.md Use the stylance CLI to bundle all `.module.scss` and `.module.css` files into a single output CSS file. By default, it scans the `./src/` directory. ```cli stylance ./path/to/crate/dir/ --output-file ./bundled.scss ``` -------------------------------- ### Build Multiple Crates Simultaneously with Stylance CLI Source: https://github.com/basro/stylance-rs/blob/main/README.md Pass multiple crate paths to the stylance CLI to build them at once. This is more efficient than running stylance for each crate individually. ```cli stylance . ./components/crate1 ./components/crate2 ./components/crate3 ``` -------------------------------- ### Watch Multiple Crates with Stylance CLI Source: https://github.com/basro/stylance-rs/blob/main/README.md Combine the --watch option with multiple crate paths to monitor changes across several crates and automatically rebuild. ```cli stylance --watch . ./components/crate1 ./components/crate2 ./components/crate3 ``` -------------------------------- ### Use `stylance-cli` programmatic API in build scripts Source: https://context7.com/basro/stylance-rs/llms.txt Integrate Stylance into build scripts (e.g., `build.rs`) using the `run` or `run_silent` functions. `run` prints processed file paths, while `run_silent` accepts a callback for custom handling without stdout output. ```rust // build.rs or a custom build tool use stylance_cli::{run, run_silent}; use stylance_core::{Config, PartialConfig}; fn main() -> anyhow::Result<()> { let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); let config = Config::from_partials( manifest_dir.into(), PartialConfig { output_file: Some("./styles/bundle.css".into()), ..Default::default() }, None, // no workspace config )?; // Prints each processed file path to stdout run(&config)?; // Or: collect processed paths without printing let mut processed = Vec::new(); run_silent(&config, |path| { processed.push(path.to_path_buf()); })?; println!("Processed {} CSS modules", processed.len()); Ok(()) } ``` -------------------------------- ### Handle Shared Output Files with Stylance CLI Source: https://github.com/basro/stylance-rs/blob/main/README.md When building multiple crates to the same output file, call stylance once with all crate paths to correctly concatenate outputs. Calling stylance multiple times with the same output file will result in overwriting. ```cli stylance ./crate1 --output_file out.css # out.css is overwritten here, crate1 output is lost stylance ./crate2 --output_file out.css ``` ```cli stylance ./crate1 ./crate2 --output_file out.css # out.css will combine both crate1 and crate2 outputs. ``` -------------------------------- ### Import Crate Style Macro Source: https://github.com/basro/stylance-rs/blob/main/README.md Use the `import_crate_style!` macro to import CSS classes as constants. The path is relative to the crate's manifest directory. Unused classes will generate warnings, which can be managed with `#[allow(dead_code)]` or `#[deny(dead_code)]` attributes. ```rust stylance::import_crate_style!(my_style, "src/component/card/card.module.scss"); ``` ```rust stylance::import_style!(my_style2, "card.module.scss"); ``` ```rust println!("{} {}", my_style::header, my_style2::content) ``` ```rust import_crate_style!(#[allow(dead_code)] my_style, "src/component/card/card.module.scss"); ``` ```rust import_crate_style!(#[deny(dead_code)] my_style, "src/component/card/card.module.scss"); ``` -------------------------------- ### Stylance Configuration in Cargo.toml Source: https://github.com/basro/stylance-rs/blob/main/README.md Configure Stylance behavior by adding settings under the [package.metadata.stylance] section in your Cargo.toml file. All settings are optional. ```toml [package.metadata.stylance] # output_file # When set, stylance-cli will bundle all css module files # by concatenating them and put the result in this file. output_file = "./styles/bundle.scss" # output_dir # When set, stylance-cli will create a folder named "stylance" inside # the output_dir directory. # The stylance folder will be populated with one file per detected css module # and one _all.scss file that contains one `@use "file.module-hash.scss";` statement # per module file. # You can use that file to import all your modules into your main scss project. output_dir = "./styles/" # folders # folders in which stylance cli will look for css module files. # defaults to ["./src/"] folders = ["./src/", "./styles/"] # extensions # files ending with these extensions will be considered to be # css modules by stylance cli and will be included in the output # bundle # defaults to [".module.scss", ".module.css"] extensions = [".module.scss", ".module.css"] # scss_prelude # When generating an scss file stylance-cli will prepend this string # Useful to include a @use statement to all scss modules. scss_prelude = '@use "../path/to/prelude" as *;' # hash_len # Controls how long the hash name used in scoped classes should be. # It is safe to lower this as much as you want, stylance cli will produce an # error if two files end up with colliding hashes. # defaults to 7 hash_len = 7 # class_name_pattern # Controls the shape of the transformed scoped class names. # [name] will be replaced with the original class name # [hash] will be replaced with the hash of css module file path. # defaults to "[name]-[hash]" class_name_pattern = "my-project-[name]-[hash]" # hash_root_path # Specifies the base path that will be used when computing the relative paths # of css modules. (The relative paths are hashed to generate the module's hash) # [name] will be replaced with the original class name # [hash] will be replaced with the hash of css module file path. # defaults to "." hash_root_path = "../../" # workspace # Set to true to enable inheriting stylance configuration from the crate's # Enabling it will also change the default hash_root_path to be the workspace's directory. # defaults to false workspace = true ``` -------------------------------- ### classes! Macro Source: https://context7.com/basro/stylance-rs/llms.txt A utility macro that joins any number of class name expressions into a space-separated string. It accepts `&str`, `&String`, and any `Option` where `T: AsRef`. `None` values are silently skipped, and `Option`s are evaluated to conditionally include classes. Returns an empty string when called with no arguments. ```APIDOC ## `classes!` — Combine multiple class names into a single string A utility macro that joins any number of class name expressions into a space-separated string. Accepts `&str`, `&String`, and any `Option` where `T: AsRef`. `None` values are silently skipped, making conditional classes ergonomic. Returns an empty string when called with no arguments. ```rust use stylance::{classes, import_crate_style}; import_crate_style!(my_style, "src/components/nav/nav.module.scss"); import_crate_style!(base_style, "src/base.module.scss"); fn nav_class(is_active: bool, is_disabled: bool) -> String { classes!( "global-nav", // plain &str always included my_style::nav_item, // scoped class always included base_style::rounded, // another scoped class is_active.then_some(my_style::active), // Some("active-abc1234") or None (!is_disabled).then_some(my_style::clickable), // conditional if is_disabled { Some("aria-disabled") } else { None } ) // e.g. "global-nav nav-item-f45126d rounded-aa2341b active-f45126d clickable-f45126d" } // Empty call: assert_eq!(classes!(), ""); // Trailing comma is allowed: let c = classes!("one", "two", "three",); assert_eq!(c, "one two three"); ``` ``` -------------------------------- ### import_style! Macro Source: https://context7.com/basro/stylance-rs/llms.txt Imports scoped class names from a CSS/SCSS module file. This macro is identical to `import_crate_style!` except the CSS file path is resolved relative to the Rust source file that calls the macro, making it convenient for co-located component files. Supports the same optional attributes and `pub` visibility modifier. ```APIDOC ## `import_style!` — Import scoped class names (file-relative path) Identical to `import_crate_style!` except the CSS file path is resolved relative to the Rust source file that calls the macro, making it convenient for co-located component files. Supports the same optional attributes and `pub` visibility modifier. Note: Rust Analyzer does not currently provide completions for this variant because it cannot resolve caller-relative file paths from the IDE. ```rust // src/components/card/card.rs // style is located at: src/components/card/card.module.scss stylance::import_style!(my_style, "card.module.scss"); // With attributes stylance::import_style!(#[deny(dead_code)] pub strict_style, "card.module.scss"); fn use_classes() -> &'static str { my_style::header // "header-f45126d" } ``` ``` -------------------------------- ### Watch for CSS Changes with Stylance CLI Source: https://github.com/basro/stylance-rs/blob/main/README.md Use the --watch option to automatically rebuild output files when .module.css or .module.scss files change. This is convenient for development. ```cli stylance --watch --output-file ./bundled.scss ./path/to/crate/dir/ ``` -------------------------------- ### Combine Multiple Class Names Source: https://context7.com/basro/stylance-rs/llms.txt The `classes!` macro joins class name expressions into a space-separated string. It accepts `&str`, `&String`, and `Option` where `T: AsRef`. `None` values are skipped, and an empty string is returned if no arguments are provided. ```rust use stylance::{classes, import_crate_style}; import_crate_style!(my_style, "src/components/nav/nav.module.scss"); import_crate_style!(base_style, "src/base.module.scss"); fn nav_class(is_active: bool, is_disabled: bool) -> String { classes!( "global-nav", // plain &str always included my_style::nav_item, // scoped class always included base_style::rounded, // another scoped class is_active.then_some(my_style::active), // Some("active-abc1234") or None (!is_disabled).then_some(my_style::clickable), // conditional if is_disabled { Some("aria-disabled") } else { None } ) // e.g. "global-nav nav-item-f45126d rounded-aa2341b active-f45126d clickable-f45126d" } // Empty call: assert_eq!(classes!(), ""); // Trailing comma is allowed: let c = classes!("one", "two", "three",); assert_eq!(c, "one two three"); ``` -------------------------------- ### import_crate_style! Macro Source: https://context7.com/basro/stylance-rs/llms.txt Imports scoped class names from a CSS/SCSS module file. The file path is resolved relative to the crate's Cargo.toml directory. CSS class names with hyphens are mapped to Rust identifiers by replacing '-' with '_'. ```APIDOC ## `import_crate_style!` — Import scoped class names (crate-relative path) Reads a CSS/SCSS module file at compile time and expands into a Rust module whose public constants are the hashed class names. The file path is resolved relative to the crate's `Cargo.toml` directory. CSS class names that contain hyphens are mapped to Rust identifiers by replacing `-` with `_`. Using a non-existent class causes a compile error; unused classes produce warnings (suppressible with `#[allow(dead_code)]`). ```rust // src/components/card/card.rs // Path is relative to the directory containing Cargo.toml stylance::import_crate_style!(my_style, "src/components/card/card.module.scss"); // With pub visibility and dead_code suppression stylance::import_crate_style!(#[allow(dead_code)] pub card_style, "src/components/card/card.module.scss"); fn render_card() -> String { // my_style::header expands to "header-f45126d" at compile time format!(r"
{{}}
", my_style::header, my_style::contents) } // card.module.scss: // .header { background-color: red; } // .contents { border: 1px solid black; } // // Macro expands to: // mod my_style { // pub const header: &str = "header-f45126d"; // pub const contents: &str = "contents-f45126d"; // } ``` ``` -------------------------------- ### Import Scoped Class Names (File-Relative) Source: https://context7.com/basro/stylance-rs/llms.txt Use `import_style!` for paths relative to the calling Rust source file, useful for co-located component files. Supports the same optional attributes and `pub` visibility modifier as `import_crate_style!`. ```rust // src/components/card/card.rs // style is located at: src/components/card/card.module.scss stylance::import_style!(my_style, "card.module.scss"); // With attributes stylance::import_style!(#[deny(dead_code)] pub strict_style, "card.module.scss"); fn use_classes() -> &'static str { my_style::header // "header-f45126d" } ``` -------------------------------- ### Import Scoped Class Names (Crate-Relative) Source: https://context7.com/basro/stylance-rs/llms.txt Use `import_crate_style!` to read a CSS/SCSS module file at compile time. The file path is resolved relative to the crate's `Cargo.toml` directory. CSS class names with hyphens are mapped to Rust identifiers by replacing `-` with `_`. ```rust // src/components/card/card.rs // Path is relative to the directory containing Cargo.toml stylance::import_crate_style!(my_style, "src/components/card/card.module.scss"); // With pub visibility and dead_code suppression stylance::import_crate_style!(#[allow(dead_code)] pub card_style, "src/components/card/card.module.scss"); fn render_card() -> String { // my_style::header expands to "header-f45126d" at compile time format!(r"
{}
", my_style::header, my_style::contents) } // card.module.scss: // .header { background-color: red; } // .contents { border: 1px solid black; } // // Macro expands to: // mod my_style { // pub const header: &str = "header-f45126d"; // pub const contents: &str = "contents-f45126d"; // } ``` -------------------------------- ### Rust Analyzer Settings for Ignored Macros Source: https://github.com/basro/stylance-rs/blob/main/README.md Configuration for Rust Analyzer to ignore specific proc macros, potentially resolving completion issues. This setting is added to `.vscode/settings.json`. ```json { "rust-analyzer.procMacro.ignored": { "stylance": ["import_style_classes"] } } ``` -------------------------------- ### Join class name tuples with `JoinClasses` trait Source: https://context7.com/basro/stylance-rs/llms.txt Use the `JoinClasses` trait on tuples or slices to join class names, similar to the `classes!` macro. Elements can be `&str`, `&String`, or `Option>`. `None` values are filtered out. ```rust use stylance::{import_crate_style, JoinClasses}; import_crate_style!(style, "src/components/button/button.module.scss"); fn button_classes(variant: &str, loading: bool) -> String { ( "btn", // &str style::base, // scoped constant Some(variant), // Option<&str> — always Some here, but type is optional loading.then_some(style::loading), // None when not loading loading.then_some("aria-busy"), ) .join_classes() // "btn base-f45126d primary loading-f45126d aria-busy" } ``` ```rust use stylance::internal::MaybeStr; let parts: &[MaybeStr] = &[ "header".into(), Some("active").into(), None::<&str>.into(), ]; let joined = parts.join_classes(); assert_eq!(joined, "header active"); ``` -------------------------------- ### CSS with Global Classname Source: https://github.com/basro/stylance-rs/blob/main/README.md Target global classnames by wrapping them with `:global()`. The module hash is applied to scoped classes, while global classes remain unchanged. ```css .my_scoped_class :global(.paragraph) { color: red; } ``` -------------------------------- ### Add Stylance Dependency Source: https://github.com/basro/stylance-rs/blob/main/README.md Add stylance as a dependency to your Rust project using Cargo. ```cli cargo add stylance ``` -------------------------------- ### Opt out of scoping with `:global()` in SCSS modules Source: https://context7.com/basro/stylance-rs/llms.txt Wrap class selectors with `:global()` in `.module.scss` files to prevent Stylance from adding a hash suffix to them. This is useful for targeting global utility classes without scoping. ```scss /* src/components/card/card.module.scss */ .card { padding: 1rem; } /* Target a global utility class without scoping it */ .card :global(.shadow-lg) { box-shadow: 0 4px 6px rgba(0,0,0,.1); } /* Bundled output produced by stylance-cli: .card-f45126d { padding: 1rem; } .card-f45126d .shadow-lg { box-shadow: 0 4px 6px rgba(0,0,0,.1); } */ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.