Skip to content

@kiln-js/vite-plugin

@kiln-js/vite-plugin connects Kiln to Vite. It builds the route table from the file system, keeps server-only code out of browser bundles, rewrites <kiln:title> tags, finds islands, and refreshes everything when files change during development.

bash
bun add -d @kiln-js/vite-plugin

The plugin is usually added through kiln() in your vite.config.ts. The CLI adds it for you when it builds or runs the app, so you only need your own Vite config when you want to change the defaults or add other plugins.


Usage

ts
// vite.config.ts
import { defineConfig } from "vite";
import { kiln } from "@kiln-js/vite-plugin";

export default defineConfig({
  plugins: [...kiln({ pagesDir: "app/pages", apiDir: "app/api", appDir: "app" })],
});

kiln() returns an array of Vite plugins, so spread it into plugins.


kiln(options?)

Options

ts
interface KilnOptions {
  pagesDir?: string;  // default "app/pages"
  apiDir?: string;    // default "app/api"
  appDir?: string;    // default "app"
  plugins?: KilnPluginSpec[];
}

interface KilnPluginSpec {
  name: string;
  vite?: () => PluginOption[];
}
OptionDefaultMeaning
pagesDirapp/pagesFolder scanned for page routes. Resolved against the working directory.
apiDirapp/apiFolder scanned for API routes.
appDirappFolder scanned for islands and used to decide which files to refresh.
plugins[]Extra plugin specs. Each vite() result is added to the Vite plugin list.

The function always adds the React plugin, @vitejs/plugin-react, so you do not add it yourself.

Plugins returned

kiln() returns these plugins, in this order:

  1. @vitejs/plugin-react
  2. kiln:routes, which generates the route table and rewrites titles.
  3. kiln:compile-server-split, which removes server code from client bundles and wraps islands.
  4. Any plugins from the plugins option.

Virtual modules

The plugin provides two virtual modules. Your code never imports them directly. The plugin adds a small prelude to the client and server entries that imports them and stores them on the global object.

virtual:kiln-routes

For the server build, it exports:

  • default: the page route table, with a load function for each page.
  • api: the API route table.
  • specials: { errors, notFounds }, the _error and _not-found tables.

For the client build, it exports the page route table with a load function for each page that can run in the browser, and empty api and specials. Server-only pages get load: null.

The prelude sets globalThis.__KILN_ROUTES__, __KILN_API__, __KILN_SPECIALS__, and __KILN_ISLANDS__. The server and client read these globals when you do not pass routes explicitly.

virtual:kiln-islands

The client build exports a map from island id to a loader. Each id is the file path relative to the working directory. The server build exports an empty map.


Server-only code

The plugin keeps server code out of browser bundles in two ways.

Files named .server

A file whose name ends in .server.ts, .server.tsx, .server.js, or .server.jsx (or with no extension, .server) is server-only. Client code that imports one fails the build:

text
"./db.server" is server-only and cannot be imported by client code.

A page with a .server suffix in its filename is also server-only. It renders on the server and sends no client script, unless it contains islands.

Server exports in pages

A page file that is not .server can export loader, action, or revalidate. Those exports run on the server only. In the client build, the plugin removes the exported declarations and any imports that are no longer used. The rest of the module is kept.

So this page works in both places, and its loader does not reach the browser:

tsx
import { db } from "../lib/db";            // removed from the client bundle if unused there

export async function loader() {           // removed from the client bundle
  return db.posts.all();
}

export default function Posts({ data }: { data: unknown[] }) {
  return <ul>{data.length}</ul>;
}

The plugin only checks files under the pages folder and the app folder. It does not check files in node_modules.


Title rewriting

<kiln:title> is a JSX tag that Kiln accepts in any page file. The plugin rewrites it to <Title> from @kiln-js/core before Vite compiles the file:

tsx
<kiln:title>Pricing · My app</kiln:title>

becomes

tsx
<KilnTitle>Pricing · My app</KilnTitle>

with import { Title as KilnTitle } from "@kiln-js/core" added at the top.

The rewrite only changes JSX. Strings, template literals, and comments that contain <kiln:title> are left alone. The exported function rewriteTitleTags(code) performs this rewrite and is available if you need it.

The type for <kiln:title> lives in .kiln/env.d.ts, which the CLI writes on every run.


Islands

A file under the app folder whose first statement is the directive "use island" or 'use island' is an island. Comments may come before the directive. The plugin wraps its default export so that:

  • On the server, the component renders inside a container that records the island's id.
  • In the browser, the island loads only its own code and hydrates its own container.

The file's raw component is kept as __kilnRaw, so the browser can hydrate it without the page.

tsx
"use island";

import { useState } from "react";

export default function Counter({ start = 0 }: { start?: number }) {
  const [n, setN] = useState(start);
  return <button onClick={() => setN(n + 1)}>{n}</button>;
}

The island id is its path relative to the working directory, with forward slashes. See Islands.

Files that are island code are found by scanning the app folder. A file that is not under the app folder is not an island, even if it has the directive.


Development refresh

During kiln dev, the plugin watches the pages, API, and app folders:

  • Changes to a file refresh the scan cache and invalidate the route table. Vite updates the module with hot reload.
  • Adding or removing a file refreshes the cache and sends a full reload, because the route table changed.

The scan cache counts hits, misses, and invalidations. buildCacheStats() returns the totals across all caches in the process. The dev dashboard shows them.


Exports

ExportPurpose
kiln(options?)The Vite plugin list.
rewriteTitleTags(code)Rewrite <kiln:title> to <KilnTitle> in a source string.
stripServerCode(code, parse)Remove server exports from a module. Returns null when there is nothing to remove.
islandCode(code, parse, islandId)Wrap an island's default export. Returns null when the module has no default export.
buildCacheStats()Totals for the scan cache.
KilnOptions, KilnPluginSpec, CacheStatsTypes.

stripServerCode and islandCode take a parse function that returns an ESTree-style AST. Vite's this.parse provides one inside plugin hooks.