kilork.github.io

static-files

Static resources, collected at build time and compiled in.

A static folder bundled by build.rs into generated.rs and compiled into the binary

Shipping a Rust server with a frontend usually means shipping two things: the binary and a directory of files it must find at runtime. This removes the second one. The files are read at build time, turned into Rust data and compiled in.

What changes for you

Getting it

cargo add static-files --build
cargo add static-files

Minimal case — a folder of files:

use static_files::resource_dir;

fn main() -> std::io::Result<()> {
    resource_dir("./static").build()
}
include!(concat!(env!("OUT_DIR"), "/generated.rs"));

let resources = generate();   // index.html, app.js, … as bytes

With a frontend that needs building first:

use static_files::NpmBuild;

fn main() -> std::io::Result<()> {
    NpmBuild::new("web")
        .install()?
        .run("build")?
        .target("web/dist")
        .to_resource_dir()
        .build()
}

NpmBuild skips the install and the build when nothing changed, so this is cheap on a warm tree.

Where to look

source · crate · docs