myFavShrimp / turf

Macro based compile-time SCSS transpilation, CSS minification, and class name uniquification toolchain inspired by CSS modules.
MIT License
58 stars 3 forks source link
css leptos rust scss wasm web yew

turf 🌱

Warning | The repository might reflect the current development state, which may differ from the officially released version. While the repository provides insights into ongoing development and potential upcoming features, it may not necessarily represent the release available through crates.io or other distribution channels.

turf allows you to build SCSS to CSS during compile time and inject those styles into your binary.

Crates.io Docs.rs Build Status MIT licensed

turf will:

Usage

For a complete runnable example project, you can check out one of the examples:

leptos-example yew-example dioxus-example axum-askama-htmx

1. Create SCSS styles for your application

// file at scss/file/path.scss

.TopLevelClass {
    color: red;

    .SomeClass {
        color: blue;
    }
}

2. Use the style_sheet macro to include the resulting CSS in your code

turf::style_sheet!("scss/file/path.scss");

The macro from the above example will expand to the following code:

static STYLE_SHEET: &'static str = "<style_sheet>";
struct ClassName;
impl ClassName {
    pub const TOP_LEVEL_CLASS: &'static str = "<unique_class_name>";
    pub const SOME_CLASS: &'static str = "<another_unique_class_name>";
}

3. Use the ClassName struct and its associated constants to access the generated class names

let top_level_class_name = ClassName::TOP_LEVEL_CLASS;
let some_class_name = ClassName::SOME_CLASS;

Configuration

The configuration for turf can be specified in the Cargo.toml file using the [package.metadata.turf] and [package.metadata.turf-dev] keys. This allows you to conveniently manage your SCSS compilation settings for both development and production builds within your project's manifest.

Both profiles offer the exact same configuration options. However, if you haven't specified a [package.metadata.turf-dev] profile, the [package.metadata.turf] settings will also be applied to debug builds. This ensures consistency in the compilation process across different build types unless you explicitly define a separate configuration for the development profile.

Example configuration:

[package.metadata.turf]
minify = true
load_paths = ["path/to/scss/files", "path/to/other/scss/files"]

[package.metadata.turf.class_names]
template = "custom-<id>-<original_name>"
excludes = ["exclude-this-class-please", "^abc-[123]{4}"]

[package.metadata.turf.browser_targets]
chrome = [80, 1, 2]
firefox = 65
safari = [12, 3]

[package.metadata.turf.file_output]
global_css_file_path = "path/to/global.css"
separate_css_files_path = "dir/for/separate/css/"

The following configuration options are available:

The class_names Key

The file_output Key

Browser Versions

The available browsers are as follows:

Browser Version Format

Three formats are supported:

major major.minor major.minor.patch
Use a single integer to specify the major version number. Use an array [major, minor] to specify both the major and minor version numbers. Use an array [major, minor, patch] to specify the major, minor, and patch version numbers.
Example: 1 or [1] represent version 1.0.0 Example: [1, 2] represents version 1.2.0 Example: [1, 2, 3] represents version 1.2.3.

Additional Macros

turf provides a few additional macros for other use cases.

The style_sheet_values Macro

In some cases, it may be necessary to have a struct's instance to access the class names (for example when using turf in askama templates). The turf::style_sheet_values macro provides an alternative to directly including the resulting CSS and obtaining the associated class names. It returns a tuple of (style_sheet: &'static str, class_names: struct).

Usage:

let (style_sheet, class_names) = turf::style_sheet_values!("path/to/style.scss");
let some_class_name = class_names.some_class;

The inline_style_sheet Macro

If you don't want your style sheet to live in another file, you can use the turf::inline_style_sheet macro. It allows you to write inline SCSS which will then be compiled to CSS.

Usage:

turf::inline_style_sheet! {
    .TopLevelClass {
        color: red;

        .SomeClass {
            color: blue;
        }
    }
}

// ...

let some_class_name = ClassName::SOME_CLASS;

The inline_style_sheet_values Macro

This macro combines the functionality of both the style_sheet_values and inline_style_sheet macros. It allows you to write inline SCSS and returns an tuple of (style_sheet: &'static str, class_names: struct).

Usage:

let (style_sheet, class_names) = turf::inline_style_sheet_values! {
    .TopLevelClass {
        color: red;

        .SomeClass {
            color: blue;
        }
    }
};
let some_class_name = class_names.some_class;

Contributions

Contributions to turf are always welcome! Whether you have ideas for new features or improvements, don't hesitate to open an issue or submit a pull request. 🤝

License

turf is licensed under the MIT license. For more details, please refer to the LICENSE file. 📄