Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vite 8 (Rolldown-powered) configuration, plugin API, SSR, library mode, and Environment API reference.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/core-plugin-api.md
1---2name: vite-plugin-api3description: Vite plugin authoring with Vite-specific hooks4---56# Vite Plugin API78Vite plugins extend Rolldown's plugin interface with Vite-specific hooks.910## Basic Structure1112```ts13function myPlugin(): Plugin {14return {15name: 'my-plugin',16// hooks...17}18}19```2021## Vite-Specific Hooks2223### config2425Modify config before resolution:2627```ts28const plugin = () => ({29name: 'add-alias',30config: () => ({31resolve: {32alias: { foo: 'bar' },33},34}),35})36```3738### configResolved3940Access final resolved config:4142```ts43const plugin = () => {44let config: ResolvedConfig45return {46name: 'read-config',47configResolved(resolvedConfig) {48config = resolvedConfig49},50transform(code, id) {51if (config.command === 'serve') { /* dev */ }52},53}54}55```5657### configureServer5859Add custom middleware to dev server:6061```ts62const plugin = () => ({63name: 'custom-middleware',64configureServer(server) {65server.middlewares.use((req, res, next) => {66// handle request67next()68})69},70})71```7273Return function to run **after** internal middlewares:7475```ts76configureServer(server) {77return () => {78server.middlewares.use((req, res, next) => {79// runs after Vite's middlewares80})81}82}83```8485### transformIndexHtml8687Transform HTML entry files:8889```ts90const plugin = () => ({91name: 'html-transform',92transformIndexHtml(html) {93return html.replace(/<title>(.*?)<\/title>/, '<title>New Title</title>')94},95})96```9798Inject tags:99100```ts101transformIndexHtml() {102return [103{ tag: 'script', attrs: { src: '/inject.js' }, injectTo: 'body' },104]105}106```107108### handleHotUpdate109110Custom HMR handling:111112```ts113handleHotUpdate({ server, modules, timestamp }) {114server.ws.send({ type: 'custom', event: 'special-update', data: {} })115return [] // empty = skip default HMR116}117```118119## Virtual Modules120121Serve virtual content without files on disk:122123```ts124const plugin = () => {125const virtualModuleId = 'virtual:my-module'126const resolvedId = '\0' + virtualModuleId127128return {129name: 'virtual-module',130resolveId(id) {131if (id === virtualModuleId) return resolvedId132},133load(id) {134if (id === resolvedId) {135return `export const msg = "from virtual module"`136}137},138}139}140```141142Usage:143144```ts145import { msg } from 'virtual:my-module'146```147148Convention: prefix user-facing path with `virtual:`, prefix resolved id with `\0`.149150## Plugin Ordering151152Use `enforce` to control execution order:153154```ts155{156name: 'pre-plugin',157enforce: 'pre', // runs before core plugins158}159160{161name: 'post-plugin',162enforce: 'post', // runs after build plugins163}164```165166Order: Alias → `enforce: 'pre'` → Core → User (no enforce) → Build → `enforce: 'post'` → Post-build167168## Conditional Application169170```ts171{172name: 'build-only',173apply: 'build', // or 'serve'174}175176// Function form:177{178apply(config, { command }) {179return command === 'build' && !config.build.ssr180}181}182```183184## Universal Hooks (from Rolldown)185186These work in both dev and build:187188- `resolveId(id, importer)` - Resolve import paths189- `load(id)` - Load module content190- `transform(code, id)` - Transform module code191192```ts193transform(code, id) {194if (id.endsWith('.custom')) {195return { code: compile(code), map: null }196}197}198```199200## Client-Server Communication201202Server to client:203204```ts205configureServer(server) {206server.ws.send('my:event', { msg: 'hello' })207}208```209210Client side:211212```ts213if (import.meta.hot) {214import.meta.hot.on('my:event', (data) => {215console.log(data.msg)216})217}218```219220Client to server:221222```ts223// Client224import.meta.hot.send('my:from-client', { msg: 'Hey!' })225226// Server227server.ws.on('my:from-client', (data, client) => {228client.send('my:ack', { msg: 'Got it!' })229})230```231232<!--233Source references:234- https://vite.dev/guide/api-plugin235-->236