2 releases
new 0.1.4 | Nov 24, 2024 |
---|---|
0.1.3 | Sep 16, 2024 |
#463 in Development tools
73 downloads per month
29KB
494 lines
Welcome to in-vite π¦
What's in-vite?
In-vite
is a small library, inspired by Laravel's Vite Plugin. It allows you
to integrate vite's bundling capabilities into your Rust π¦ backend.
Getting Started π
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.
Setting up Vite π§
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.
Further configurations
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);
Mode Configuration
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);
Integrations πΊοΈ
in-vite
provides integrations for templating engines such as
tera and
minijinja. Which can be activated
using the appropriate feature flag.
Integration with 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())?;
Integration with minijinja
π₯·
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)?;
Contributing
If you consider contributing, then first of all: Thank you! π 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.
Sponsoring
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) π¨βπ with a steady supply of caffeinated beverages which are then metabolized into 100% organic Rust code.
License
This project is licensed under the MIT license, which you find here.
Dependencies
~0.7β9.5MB
~95K SLoC