Crates.io | in-vite |
lib.rs | in-vite |
version | 0.1.4 |
source | src |
created_at | 2024-09-16 17:20:07.339865 |
updated_at | 2024-11-24 18:49:32.989103 |
description | Vite integration for your Rust backend. |
homepage | |
repository | https://github.com/HiImJulien/in-vite |
max_upload_size | |
id | 1376633 |
size | 37,068 |
In-vite
is a small library, inspired by Laravel's Vite Plugin. It allows you
to integrate vite's bundling capabilities into your Rust :crab: backend.
cargo install in-vite
The library revolves around the struct Vite
which handles most aspects and
is required for integration:
use in_vite::Vite;
fn main() {
let vite = Vite::default();
// Retrieve the HTML required to include app.js and it's dependencies.
let code = vite.make_html(vec!["app.js"]).unwrap();
}
[!IMPORTANT]
in-vite
does not setup Vite by itself, rather it expects an already setup instance. On how to setup Vite read further.
This library requires an instance of Vite to be already setup. To setup Vite
use your favorite package manager, for example using npm
:
npm create vite@latest
Next, you need to extend Vite's vite.config.js
:
// vite.config.js
export default defineConfig({
build: {
manifest: true,
rollupOptions: {
input: 'app.js'
},
}
});
The manifest is used in production builds to resolve the appropriate build artifact.
[!NOTE] You must manually specify entrypoints, since Vite has no
index.html
to go from.
By default, Vite serves assets on http://localhost:5173
. This and other
defaults can be overwritten by constructing an instance of Vite
with
ViteOptions
.
Let's suppose, you're running Vite on port 8090
, you can construct an instance
like this:
let opts = ViteOptions::default().host("http://localhost:8090");
let vite = Vite::with_options(opts);
By default in-vite
is assuming that you're running in development mode,
unless any of the following environemt variables are set to production
:
LOCO_ENV=production
# or
RAILS_ENV=production
# or
NODE_ENV=production
This behavior can be explicitly overwritten using ViteOptions
:
let opts = ViteOptions::default().mode(ViteMode::Production);
let vite = Vite::with_options(opts);
in-vite
provides integrations for templating engines such as
tera and
minijinja. Which can be activated
using the appropriate feature flag.
tera
Using the feature flag tera
, the integration can be activated:
cargo add in-vite -F tera
Integrating Vite is as simple as registering a function with your tera::Tera
instance:
let vite = Vite::default();
let mut tera = tera::Tera::default();
tera.register_function("vite", vite);
let template = tera.render_str(r#"{{ vite(resources="app.js") }}"#, &tera::Context::new())?;
minijinja
:ninja:Like other integrations, this one can be activated with the feature flag minijinja
:
cargo add in-vite -F minijinja
let vite = Vite::default();
let mut env = minijinja::Environment::new();
env.add_global("vite", minijinja::Value::from_object(vite));
let template = env.render_str(r#"{{ vite(resources="app.js") }}"#, minijinja::Value::UNDEFINED)?;
If you consider contributing, then first of all: Thank you! :gift_heart: The first and simplest way to show your support is to star this repo.
This project accepts bug reports and feature requests via the integrated issue tracker. Pull requests for new integrations are also welcome!
Additionally, code reviews and pointers on how to improve the libraries code are welcome. This is my first Rust library after all.
Thank you for considering sponsoring! While this project does not require sponsoring, small donations are accepted. 100% of the donations are used to provide a student (me) :man_student: with a steady supply of caffeinated beverages which are then metabolized into 100% organic Rust code.
This project is licensed under the MIT license, which you find here.