css-loader 0.0.1

A simple macro to include style files in Rust using CSS Modules-like scoping
Documentation
  • Coverage
  • 0%
    0 out of 1 items documented0 out of 0 items with examples
  • Size
  • Source code size: 15.87 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.04 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 46s Average build duration of successful builds.
  • all releases: 46s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • thepeak99

rust-css-loader

A simple macro to include style files in Rust using CSS Modules-like scoping.

Example

main.rs:

use yew::prelude::*;

import_style!("src/styles.scss");

#[function_component(App)]
fn app() -> Html {
    html! {
        <h1 class={styles.header}>{ "Hello World" }</h1>
    }
}

fn main() {
    println!("Hello, world!");
    yew::start_app::<App>();
}

styles.scss

$textcolor: yellow;
$fontsize: 18px;

.header {
  color: $textcolor;
  font-size: $fontsize;
}

Notes

Styles are imported relative to CARGO_MANIFEST_DIR, basically the directory that contains the Cargo.toml file. It would be nice to do relative path imports like in Javascript, however, due to a little limitation in the Rust compiler, this can't be done right now.

Secondly, the same limitation prevents us from having a way to "link" the style file to the Rust source file, hence modifying the style files won't trigger a rebuild/reload.

These both issues be addressed after this API is stabilized in the Rust compiler.

.css files are imported directly and .scss are compiled with rsass and loaded automatically.