skip to content

In a Vite plugin, what do the enforce and apply properties control, and where does a plugin without them run among Vite's core plugins?

level: middleimportance: should knowfreq 8%

answer

  1. tiers first, then array order
  2. alias, pre, core, normal, build, post
  3. default tier sits after core
  4. serve, build or a function
  5. a hook's order is a separate knob

basics

~20 s

enforce sets a plugin's tier: pre runs before Vite's core plugins, post after its build plugins, and no enforce right after the core plugins. apply keeps a plugin only for serve, for build, or where a function returns true.

solid answer

~40 s

Vite resolves plugins in this order: alias handling, user plugins with `enforce: 'pre'`, Vite core plugins, user plugins with no `enforce`, Vite build plugins, user plugins with `enforce: 'post'`, then Vite's post-build plugins (minify, manifest, reporting). Inside one tier, plugins keep their `plugins` array order. A plugin with no `enforce` therefore sees a `.ts` file after Vite's core TypeScript handling; one that needs the raw source uses `'pre'`. `apply: 'serve'` or `apply: 'build'` drops the plugin entirely for the other command, and `apply(config, { command, mode })` returning a boolean gives finer control, such as the client build but not SSR. A hook's own `order: 'pre' | 'post'` is separate: it reorders that one hook across all plugins without moving the plugin's tier.

go deeper

for a junior

Recall that plugins run in a defined order, that enforce: 'pre' puts one before Vite's own plugins, and that apply can limit a plugin to dev or to build.

for a middle

Lay out the sequence from alias handling to post-build plugins, say what a default-tier transform sees of a .ts file, and contrast apply's string and function forms.

for a senior

Diagnose a transform that receives already-compiled code or misses an id by checking its tier and hook order, and adapt third-party plugins with enforce and apply instead of forking them.

for a principal

Set conventions for shared in-house plugins, such as which tier each may claim and when hook-level order is allowed, so plugins composed by many teams stay predictable.

Several hook kinds make order decisive. `resolveId` and `load` stop at the first plugin that answers, and every `transform` receives the output of the one before it. Where a plugin sits therefore decides which ids it gets to own and what code it is given. ## The tiers `enforce` creates Vite builds the final plugin list in this order: 1. Alias handling 2. User plugins with `enforce: 'pre'` 3. Vite core plugins 4. User plugins with no `enforce` 5. Vite build plugins 6. User plugins with `enforce: 'post'` 7. Vite post-build plugins (minify, manifest, reporting) Within one tier, plugins keep the order they have in the `plugins` array, so two default-tier plugins that both transform `.md` files run in the order they are listed, and swapping them changes the result. In dev, a few internal server-only plugins, such as Vite's import analysis, always run after everything else. | Tier | What its `transform` receives for a `.ts` file | Typical use | |---|---|---| | `enforce: 'pre'` | the original TypeScript | pre-processing raw source, claiming ids before Vite's resolver | | no `enforce` | JavaScript after Vite's core TypeScript and JSX handling | most plugins | | `enforce: 'post'` | code after core, normal and build-plugin transforms | late rewrites, inspecting output before minify and manifest | So the **default tier** sits after Vite's core plugins: by the time a normal plugin's `transform` runs, Vite has already resolved the id, stripped TypeScript types and run its first pass over CSS and asset imports. ## `apply`: serve, build or a decision By default a plugin is invoked for both the dev server and the build. `apply` narrows that: - `apply: 'serve'` keeps the plugin only for the dev server (`vite`, `vite dev`, `vite serve`). - `apply: 'build'` keeps it only for `vite build`. - `apply(config, env)` returns a boolean; `env` carries `command`, `mode`, `isSsrBuild` and `isPreview`. ```ts const consentBanner = { name: 'newsroom-consent', apply(config, { command }) { // client build only, not the SSR build return command === 'build' && !config.build?.ssr }, } ``` Vite evaluates `apply` while it resolves the config, **before** any `config` hook runs. A plugin it rejects is dropped like a falsy entry in `plugins`: none of its hooks run for that command, not even `config`. ## `enforce` is not hook `order` Object hooks accept `order: 'pre' | 'post'`. For each hook, Vite gathers every plugin that implements it and runs those marked `'pre'` first, then the unmarked ones, then those marked `'post'`, keeping plugin order inside each group. A normal-tier plugin with `transform: { order: 'pre', handler }` therefore runs that one transform ahead of unmarked transforms, core ones included, while its other hooks stay in the default tier. - Use **`enforce`** to move a whole plugin. - Use a hook's **`order`** to move a single hook. **Re-tiering someone else's plugin.** You can change a third-party plugin's tier or command without forking it, because the factory returns a plain object: ```ts plugins: [{ ...markdownPlugin(), enforce: 'pre', apply: 'build' }] ``` This is how a Rollup-style plugin that must run before Vite's core handling, or only during the build, is adapted. ## Choosing a tier in practice - **Stay in the default tier** unless you have a reason; most plugins want resolved ids and plain JavaScript, which is what that tier provides. - **Use `'pre'`** when the plugin must see source before Vite touches it, or must answer `resolveId` before Vite's own resolver claims an id. A markdown pre-processor that rewrites raw `.md` text before another plugin compiles it is a typical case. - **Use `'post'`** for late, whole-module rewrites or for build-time inspection of what the other plugins produced. - **Remember that tiers apply to every hook**, including `config`: Vite runs `config` hooks in the same pre, normal, post order, so a `'post'` plugin's `config` sees what earlier plugins returned. ## Mistakes to avoid - Assuming the `plugins` array order alone decides when a plugin runs. - Expecting a default-tier `transform` to receive raw TypeScript. - Using `enforce: 'post'` to process minified output; minification comes later still. - Believing `apply: 'build'` only silences dev-server hooks, when it removes the whole plugin from the dev server.

  • Does transform: { order: 'pre', handler } on a plugin without enforce move the whole plugin before Vite's core plugins?
    No. `order` sorts one hook. For `transform`, Vite collects every plugin's hook, runs the `'pre'` ones first, then unmarked ones, then `'post'`, keeping plugin order inside each group. That transform therefore runs ahead of unmarked core transforms, but the plugin's `resolveId`, `load` and other hooks stay in the default tier. Use `enforce` to move a plugin and `order` to move one hook.
  • When is apply evaluated, and what happens to a plugin it rejects?
    While Vite resolves the config, before any `config` hook runs. A rejected plugin is filtered out exactly like a falsy entry in `plugins`, so none of its hooks run for that command, including `config`. The function form receives the user config (with the resolved mode) and the config env: `command`, `mode`, `isSsrBuild` and `isPreview`.

  • Plugins run strictly in plugins-array order whatever their enforce value.
  • A plugin without enforce runs before Vite's core plugins.
  • enforce: 'post' runs after minification, so it sees the final minified chunks.
  • apply: 'build' only skips configureServer; the plugin's other hooks still run in dev.
  • Setting a hook's order property changes the plugin's enforce tier.