scoped-sass
Procedural macros for scoped Sass in Leptos apps.
This crate scans .scss/.sass files, compiles them with scoped-sass-core, rewrites class names with deterministic suffixes, and generates Rust modules you can use from components.
Install
[]
= "0.1.0"
The package name is scoped-sass, but in Rust code you import it as scoped_sass.
Macros
scoped_scss!
Compile one explicit Sass file:
scoped_scss!
scoped_scss_auto!
Scan a source directory recursively and generate one module per file:
scoped_scss_auto!;
scoped_sass_auto!
Alias of scoped_scss_auto! (same behavior, shorter name).
Options (scoped_scss_auto! / scoped_sass_auto!)
inject = <bool>Iftrue,app_styles()returns inline<style>tags with generated CSS.output_file = "<path>"Writes all generated CSS into a single file (relative toCARGO_MANIFEST_DIR).href = "<url>"Optional URL used byapp_styles()wheninject = false. If omitted, it defaults to"/" + output_file.
Generated items
The auto macros generate:
- a rooted namespace:
pub mod scoped { ... } - one Rust module per Sass file under path-based namespaces:
src/components/card.scss->scoped::components::cardsrc/views/protected/home.scss->scoped::views::protected::home
cls!(...)helper macro (crate-local) for joining classes from strings andOption<...>global_styles()(inline styles only wheninject = true)app_styles()(high-level style entrypoint for app root)
Inside each generated module:
CSS: &'static strSUFFIX: &'static strclasses::<class_name>constants mapped to transformed scoped class names
Suggested app usage
Declare the macro in your app entry module:
use scoped_sass_auto;
scoped_sass_auto!;
Render styles once near the root:
Ok
For colocated files (card.rs + card.scss), re-export classes inside the module:
// src/components/card.rs
pub use crateclasses;
Then usage stays lightweight:
use crateclasses;
For Sass modules without a matching .rs file, add a module in mod.rs:
// src/components/mod.rs
Then:
use crateclasses;
Notes
- The macro includes Sass files as tracked dependencies so recompilation happens when imports change.
- Rust Analyzer snapshots are kept under
target/scoped_sass_cache/generated_rust(andtarget/rust-analyzer/...) so consumers don't need asrc/.scopedfolder.