Stylance
Stylance is a library and tool for working with scoped CSS in rust inspired by CSS modules.
Usage
Stylance is divided in two parts:
- Rust proc macros for importing scoped/hashed class names from css files as constants into your rust code.
- A cli tool that finds all css modules in your crate and generates an output css file with hashed class names.
Proc macro
Add stylance to your rust cargo.toml:
cargo add stylance
Then use the import_crate_style proc read a css/scss file and bring the classes from within that file as constants.
css/scss file:
/* src/component/card/card.module.scss */
}
rust file:
// src/component/card/card.rs
// Import a css file's classes:
import_crate_style!;
All classnames found inside the file src/component/card/card.module.scss will be included as constants inside a module named as the identifier passed as first argument to import_style.
The proc macro has no other effects, generating the modified css file is done using the stylance cli.
Stylance cli
Install stylance cli
cargo install stylance-cli
Run stylance cli:
stylance --output ./bundled.scss ./path/to/crate/dir/
This will find all the files ending with .module.scss and .module.cssand bundle them into ./bundled.scss, all classes will be modified to include a hash that matches the one the import_crate_style! macro produces.
Resulting output.scss:
}
During development it is convenient to use sylance cli in watch mode:
stylance --watch --output ./bundled.scss ./path/to/crate/dir/
The stylance process will then watch the .module.css files for changes and automatically rebuild the output file.