From af0b1feab6fe080aaee86e16886d40bd0326cfc9 Mon Sep 17 00:00:00 2001 From: andylizi Date: Mon, 6 Sep 2021 17:56:10 +0800 Subject: [PATCH 1/2] feat: allow `apply` condition to be a function --- docs/guide/api-plugin.md | 8 ++++++++ docs/guide/using-plugins.md | 8 ++++++++ packages/vite/src/node/config.ts | 10 +++++++++- packages/vite/src/node/plugin.ts | 4 ++-- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/docs/guide/api-plugin.md b/docs/guide/api-plugin.md index 8de592b53bc6f6..a8638d307afc4d 100644 --- a/docs/guide/api-plugin.md +++ b/docs/guide/api-plugin.md @@ -414,6 +414,14 @@ function myPlugin() { } ``` +A function can also be used for more precise control: + +```js +apply(config, { command }) { + return command === 'build' && !config.build.ssr +} +``` + ## Rollup Plugin Compatibility A fair number of Rollup plugins will work directly as a Vite plugin (e.g. `@rollup/plugin-alias` or `@rollup/plugin-json`), but not all of them, since some plugin hooks do not make sense in an unbundled dev server context. diff --git a/docs/guide/using-plugins.md b/docs/guide/using-plugins.md index ca4faf9d501718..875be13646f1f0 100644 --- a/docs/guide/using-plugins.md +++ b/docs/guide/using-plugins.md @@ -82,6 +82,14 @@ export default defineConfig({ }) ``` +A function can also be used for more precise control: + +```js +apply(config, { command }) { + return command === 'build' && !config.build.ssr +} +``` + ## Building Plugins Check out the [Plugins API Guide](./api-plugin.md) for documentation about creating plugins. diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 19274a725d785a..272331e47b87eb 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -285,7 +285,15 @@ export async function resolveConfig( // resolve plugins const rawUserPlugins = (config.plugins || []).flat().filter((p) => { - return p && (!p.apply || p.apply === command) + if (!p) { + return false + } else if (!p.apply) { + return true + } else if (typeof p.apply === 'function') { + return p.apply({ ...config, mode }, configEnv) + } else { + return p.apply === command + } }) as Plugin[] const [prePlugins, normalPlugins, postPlugins] = sortUserPlugins(rawUserPlugins) diff --git a/packages/vite/src/node/plugin.ts b/packages/vite/src/node/plugin.ts index 012c32e0fa6dfb..3af59bbd09b62c 100644 --- a/packages/vite/src/node/plugin.ts +++ b/packages/vite/src/node/plugin.ts @@ -50,9 +50,9 @@ export interface Plugin extends RollupPlugin { */ enforce?: 'pre' | 'post' /** - * Apply the plugin only for serve or for build. + * Apply the plugin only for serve or build, or on certain conditions. */ - apply?: 'serve' | 'build' + apply?: 'serve' | 'build' | ((config: UserConfig, env: ConfigEnv) => boolean) /** * 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 From e41637d16c1ef3c449ee3ca6bd45131af653ac70 Mon Sep 17 00:00:00 2001 From: andylizi Date: Mon, 6 Sep 2021 19:19:20 +0800 Subject: [PATCH 2/2] docs: apply review suggestions --- docs/guide/api-plugin.md | 1 + docs/guide/using-plugins.md | 8 -------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/docs/guide/api-plugin.md b/docs/guide/api-plugin.md index a8638d307afc4d..0a4696595430af 100644 --- a/docs/guide/api-plugin.md +++ b/docs/guide/api-plugin.md @@ -418,6 +418,7 @@ A function can also be used for more precise control: ```js apply(config, { command }) { + // apply only on build but not for SSR return command === 'build' && !config.build.ssr } ``` diff --git a/docs/guide/using-plugins.md b/docs/guide/using-plugins.md index 875be13646f1f0..ca4faf9d501718 100644 --- a/docs/guide/using-plugins.md +++ b/docs/guide/using-plugins.md @@ -82,14 +82,6 @@ export default defineConfig({ }) ``` -A function can also be used for more precise control: - -```js -apply(config, { command }) { - return command === 'build' && !config.build.ssr -} -``` - ## Building Plugins Check out the [Plugins API Guide](./api-plugin.md) for documentation about creating plugins.