# ferrovue [![crates.io](https://img.shields.io/crates/v/ferrovue.svg)](https://crates.io/crates/ferrovue) [![npm](https://img.shields.io/npm/v/ferrovue.svg)](https://www.npmjs.com/package/ferrovue) [![docs.rs](https://img.shields.io/docsrs/ferrovue)](https://docs.rs/ferrovue) [![CI](https://github.com/Kinsmir/ferrovue/actions/workflows/ci.yml/badge.svg)](https://github.com/Kinsmir/ferrovue/actions/workflows/ci.yml) [![Mutation score](https://img.shields.io/endpoint?url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2FKinsmir%2Fferrovue%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/Kinsmir/ferrovue/main) [![License: MIT OR Apache-2.0](https://img.shields.io/badge/license-MIT%20OR%20Apache--2.0-blue.svg)](#licence) Documentation: [docs.rs/ferrovue](https://docs.rs/ferrovue) (API and guide) · [kinsmir.github.io/ferrovue](https://kinsmir.github.io/ferrovue/) (guide and overview) Vue single-file components compiled to Rust render functions. A Rust server renders a component's HTML with no JavaScript runtime, and the browser hydrates that HTML with the same `.vue` file. - **No Node on the server.** The compiler runs at build time; the server is your Rust binary, and a render is a function call that writes into a `String`, 7.7 to 135 times faster than Vue's `renderToString` in the benchmarks (see [Performance](#performance)). - **Byte-identical to Vue.** The generated Rust writes exactly the bytes Vue's `renderToString` writes for the same props, so Vue hydrates the page without a mismatch. Every supported construct is held to fixtures recorded from Vue. - **Exact or refused.** What ferrovue cannot reproduce exactly is a compile error that names the file, line and construct. A component that compiles renders as Vue renders it, and props it cannot render exactly, such as a date string from a query that browsers read differently, are refused as it renders: `try_render` returns the refusal, so a server answers `400`. ```text Button.vue ──(ferrovue compiler, Node)──▶ button.rs ──(your Rust server)──▶ HTML ──▶ Vue hydrates it ``` ferrovue doesn't interpret templates. It takes what `@vue/compiler-sfc` compiles a template to for server rendering (straight-line `_push` calls plus a few `@vue/server-renderer` helpers) and translates that small, closed vocabulary into Rust that writes **the same bytes**. Vue still decides element structure, attribute order, whitespace and fragment markers. | | What it is | Published as | |---|---|---| | `packages/ferrovue` | The compiler, the `ferrovue` command, the Vite plugin and the browser client (TypeScript) | npm: [`ferrovue`](https://www.npmjs.com/package/ferrovue) | | `crates/ferrovue` | The runtime the generated Rust calls: escaping, slots, islands, `` matching. It holds `ferrovue-core`, `ferrovue-router` and `ferrovue-i18n`, released with it, as its modules `core`, `router` and `i18n` | crates.io: [`ferrovue`](https://crates.io/crates/ferrovue) ([API docs](https://docs.rs/ferrovue)) | ## Quick start ### 1. Install both halves ```sh pnpm add -D ferrovue cargo add ferrovue serde --features serde/derive cargo add --dev serde_json # the generated conformance helper uses it under cfg(test) ``` ferrovue needs Rust **1.89** or later (CI tests that and the latest stable). Generated code uses let-chains, so the crate that includes it needs **edition 2024**. ### 2. Configure `pnpm ferrovue init` writes a starter `ferrovue.config.json` and `components/Hello.vue`. Or write the configuration at your project root yourself: ```json { "$schema": "https://cdn.jsdelivr.net/npm/ferrovue@0.16.0/schema.json", "components": "client/components", "out": "src/generated" } ``` `$schema` points an editor at the configuration's JSON Schema, published with each version of the npm package, so VS Code and other JSON editors complete the keys, describe them on hover and flag a misspelt one. Name the version of `ferrovue` you use; `ferrovue init` writes it. ferrovue does not read `$schema`. Offline, the copy in the installed package works as well: `"$schema": "./node_modules/ferrovue/schema.json"`. | Key | Required | Meaning | |---|---|---| | `components` | yes | Directory of `.vue` files to compile | | `out` | yes | Directory the Rust modules are written to. **Every module ferrovue wrote there is replaced**, and one no component produces any more is removed; a `.rs` file without ferrovue's `// @generated` header is left alone, and `--check` reports it | | `routes` | no | JSON file listing the app's routes: each a vue-router path, or `{ "path", "name", "children" }`. Or `{ "pages": "client/pages" }`: a folder of pages, whose file names give the routes as vue-router's file-based routing reads them (`index.vue`, `[id].vue`, `[[id]].vue`, `[...path].vue`, `(group)` folders, a `name.vue` beside `name/` as its layout), each page compiled as a component. Needed for ``, `` and `useRoute()` | | `router` | no | Instead of `routes`: `{ routes, base?, linkActiveClass?, linkExactActiveClass?, strict?, sensitive? }`, matching `createWebHistory(base)` and `createRouter`'s options | | `stores` | no | Directory of Pinia option stores whose state components may read | | `sharedDirectories` | no | Directories outside the project that its components, stores, locale files and routes may link to | | `trustedHtml` | no | Rust type of a `TrustedHtml` prop (needed for `v-html`): `ferrovue::html::BasicHtml` (text with a few formatting tags, built in), `ferrovue::html::InlineHtml` (inline formatting tags only, built in, which makes every `TrustedHtml` prop inline HTML), `ferrovue::html::Sanitised` (HTML cleaned by ammonia, the `ammonia` feature), or a type of your own such as `crate::html::CleanHtml` | | `dateType` | no | Rust type of a `Date` prop: `ferrovue::date::JsDate` (the default), `chrono::DateTime` (the `chrono` feature), or a type of your own that implements `ferrovue::date::JsTime`. See [`dates_and_prices`](https://docs.rs/ferrovue/latest/ferrovue/guide/dates_and_prices/index.html#date-props) | | `unions` | no | Rust enums of your own for unions of string literals, by `type` alias: `{ "Tone": "crate::domain::Tone" }`. Generated code uses the enum, which implements `ferrovue::props::Union` and has the variants ferrovue would generate, and generates none for the union. See [`props`](https://docs.rs/ferrovue/latest/ferrovue/guide/props/index.html#unions-mapped-to-existing-enums) | | `helpers` | no | `{ module, functions }`: functions a template may call, each mapped to a Rust twin | | `twins` | no | Components ferrovue does not compile, each rendered by a Rust function of yours: `{ "VBtn": { "rust": "crate::ui::v_btn", "props": { "label": "string" }, "slots": ["default"] } }`. See [Rust twins](https://docs.rs/ferrovue/latest/ferrovue/guide/errors_and_limits/index.html#rust-twins) | | `i18n` | no | vue-i18n: `{ messages, locale?, fallbackLocale? }`, the directory of locale files (`en.json`, `nl.json`), the default locale and the fallbacks | | `clientDirectives` | no | Custom directives with no server output (no `getSSRProps`), by name without `v-`: `["focus"]` | | `scopeId` | no | How a `