#css #css-class #style-sheet #module #scoped-css #style #macro

css-modules

CSS Modules with a macro for convenience

5 releases (breaking)

0.5.2 Dec 18, 2019
0.5.1 Apr 22, 2019
0.4.1 Dec 18, 2019
0.4.0 Nov 29, 2018

#507 in Procedural macros

MIT license

32KB
832 lines

CSS Modules

Build Status Latest Version Documentation Minimum rustc version License:MIT lines of code

The CSS Modules project defines CSS Modules as:

A CSS Module is a CSS file in which all class names and animation names are scoped locally by default.

This implementation is however currently immature and has not (as far as I'm aware) been used in a real world situation. Currently only animation and class names are locally scoped and the following work is in progress:

  • Inlining url() and @import statements
  • Creating or integrating with a asset compilation tool tool (Rollup, Webpack, etc)

Usage

Add this crate as a build dependency and as a regular dependency:

[dependencies]
css-modules = "0.5"

[build-dependencies]
css-modules = "0.5"

Create a build script (build.rs) in the root of your project:

use css_modules::CssModules;

fn main() {
    let mut css_modules = CssModules::default();

    // Include all CSS files in the src dir:
    css_modules.add_modules("src/**/*.css").unwrap();

    // Compile all modules and export CSS into one file:
    css_modules.compile("public/app.css");
}

And then finally, you can include CSS modules:

use css_modules::include_css_module;

let css = include_css_module!("test.css"); // relative path to your CSS
let myClass = css["original-class-name"]; // aliased class name

For more detailed examples, look in the examples/ directory.

Dependencies

~2.2–3MB
~59K SLoC