Skip to content

@plinthjs/vite-plugin ​

The Plinth Vite plugin, the conventional first-party Vite plugin equivalent. It is the build-time half of Plinth's Vite integration; @plinthjs/vite is the runtime half that turns entrypoints into <script> and <link> tags. Given your entrypoints, the plugin:

  • Build: points Vite at public/build, enables the manifest, sets the entrypoints as Rollup inputs, and writes public/build/manifest.json where @plinthjs/vite reads it (copying Vite 5+'s .vite/manifest.json). publicDir is disabled so your public/ is not copied into the build.
  • Dev: writes the running dev-server URL to the hot file (default public/hot) so @plinthjs/vite switches to dev mode. The file is removed when the dev server exits.
  • Refresh (optional): triggers a full page reload when server-rendered views or routes change.

It has no runtime dependencies beyond Node built-ins.

Install ​

sh
npm install @plinthjs/vite-plugin

vite is a peer dependency (^5.0.0 or ^6.0.0), so install it alongside: npm install -D vite.

Usage ​

ts
// vite.config.ts
import plinth from '@plinthjs/vite-plugin'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    plinth({
      input: ['src/resources/js/app.ts', 'src/resources/css/app.css'],
      refresh: true,
    }),
  ],
})

The plugin is the package's default export. For a single entrypoint (or a list) with no other options, pass it directly: plinth('src/resources/js/app.ts').

Then render the tags at runtime with @plinthjs/vite:

ts
import { Vite } from '@plinthjs/vite'

new Vite().tags(['src/resources/js/app.ts', 'src/resources/css/app.css'])

Run vite for development (writes public/hot) and vite build for production (writes public/build/manifest.json).

Options ​

plinth() accepts an entrypoint, a list of entrypoints, or a PlinthViteOptions object:

ts
import plinth, { type PlinthViteOptions } from '@plinthjs/vite-plugin'

const options: PlinthViteOptions = {
  input: 'src/resources/js/app.tsx', // required: string or string[]
  publicDirectory: 'public', // the app's web root (default 'public')
  buildDirectory: 'build', // build sub-directory under the web root (default 'build')
  hotFile: 'public/hot', // dev-server hot file (default '<publicDirectory>/hot')
  refresh: ['src/resources/views/**', 'src/routes/**'], // true, a glob, a list of globs, or false
}

export default defineConfig({ plugins: [plinth(options)] })

When you change publicDirectory, buildDirectory or hotFile, pass the same values to new Vite({ ... }) from @plinthjs/vite so both halves agree.

During vite build the plugin sets base to /<buildDirectory>/, and to / in dev. It also enables server.cors so pages served by your app can load modules from the dev server.

Full-page refresh ​

With refresh: true a second plugin (plinth:refresh) is added that sends a full reload when a matching file is added or changed. The default patterns are:

src/resources/views/**
src/resources/**/*.plinth
src/routes/**
src/controllers/**
src/middleware/**
src/models/**
src/providers/**
src/support/**
src/auth/**
resources/views/**
resources/**/*.plinth
routes/**

Patterns are simple globs (*, **, ?) matched against paths relative to the Vite root. With refresh omitted or false, only the core plinth plugin is returned.

Released under the MIT License.