Skip to content

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 ​

sh
npx megh add vite
npm install

This adds:

FilePurpose
frontend/app.tsAn entry: browser code for your pages
frontend/css/app.cssStyles, imported by the entry
frontend/tsconfig.jsonEditor settings for browser code (the server's tsconfig.json covers src/ only)
vite.config.mtsBuilds 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 ​

FolderHoldsRun by
src/Server TypeScriptNode, compiled by megh
views/.megh.html templates onlyMegh's view engine
frontend/Browser code: .ts, .css, .vue, .tsx, .svelteThe browser, built by Vite
public/build/Vite's outputServed at /build/

Loading an entry ​

html
<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:

ts
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:

html
<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.port in vite.config.mts to use another.
  • The page is served by Megh, and modules come from the dev server. Vite allows cross-origin requests from localhost and 127.0.0.1 by default. If you open the app under another host name, set server.cors.
  • With Inertia, the asset version is "hot" while the dev server runs, so updates never force a full reload.
  • Pass --no-hmr to build with vite build --watch instead: the page then loads the built files, and you reload to see changes. A vite.config without the megh() plugin always works this way. --no-vite doesn't run Vite at all.
  • In production (NODE_ENV=production, which megh start sets) 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 ​

ts
app.useVite({ buildDir: "public/build", base: "/build/" });
OptionDefaultMeaning
buildDir"public/build"Vite's build.outDir
base"/build/"URL the build is served at: Vite's base
entrynoneEntry used by @vite with no arguments
servetrueServe 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.

Released under the MIT License.