From db42d7709737e4b3cfb8563d20361a9e89310737 Mon Sep 17 00:00:00 2001
From: Anthony Fu <anthonyfu117@hotmail.com>
Date: Sun, 14 Aug 2022 23:23:38 +0800
Subject: [PATCH 1/2] feat: rename plugin `enforce` to `order`

---
 docs/config/shared-options.md           |  2 +-
 docs/guide/api-plugin.md                | 12 ++++++------
 docs/guide/using-plugins.md             |  6 +++---
 packages/vite/src/node/config.ts        |  5 +++--
 packages/vite/src/node/plugin.ts        | 10 +++++++---
 playground/optimize-deps/vite.config.js |  2 +-
 playground/ssr-deps/server.js           |  2 +-
 playground/ssr-vue/vite.config.js       |  2 +-
 playground/worker/vite.config-es.js     |  2 --
 9 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/docs/config/shared-options.md b/docs/config/shared-options.md
index 8f7d151499e906..a3ad11ed50b714 100644
--- a/docs/config/shared-options.md
+++ b/docs/config/shared-options.md
@@ -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).
 
diff --git a/docs/guide/api-plugin.md b/docs/guide/api-plugin.md
index 89c71cfdeecee7..f86cb041a77088 100644
--- a/docs/guide/api-plugin.md
+++ b/docs/guide/api-plugin.md
@@ -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
@@ -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:
 
@@ -495,7 +495,7 @@ export default defineConfig({
   plugins: [
     {
       ...example(),
-      enforce: 'post',
+      order: 'post',
       apply: 'build'
     }
   ]
diff --git a/docs/guide/using-plugins.md b/docs/guide/using-plugins.md
index 39c81dc24f709b..702d3eb557f86c 100644
--- a/docs/guide/using-plugins.md
+++ b/docs/guide/using-plugins.md
@@ -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
@@ -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
 
diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts
index f29d06e27e29e2..bd32eadccc1531 100644
--- a/packages/vite/src/node/config.ts
+++ b/packages/vite/src/node/config.ts
@@ -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
+      if (order === 'pre') prePlugins.push(p)
+      else if (order === 'post') postPlugins.push(p)
       else normalPlugins.push(p)
     })
   }
diff --git a/packages/vite/src/node/plugin.ts b/packages/vite/src/node/plugin.ts
index 69b19415b0daa7..ed8627fe313380 100644
--- a/packages/vite/src/node/plugin.ts
+++ b/packages/vite/src/node/plugin.ts
@@ -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.
diff --git a/playground/optimize-deps/vite.config.js b/playground/optimize-deps/vite.config.js
index a44ca2ef2449d6..69a9b66ac9eabc 100644
--- a/playground/optimize-deps/vite.config.js
+++ b/playground/optimize-deps/vite.config.js
@@ -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() {}`
diff --git a/playground/ssr-deps/server.js b/playground/ssr-deps/server.js
index d82f80c599583b..f26e2a9bf4302c 100644
--- a/playground/ssr-deps/server.js
+++ b/playground/ssr-deps/server.js
@@ -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'
diff --git a/playground/ssr-vue/vite.config.js b/playground/ssr-vue/vite.config.js
index c8be7320c8a9b0..e3729b276782dc 100644
--- a/playground/ssr-vue/vite.config.js
+++ b/playground/ssr-vue/vite.config.js
@@ -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
         },
diff --git a/playground/worker/vite.config-es.js b/playground/worker/vite.config-es.js
index 6d6704de0bc213..b8ba8521a1d3cc 100644
--- a/playground/worker/vite.config-es.js
+++ b/playground/worker/vite.config-es.js
@@ -3,7 +3,6 @@ const vite = require('vite')
 
 module.exports = vite.defineConfig({
   base: '/es/',
-  enforce: 'pre',
   worker: {
     format: 'es',
     plugins: [vueJsx()],
@@ -28,7 +27,6 @@ module.exports = vite.defineConfig({
   plugins: [
     {
       name: 'resolve-format-es',
-
       transform(code, id) {
         if (id.includes('main.js')) {
           return code.replace(

From c145ac8e5c43185cada0c7f04bd6522c42216fff Mon Sep 17 00:00:00 2001
From: Anthony Fu <anthonyfu117@hotmail.com>
Date: Mon, 15 Aug 2022 10:14:56 +0800
Subject: [PATCH 2/2] Update packages/vite/src/node/config.ts

Co-authored-by: patak <matias.capeletto@gmail.com>
---
 packages/vite/src/node/config.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts
index bd32eadccc1531..340c0891245c4c 100644
--- a/packages/vite/src/node/config.ts
+++ b/packages/vite/src/node/config.ts
@@ -838,7 +838,7 @@ export function sortUserPlugins(
 
   if (plugins) {
     plugins.flat().forEach((p) => {
-      const order = p.order || p.enforce
+      const order = p.order ?? p.enforce
       if (order === 'pre') prePlugins.push(p)
       else if (order === 'post') postPlugins.push(p)
       else normalPlugins.push(p)