Giter Club home page Giter Club logo

creme's Introduction

crème 🍦

⚠️ creme is in an experimental state. Use with caution!

Creme is a simple, opinionated build-time static asset tool for server-side Rust websites, complete with compile-time checks and a fast dev server for quick iteration.

Features

  • πŸ”₯ Fast Dev-Mode Server
  • πŸ“ Static File Handling
  • πŸ”Ž Cache Busting
  • ⚑ CSS Bundling With LightningCSS

Usage

Check out the examples here

Create a public and assets folder in your project's manifest. Public files are copied over without any modifications, asset files are hashed, minified, and optimized.

An example project may look like this:

my_website/
β”œβ”€β”€ Cargo.toml
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.rs
β”‚   └── ...
β”œβ”€β”€ public/
β”‚   β”œβ”€β”€ robots.txt
β”‚   └── favicon.ico
└── assets/
    └── css/
        β”œβ”€β”€ style.css
        └── modules/
            β”œβ”€β”€ _mod1.css
            └── _mod2.css

Configure Creme in your build.rs script, using the included builder.

Creme lets you either embed assets into the binary for quick and easy deployment, or to a directory of your choice for deploying to a CDN.

use creme_bundler::{Creme, CremeResult};

fn main() -> CremeResult<()> {
    // Recommended to include. Creme will setup the rest.
    println!("cargo:rerun-if-changed=build.rs");

    Creme::new()
        .out_dir("dist")? // Outputs assets to the `dist` directory, for CDN deployment.
        // .embedded()? // Or prepare assets to be embedded into the binary and served directly.
        .recommended()? // Reads from `public` and `assets` folder.
        .bundle()
}

In your Rust code, reference an asset's URL:

use creme::asset;

asset!("css/style.css");
// Becomes "/assets/style-[hash].css" in release mode
// Or "/assets/css/style.css" in dev mode.

Or directly in your template engine of choice:

use creme::asset;

html! { 
    head {
        title { "Hello Creme + Maud!" }
        link rel="stylesheet" href=(asset!("css/style.css"));
    }
    ...
    img src=(asset!("img/cat.jpeg"));
}

Optionally, use the built-in tower creme::service!() macro. This handles creating and setting up the dev server service.

For example, with Axum:

let app = Router::new()
    .route("/", get(index_handler))
    .fallback_service(
        creme::service!()
            .fallback(not_found_handler.into_service())
    );

For more, see here for examples

creme's People

Contributors

wrapperup avatar

Stargazers

 avatar  avatar

Watchers

 avatar

creme's Issues

Extensions / Plugin support

Creme is currently not fairly modular under the hood, but a minor refactor could make it so. This would make it trivial to make extensions to automatically handle bundling htmx, Tailwind, etc. Since the main API is a builder, it be as simple as adding a trait implementation, so for eg.

Creme::new()
    .tailwind()
    .htmx()
    ...

More details are probably needed.

Add Documentation

All of the documentation exists mostly in the README currently, some of it could live in docs

Remove unused files from final build (hard)

Since creme's bundler runs before any of your Rust source code is compiled, it has to copy over all assets, even if they're unused.

We could solve this by bundling the artifact files into a temp directory, and then making use of the asset! macro to copy over only the assets that are called in the source. This probably also doesn't work for rust_embed, since it also runs during this stage. This could affect build times, so this is only going to be for release builds.

Another solution is to basically grep all invocations of creme::asset!() (or asset!() if it's imported in the source). This has other caveats, but it fixes some of the problems in the previous solution.

Support Tailwind

Having Tailwind support would be nice to have built-in. Leptos and Dioxus have it built-in in their tooling. Perhaps Tailwind's upcoming LightningCSS refactor would make this more trivial.

Runtime CSS compile step

Currently, Creme just serves the CSS as-is, which works great for modern browsers already, so it's a low priority. This would also be a building block feature for supporting Sass and SCSS.

Embed Static Assets

Basically, copy rust_embed's functionality for release mode. If we can reuse that crate, then perfect.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.