Vite
Megh serves browser code built by Vite. The code lives in frontend/, Vite builds it into public/build/, and @vite in a view prints the tags that load it. Inertia apps use the same setup.
Setup
npx megh add vite
npm installThis adds:
| File | Purpose |
|---|---|
frontend/app.ts | An entry: browser code for your pages |
frontend/css/app.css | Styles, imported by the entry |
frontend/tsconfig.json | Editor settings for browser code (the server's tsconfig.json covers src/ only) |
vite.config.mts | Builds frontend/ into public/build with the megh() plugin, and runs the dev server for HMR |
It also adds app.useVite() to src/app.ts, vite and @fastify/static to package.json, and public/build/ to .gitignore. If views/layouts/app.megh.html exists, its <head> gets @vite('frontend/app.ts').
Folders
| Folder | Holds | Run by |
|---|---|---|
src/ | Server TypeScript | Node, compiled by megh |
views/ | .megh.html templates only | Megh's view engine |
frontend/ | Browser code: .ts, .css, .vue, .tsx, .svelte | The browser, built by Vite |
public/build/ | Vite's output | Served at /build/ |
Loading an entry
<head>
@vite('frontend/app.ts')
</head>@vite reads public/build/.vite/manifest.json and prints the entry's CSS (including CSS of the chunks it imports), modulepreload links for those chunks, and the entry's <script type="module">. Pass several entries to load them together: @vite('frontend/app.ts', 'frontend/admin.ts'). Every entry must be listed in megh({ input }) in vite.config.mts.
@vite works in pages, layouts, includes and components. It needs app.useVite(). Rendering fails with a clear error if the build is missing or the entry isn't in it.
The Vite plugin
vite.config.mts uses the megh() plugin from @meghjs/core/vite:
import { defineConfig } from 'vite';
import { megh } from '@meghjs/core/vite';
import vue from '@vitejs/plugin-vue'; // for Inertia with Vue
export default defineConfig({
plugins: [megh({ input: ['frontend/app.ts'] }), vue()],
});It sets what useVite() expects: base: '/build/', output in public/build with a manifest, publicDir: false (Megh serves public/ itself) and the dev server settings for hot module replacement. The plugin options are input, buildDir and base. Any option you set in the config yourself wins.
Hot module replacement
megh serve starts Vite's dev server alongside the app (output prefixed with [VITE]). While it runs, the plugin writes its URL to public/build/hot, and @vite loads the entry from the dev server with Vite's HMR client instead of from the build:
<script type="module" src="http://localhost:5173/build/@vite/client"></script>
<script type="module" src="http://localhost:5173/build/frontend/app.ts"></script>Edits to components (Vue, React with Fast Refresh, Svelte) and CSS show up in the page without a reload, and component state is kept. Server code in src/ still restarts the app as usual. For .tsx and .jsx entries, @vite also installs React Fast Refresh before the entry runs.
- The dev server uses port 5173 and fails to start if it's taken, so the URL in the hot file is always right. Set
server.portinvite.config.mtsto use another. - The page is served by Megh, and modules come from the dev server. Vite allows cross-origin requests from
localhostand127.0.0.1by default. If you open the app under another host name, setserver.cors. - With Inertia, the asset version is
"hot"while the dev server runs, so updates never force a full reload. - Pass
--no-hmrto build withvite build --watchinstead: the page then loads the built files, and you reload to see changes. Avite.configwithout themegh()plugin always works this way.--no-vitedoesn't run Vite at all. - In production (
NODE_ENV=production, whichmegh startsets) the hot file is ignored, so a stale one can't point pages at a dev server.
Builds
npm run build runs vite build && megh build, so a deployment ships a current public/build, and @vite loads the hashed files from the manifest.
Options
app.useVite({ buildDir: "public/build", base: "/build/" });| Option | Default | Meaning |
|---|---|---|
buildDir | "public/build" | Vite's build.outDir |
base | "/build/" | URL the build is served at: Vite's base |
entry | none | Entry used by @vite with no arguments |
serve | true | Serve buildDir at base. Set false when a CDN or proxy serves it |
Keep buildDir and base in step with the megh() plugin options in vite.config.mts.
Asset version
useVite() hashes the manifest, so the version changes with each build. useInertia() uses it as the Inertia asset version unless you pass one.