@plinthjs/vite
The Plinth Vite integration (the @vite directive). Vite resolves entrypoints to the HTML tags a page needs:
- Hot (dev): when the dev server's hot file exists, tags point at the running dev server and include the
@vite/clientruntime (plus an optional React Fast Refresh preamble). - Build (prod): otherwise it reads the build
manifest.jsonand emits hashed<script>,<link rel="stylesheet">and<link rel="modulepreload">tags.
It pairs with @plinthjs/vite-plugin, which writes the hot file and the manifest. Filesystem access goes through an injectable ViteFileSystem, so it is fully unit-testable.
Install
sh
npm install @plinthjs/viteUsage
ts
import { Vite } from '@plinthjs/vite'
const vite = new Vite() // reads public/hot and public/build/manifest.json
vite.tags(['src/resources/js/app.ts', 'src/resources/css/app.css'])
// build mode:
// <link rel="stylesheet" href="/build/assets/app.9b2c.css" />
// <link rel="modulepreload" href="/build/assets/vendor.1a2b.js" />
// <script type="module" src="/build/assets/app.4f3a.js"></script>
vite.asset('src/resources/js/app.ts') // '/build/assets/app.4f3a.js'
vite.isRunningHot() // true while `vite` dev is runningThe integration is renderer-agnostic. In a @plinthjs/view template, share the instance and emit its tags raw:
ts
views.share({ vite })
views.register(
'layout',
'<head>{!! vite.reactRefresh() !!}{!! vite.tags("src/resources/js/app.ts") !!}</head>',
)Options
ts
const vite = new Vite({
publicDirectory: 'public', // the web root (default 'public')
buildDirectory: 'build', // build sub-directory (default 'build')
manifestFilename: 'manifest.json', // default 'manifest.json'
hotFile: 'public/hot', // default '<publicDirectory>/hot'
assetUrl: 'https://cdn.example.com', // prefix for built asset URLs (default same-origin)
})
// Or adjust later, framework-style.
vite
.useBuildDirectory('admin-build')
.useHotFile('public/admin.hot')
.useManifestFilename('admin.json')CSP, integrity and tag attributes
ts
const nonce = vite.useCspNonce() // generate one, or pass your own: vite.useCspNonce('abc123')
vite.cspNonce() // the nonce attached to every emitted tag
vite.useIntegrityKey('integrity') // read SRI hashes from this manifest key (the default)
vite.useIntegrityKey(false) // disable integrity attributes
vite.useScriptTagAttributes({ defer: true, 'data-turbo-track': 'reload' })
vite.useStyleTagAttributes(({ src }) => ({ 'data-src': src }))
vite.usePreloadTagAttributes({ crossorigin: 'anonymous' })Attribute values render as name="value" for strings, a bare name for true, and are omitted for false or null. A resolver receives { src, url, chunk, manifest } for each tag.
Manifest and content
ts
import { parseManifest } from '@plinthjs/vite'
vite.manifest() // the parsed, cached Manifest (throws if the build has not run)
vite.content('src/resources/css/app.css') // the built file's raw contents, e.g. for inlining
parseManifest(json) // parse a manifest.json string into a typed ManifestTesting
ts
const vite = new Vite({
fileSystem: {
exists: (path) => path === 'public/build/manifest.json',
read: () =>
JSON.stringify({ 'src/resources/js/app.ts': { file: 'assets/app.js', isEntry: true } }),
},
})
vite.asset('src/resources/js/app.ts') // '/build/assets/app.js'