Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rename plugin enforce to order #9670

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/config/shared-options.md
Expand Up @@ -297,7 +297,7 @@ Specify additional [picomatch patterns](https://github.com/micromatch/picomatch#

- They will be excluded from the plugin transform pipeline when referenced from HTML or directly requested over `fetch` or XHR.

- Importing them from JS will return their resolved URL string (this can be overwritten if you have a `enforce: 'pre'` plugin to handle the asset type differently).
- Importing them from JS will return their resolved URL string (this can be overwritten if you have a `order: 'pre'` plugin to handle the asset type differently).

The built-in asset type list can be found [here](https://github.com/vitejs/vite/blob/main/packages/vite/src/node/constants.ts).

Expand Down
12 changes: 6 additions & 6 deletions docs/guide/api-plugin.md
Expand Up @@ -441,14 +441,14 @@ Vite plugins can also provide hooks that serve Vite-specific purposes. These hoo

## Plugin Ordering

A Vite plugin can additionally specify an `enforce` property (similar to webpack loaders) to adjust its application order. The value of `enforce` can be either `"pre"` or `"post"`. The resolved plugins will be in the following order:
A Vite plugin can additionally specify an `order` property (similar to webpack loaders) to adjust its application order. The value of `order` can be either `"pre"` or `"post"`. The resolved plugins will be in the following order:

- Alias
- User plugins with `enforce: 'pre'`
- User plugins with `order: 'pre'`
- Vite core plugins
- User plugins without enforce value
- User plugins without `order` value
- Vite build plugins
- User plugins with `enforce: 'post'`
- User plugins with `order: 'post'`
- Vite post build plugins (minify, manifest, reporting)

## Conditional Application
Expand Down Expand Up @@ -482,7 +482,7 @@ In general, as long as a Rollup plugin fits the following criteria then it shoul
- It doesn't use the [`moduleParsed`](https://rollupjs.org/guide/en/#moduleparsed) hook.
- It doesn't have strong coupling between bundle-phase hooks and output-phase hooks.

If a Rollup plugin only makes sense for the build phase, then it can be specified under `build.rollupOptions.plugins` instead. It will work the same as a Vite plugin with `enforce: 'post'` and `apply: 'build'`.
If a Rollup plugin only makes sense for the build phase, then it can be specified under `build.rollupOptions.plugins` instead. It will work the same as a Vite plugin with `order: 'post'` and `apply: 'build'`.

You can also augment an existing Rollup plugin with Vite-only properties:

Expand All @@ -495,7 +495,7 @@ export default defineConfig({
plugins: [
{
...example(),
enforce: 'post',
order: 'post',
apply: 'build'
}
]
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/using-plugins.md
Expand Up @@ -40,7 +40,7 @@ You can also find plugins that follow the [recommended conventions](./api-plugin

## Enforcing Plugin Ordering

For compatibility with some Rollup plugins, it may be needed to enforce the order of the plugin or only apply at build time. This should be an implementation detail for Vite plugins. You can enforce the position of a plugin with the `enforce` modifier:
For compatibility with some Rollup plugins, it may be needed to enforce the order of the plugin or only apply at build time. This should be an implementation detail for Vite plugins. You can change the position of a plugin with the `order` modifier:

- `pre`: invoke plugin before Vite core plugins
- default: invoke plugin after Vite core plugins
Expand All @@ -55,13 +55,13 @@ export default defineConfig({
plugins: [
{
...image(),
enforce: 'pre'
order: 'pre'
}
]
})
```

Check out [Plugins API Guide](./api-plugin.md#plugin-ordering) for detailed information, and look out for the `enforce` label and usage instructions for popular plugins in the [Vite Rollup Plugins](https://vite-rollup-plugins.patak.dev) compatibility listing.
Check out [Plugins API Guide](./api-plugin.md#plugin-ordering) for detailed information, and look out for the `order` label and usage instructions for popular plugins in the [Vite Rollup Plugins](https://vite-rollup-plugins.patak.dev) compatibility listing.

## Conditional Application

Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/config.ts
Expand Up @@ -838,8 +838,9 @@ export function sortUserPlugins(

if (plugins) {
plugins.flat().forEach((p) => {
if (p.enforce === 'pre') prePlugins.push(p)
else if (p.enforce === 'post') postPlugins.push(p)
const order = p.order || p.enforce
antfu marked this conversation as resolved.
Show resolved Hide resolved
if (order === 'pre') prePlugins.push(p)
else if (order === 'post') postPlugins.push(p)
else normalPlugins.push(p)
})
}
Expand Down
10 changes: 7 additions & 3 deletions packages/vite/src/node/plugin.ts
Expand Up @@ -39,17 +39,21 @@ import type { ConfigEnv, ResolvedConfig } from './'
*/
export interface Plugin extends RollupPlugin {
/**
* Enforce plugin invocation tier similar to webpack loaders.
* Change plugin invocation order similar to webpack loaders.
*
* Plugin invocation order:
* - alias resolution
* - `enforce: 'pre'` plugins
* - `order: 'pre'` plugins
* - vite core plugins
* - normal plugins
* - vite build plugins
* - `enforce: 'post'` plugins
* - `order: 'post'` plugins
* - vite build post plugins
*/
order?: 'pre' | 'post' | null
/**
* @deprecated renamed to `order`
*/
enforce?: 'pre' | 'post'
/**
* Apply the plugin only for serve or build, or on certain conditions.
Expand Down
2 changes: 1 addition & 1 deletion playground/optimize-deps/vite.config.js
Expand Up @@ -79,7 +79,7 @@ module.exports = {
{
name: 'polyfill-named-fs-build',
apply: 'build',
enforce: 'pre',
order: 'pre',
load(id) {
if (id === '__vite-browser-external') {
return `export default {}; export function readFileSync() {}`
Expand Down
2 changes: 1 addition & 1 deletion playground/ssr-deps/server.js
Expand Up @@ -49,7 +49,7 @@ export async function createServer(root = process.cwd(), hmrPort) {
plugins: [
{
name: 'dep-virtual',
enforce: 'pre',
order: 'pre',
resolveId(id) {
if (id === 'pkg-exports/virtual') {
return 'pkg-exports/virtual'
Expand Down
2 changes: 1 addition & 1 deletion playground/ssr-vue/vite.config.js
Expand Up @@ -67,7 +67,7 @@ export default defineConfig(({ command, ssrBuild }) => ({
const virtualId = '\0virtual:ssr-vue-built-url'
return {
name: 'built-url',
enforce: 'post',
order: 'post',
configResolved(_config) {
config = _config
},
Expand Down
2 changes: 0 additions & 2 deletions playground/worker/vite.config-es.js
Expand Up @@ -3,7 +3,6 @@ const vite = require('vite')

module.exports = vite.defineConfig({
base: '/es/',
enforce: 'pre',
worker: {
format: 'es',
plugins: [vueJsx()],
Expand All @@ -28,7 +27,6 @@ module.exports = vite.defineConfig({
plugins: [
{
name: 'resolve-format-es',

transform(code, id) {
if (id.includes('main.js')) {
return code.replace(
Expand Down