# Vite Static
Embed Vite chunks into your Rust application and query them individually.
## How it works
Vite Static has `Manifest` trait, that requires to implement 2 methods and adds methods to query
chunks and resolve output filename (f.e. `main.SOMEHASH.js`) from manifest key (f.e. `main.ts`).
Vite Static provides `derive` feature (enabled by default), that adds `Manifest` derive to
automatically implement trait.
Also, Vite Static has integrations with web frameworks, to easily serve your static files.
## Usage
1. Add `vite-static` library
```shell
cargo add vite-static # WIP
```
2. Enable `build.manifest` vite option.
In `path/to/vite-project/vite.config.js`:
```javascript
export default defineConfig({
build: {
manifest: true, }
})
```
3. Add `Manifest` derive
```rust
use vite_static::Manifest;
#[derive(Manifest)]
#[vite_dist = "path/to/vite-project/dist"] struct MyViteStatic;
```
4. Use it!
To see more use cases, look into `examples/` folder and `Manifest` trait.
## Features
Without any feature, you have `Manifest` trait, that helps you with quering chunks.
| `derive` | adds `Manifest` derive macro (default) |
| `actix-web` | adds `ActixFiles` service |
| `axum` | WIP |
| `rocket` | WIP |
| `html` | adds `HTMLIntegration` builder |
## Q&A
- **Q:** Why should am I use this library, over [vite-rs](https://github.com/Wulf/vite-rs)?
**A:** It depends on your needs.
You should use `vite-rs`, when you have frontend built with Vite, including routing and
HTML, and you need a library to embed whole frontend into application.
You should use `vite-static`, when you have assets (JS, CSS, PNGs, etc.) built with Vite, but you
use HTML templating to build faster and lighter websites, so you need to embed only assets.
Because of `vite-static` use case and lightness, it doesn't have development server and auto-running `npm build` in derive macro.
- **Q:** Why it doesn't run `npm build` automatically?
**A:** Because it only adds problems.
First, it will run `npm build` on every rust compilation and will require adding additional options.
Second, it will make configuration only harder. For example you use nix. While building release, you build vite project in separate derivation, but during development you would use automatic `npm build`.
So it's simpler to just open 2 terminals: first to watch and re-run `cargo run`, second to run `vite watch`.
[KISS principle!](https://en.wikipedia.org/wiki/KISS_principle)
## Credits
- [vite-rs](https://github.com/Wulf/vite-rs) - good library to embed entire frontend into rust application
- [actix-web-static-files](https://github.com/kilork/actix-web-static-files) - `ActixFiles` service implementation is partially taken from this crate