stylance-rs/README.md

130 lines
3.4 KiB
Markdown
Raw Normal View History

2024-01-09 09:03:32 +08:00
# Stylance ![crates.io](https://img.shields.io/crates/v/stylance.svg)
2024-01-08 07:23:37 +08:00
2024-01-09 09:54:49 +08:00
Stylance is a library and cli tool for working with scoped CSS in rust.
2024-01-08 07:23:37 +08:00
2024-01-09 09:54:49 +08:00
# Usage
2024-01-08 07:23:37 +08:00
Stylance is divided in two parts:
2024-01-09 09:54:49 +08:00
1. Rust proc macros for importing scoped class names from css files as string constants into your rust code.
2024-01-08 21:01:31 +08:00
2. A cli tool that finds all css modules in your crate and generates an output css file with hashed class names.
2024-01-08 07:23:37 +08:00
2024-01-09 09:54:49 +08:00
## Proc macro
2024-01-08 07:23:37 +08:00
2024-01-09 09:54:49 +08:00
Add stylance as a dependency:
2024-01-08 21:01:31 +08:00
```cli
cargo add stylance
```
2024-01-09 09:54:49 +08:00
Then use the import_crate_style proc macro to read a css/scss file and bring the classes from within that file as constants.
2024-01-08 21:01:31 +08:00
2024-01-09 09:54:49 +08:00
`src/component/card/card.module.scss` file's content:
2024-01-08 07:23:37 +08:00
```css
2024-01-09 09:54:49 +08:00
/* */
2024-01-08 07:23:37 +08:00
.header {
background-color: red;
}
```
2024-01-09 09:54:49 +08:00
`src/component/card/card.rs` file's contents:
2024-01-08 21:01:31 +08:00
2024-01-08 07:23:37 +08:00
```rust
// Import a css file's classes:
2024-01-08 21:01:31 +08:00
stylance::import_crate_style!(my_style, "src/component/card/card.module.scss");
2024-01-08 07:23:37 +08:00
fn use_style() {
// Use the classnames:
println!("{}", my_style::header) // prints header-f45126d
}
```
2024-01-09 09:54:49 +08:00
All class names 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 side effects, to generate the transformed css file we then use the stylance cli.
### Nightly feature
If you are using rust nightly you can enable the `nightly` feature to get access to the `import_style!` macro which lets you specify the css module file as relative to the current file.
Enable the nightly feature:
```toml
stylance = { version = "<version here>", features = ["nightly"] }
```
Then import style as relative:
`src/component/card/card.rs`:
2024-01-08 21:01:31 +08:00
2024-01-09 09:54:49 +08:00
```rust
stylance::import_style!(my_style, "card.module.scss");
```
2024-01-08 21:01:31 +08:00
2024-01-09 09:54:49 +08:00
## Stylance cli
2024-01-08 21:01:31 +08:00
Install stylance cli
```cli
cargo install stylance-cli
```
Run stylance cli:
```cli
2024-01-09 09:54:49 +08:00
stylance ./path/to/crate/dir/ --output ./bundled.scss
2024-01-08 21:01:31 +08:00
```
2024-01-09 09:54:49 +08:00
The first argument is the path to the directory containing the Cargo.toml of your package/crate.
2024-01-08 22:07:19 +08:00
This will find all the files ending with `.module.scss` and `.module.css`and bundle them into `./bundled.scss`, all classes will be modified to include a hash that matches the one the `import_crate_style!` macro produces.
2024-01-08 21:01:31 +08:00
2024-01-09 09:54:49 +08:00
Resulting `./bundled.scss`:
2024-01-08 21:01:31 +08:00
```css
.header-f45126d {
background-color: red;
}
```
2024-01-09 09:54:49 +08:00
By default stylance cli will only look for css modules inside the crate's `./src/` folder. This can be [configured](#configuration).
### Watching for changes
2024-01-08 21:01:31 +08:00
During development it is convenient to use sylance cli in watch mode:
2024-01-09 09:54:49 +08:00
```cli
2024-01-08 21:01:31 +08:00
stylance --watch --output ./bundled.scss ./path/to/crate/dir/
```
2024-01-08 07:23:37 +08:00
2024-01-09 09:54:49 +08:00
The stylance process will then watch any `.module.css` and `.module.scss` files for changes and automatically rebuild the output file.
## <a name="configuration"></a> Configuration
Stylance configuration lives inside the Cargo.toml file of your crate.
All configuration settings are optional.
```toml
[package.metadata.stylance]
# output
# output file to generate
# has no default value, when not set you must provide an output
# to the stylance cli using the --output argumnent.
output = "./styles/bundle.scss"
# 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"]
```