Options
All
  • Public
  • Public/Protected
  • All
Menu

Vite plugins extends the Rollup plugin interface with a few extra vite-specific options. A valid vite plugin is also a valid Rollup plugin. On the contrary, a Rollup plugin may or may NOT be a valid vite universal plugin, since some Rollup features do not make sense in an unbundled dev server context. That said, as long as a rollup plugin doesn't have strong coupling between its bundle phase and output phase hooks then it should just work (that means, most of them).

By default, the plugins are run during both serve and build. When a plugin is applied during serve, it will only run non output plugin hooks (see rollup type definition of {@link rollup#PluginHooks}). You can think of the dev server as only running const bundle = rollup.rollup() but never calling bundle.generate().

A plugin that expects to have different behavior depending on serve/build can export a factory function that receives the command being run via options.

If a plugin should be applied only for server or build, a function format config file can be used to conditional determine the plugins to use.

Type parameters

  • A = any

Hierarchy

Index

Properties

api?: A
apply?: "build" | "serve" | ((this: void, config: UserConfig, env: ConfigEnv) => boolean)

Apply the plugin only for serve or build, or on certain conditions.

augmentChunkHash: undefined | ObjectHook<(this: PluginContext, chunk: RenderedChunk) => string | void, {}>
banner: undefined | ObjectHook<AddonHook, {}>
cacheKey?: string
config?: ObjectHook<(this: void, config: UserConfig, env: ConfigEnv) => null | void | Omit<UserConfig, "plugins"> | Promise<null | void | Omit<UserConfig, "plugins">>, {}>

Modify vite config before it's resolved. The hook can either mutate the passed-in config directly, or return a partial config object that will be deeply merged into existing config.

Note: User plugins are resolved before running this hook so injecting other plugins inside the config hook will have no effect.

configResolved?: ObjectHook<(this: void, config: Readonly<Omit<UserConfig, "plugins" | "css" | "assetsInclude" | "optimizeDeps" | "worker" | "build"> & { appType: AppType; base: string; build: ResolvedBuildOptions; cacheDir: string; command: "build" | "serve"; configFile: undefined | string; configFileDependencies: string[]; css: ResolvedCSSOptions; env: Record<string, any>; envDir: string; esbuild: false | ESBuildOptions; experimental: ExperimentalOptions; inlineConfig: InlineConfig; isProduction: boolean; isWorker: boolean; logger: Logger; mode: string; optimizeDeps: DepOptimizationOptions; plugins: readonly Plugin<any>[]; preview: ResolvedPreviewOptions; publicDir: string; resolve: Required<ResolveOptions> & { alias: Alias[] }; root: string; server: ResolvedServerOptions; ssr: ResolvedSSROptions; worker: ResolvedWorkerOptions; assetsInclude: any; createResolver: any } & PluginHookUtils>) => void | Promise<void>, {}>

Use this hook to read and store the final resolved vite config.

configurePreviewServer?: ObjectHook<PreviewServerHook, {}>

Configure the preview server. The hook receives the PreviewServer instance. This can also be used to store a reference to the server for use in other hooks.

The hooks are called before other middlewares are applied. A hook can return a post hook that will be called after other middlewares are applied. Hooks can be async functions and will be called in series.

configureServer?: ObjectHook<ServerHook, {}>

Configure the vite server. The hook receives the ViteDevServer instance. This can also be used to store a reference to the server for use in other hooks.

The hooks will be called before internal middlewares are applied. A hook can return a post hook that will be called after internal middlewares are applied. Hook can be async functions and will be called in series.

enforce?: "pre" | "post"

Enforce plugin invocation tier similar to webpack loaders.

Plugin invocation order:

  • alias resolution
  • enforce: 'pre' plugins
  • vite core plugins
  • normal plugins
  • vite build plugins
  • enforce: 'post' plugins
  • vite build post plugins
footer: undefined | ObjectHook<AddonHook, {}>
generateBundle: undefined | ObjectHook<(this: PluginContext, ...parameters: [options: NormalizedOutputOptions, bundle: OutputBundle, isWrite: boolean]) => void | Promise<void>, {}>
handleHotUpdate?: ObjectHook<(this: void, ctx: HmrContext) => void | ModuleNode[] | Promise<void | ModuleNode[]>, {}>

Perform custom handling of HMR updates. The handler receives a context containing changed filename, timestamp, a list of modules affected by the file change, and the dev server instance.

  • The hook can return a filtered list of modules to narrow down the update. e.g. for a Vue SFC, we can narrow down the part to update by comparing the descriptors.

  • The hook can also return an empty array and then perform custom updates by sending a custom hmr payload via server.ws.send().

  • If the hook doesn't return a value, the hmr update will be performed as normal.

intro: undefined | ObjectHook<AddonHook, {}>
load?: ObjectHook<(this: PluginContext, id: string, options?: { ssr?: boolean }) => LoadResult | Promise<LoadResult>, {}>
moduleParsed?: ObjectHook<(this: PluginContext, ...parameters: [info: ModuleInfo]) => void | Promise<void>, { sequential?: boolean }>
name: string
outputOptions: undefined | ObjectHook<(this: PluginContext, options: OutputOptions) => NullValue | OutputOptions, {}>
outro: undefined | ObjectHook<AddonHook, {}>
renderChunk: undefined | ObjectHook<(this: PluginContext, ...parameters: [code: string, chunk: RenderedChunk, options: NormalizedOutputOptions, meta: { chunks: Record<string, RenderedChunk> }]) => string | NullValue | { code: string; map?: SourceMapInput } | Promise<string | NullValue | { code: string; map?: SourceMapInput }>, {}>
renderDynamicImport: undefined | ObjectHook<(this: PluginContext, options: { customResolution: null | string; format: InternalModuleFormat; moduleId: string; targetModuleId: null | string }) => NullValue | { left: string; right: string }, {}>
renderError: undefined | ObjectHook<(this: PluginContext, ...parameters: [error?: Error]) => void | Promise<void>, { sequential?: boolean }>
renderStart: undefined | ObjectHook<(this: PluginContext, ...parameters: [outputOptions: NormalizedOutputOptions, inputOptions: NormalizedInputOptions]) => void | Promise<void>, { sequential?: boolean }>
resolveDynamicImport?: ObjectHook<(this: PluginContext, ...parameters: [specifier: string | AstNode, importer: string, options: { attributes: Record<string, string> }]) => ResolveIdResult | Promise<ResolveIdResult>, {}>
resolveFileUrl: undefined | ObjectHook<ResolveFileUrlHook, {}>
resolveId?: ObjectHook<(this: PluginContext, source: string, importer: undefined | string, options: { attributes: Record<string, string>; custom?: CustomPluginOptions; isEntry: boolean; ssr?: boolean }) => ResolveIdResult | Promise<ResolveIdResult>, {}>

extend hooks with ssr flag

resolveImportMeta: undefined | ObjectHook<ResolveImportMetaHook, {}>
shouldTransformCachedModule?: ObjectHook<(this: PluginContext, ...parameters: [options: { ast: ProgramNode; code: string; id: string; meta: CustomPluginOptions; moduleSideEffects: boolean | "no-treeshake"; resolvedSources: ResolvedIdMap; syntheticNamedExports: string | boolean }]) => boolean | NullValue | Promise<boolean | NullValue>, {}>
transform?: ObjectHook<(this: TransformPluginContext, code: string, id: string, options?: { ssr?: boolean }) => TransformResult | Promise<TransformResult>, {}>
transformIndexHtml?: IndexHtmlTransform

Transform index.html. The hook receives the following arguments:

  • html: string
  • ctx?: vite.ServerContext (only present during serve)
  • bundle?: rollup.OutputBundle (only present during build)

It can either return a transformed string, or a list of html tag descriptors that will be injected into the <head> or <body>.

By default the transform is applied after vite's internal html transform. If you need to apply the transform before vite, use an object: { order: 'pre', handler: hook }

version?: string
watchChange?: ObjectHook<(this: PluginContext, ...parameters: [id: string, change: { event: ChangeEvent }]) => void | Promise<void>, { sequential?: boolean }>
writeBundle: undefined | ObjectHook<(this: PluginContext, ...parameters: [options: NormalizedOutputOptions, bundle: OutputBundle]) => void | Promise<void>, { sequential?: boolean }>

Methods

  • Parameters

    Returns void