Crate turf

source ·
Expand description

turf 🌱

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

Rust 1.70.0 MIT licensed

turf will:

  • 🌿 transform your SCSS files into CSS with grass, right at compilation time
  • 🪴 generate unique and dynamic class names for your CSS during compilation
  • 🔬 minify and optimize your CSS using lightningcss, ensuring compatibility with various browser targets
  • 🎨 inject the generated CSS into your binary, guaranteeing quick access to your styles whenever you need them


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

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


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;


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:

minify = true
load_paths = ["path/to/scss/files", "path/to/other/scss/files"]

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

chrome = [80, 1, 2]
firefox = 65
safari = [12, 3]

global_css_file_path = "path/to/global.css"
separate_css_files_path = "dir/for/separate/css/"

The following configuration options are available:

  • minify (default: true): Specifies whether the generated CSS should be minified or not. If set to true, the CSS output will be compressed and optimized for reduced file size. If set to false, the CSS output will be formatted with indentation and line breaks for improved readability.

  • load_paths: Specifies additional paths to search for SCSS files to include during compilation. It accepts a list of string values, where each value represents a directory path to be included. This option allows you to import SCSS files from multiple directories.

  • browser_targets: Defines the target browser versions for compatibility when generating CSS. It expects a structure that contains specific versions for different browsers. Each browser can have its own version specified.

  • class_names: Allows configuration of the CSS class name generation. It expects a structure that contains two values for generating CSS class names and excluding class names from the uniquification process.

  • debug (default: false): When set to true, this option will enable debug output of the read configuration and the generated CSS class names. This can be helpful for troubleshooting and understanding how the CSS is being generated.

  • file_output: Enables output of compiled CSS. It expects a structure that contains two values for a single global CSS file or separate CSS files for each compiled SCSS file.

The class_names Key
  • template (default: "class-<id>"): Specifies the template for generating randomized CSS class names. The template can include placeholders to customize the output. <id> will be replaced with a unique identifier for each CSS class name and <original_name> will be replaced with the original class name from the SCSS file.

  • excludes: An array of regex patterns that exclude class names in your SCSS files from the class name uniquification process.

The file_output Key
  • global_css_file_path: Specifies the file path for a global CSS file. If set, a CSS file will be created at the provided path, and all compiled styles will be written to this file. This allows you to have a single CSS file containing all the compiled styles.

  • separate_css_files_path: Specifies the directory path for separate CSS files. If set, all compiled CSS files will be saved in the specified directory. Each compiled SCSS file will have its corresponding CSS file in this directory, allowing for modular CSS management.

Browser Versions

The available browsers are as follows:

  • android
  • chrome
  • edge
  • firefox
  • ie
  • ios_saf
  • opera
  • safari
  • samsung
Browser Version Format

Three formats are supported:

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.0Example: [1, 2] represents version 1.2.0Example: [1, 2, 3] represents version 1.2.3.


once_cellThe minimum supported Rust version is 1.70.0 by default. This can be circumvented by using the once_cell feature flag, which will lower the minimum supported version to 1.65.0.

The inline_style_sheet 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::inline_style_sheet 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).


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


  • Returns a tuple of (style_sheet: &'static str, class_names: struct)
  • Generates the static variable STYLE_SHEET and the ClassName struct with default settings or the settings specified in the Cargo.toml