mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 14:48:41 +00:00
refactor: applyToEnvironment -> perEnvironment hook
This commit is contained in:
parent
95c4b3c371
commit
beb2ff9223
@ -128,7 +128,7 @@ The hook can choose to:
|
|||||||
|
|
||||||
## Per-environment Plugins
|
## Per-environment Plugins
|
||||||
|
|
||||||
A plugin can define what are the environments it should apply to with the `applyToEnvironment` function.
|
A plugin can define what are the environments it should apply to with the `perEnvironment` function.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const UnoCssPlugin = () => {
|
const UnoCssPlugin = () => {
|
||||||
@ -141,7 +141,7 @@ const UnoCssPlugin = () => {
|
|||||||
configureServer() {
|
configureServer() {
|
||||||
// use global hooks normally
|
// use global hooks normally
|
||||||
},
|
},
|
||||||
applyToEnvironment(environment) {
|
perEnvironment(environment) {
|
||||||
// return true if this plugin should be active in this environment,
|
// return true if this plugin should be active in this environment,
|
||||||
// or return a new plugin to replace it.
|
// or return a new plugin to replace it.
|
||||||
// if the hook is not used, the plugin is active in all environments
|
// if the hook is not used, the plugin is active in all environments
|
||||||
@ -153,7 +153,7 @@ const UnoCssPlugin = () => {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
If a plugin isn't environment aware and has state that isn't keyed on the current environment, the `applyToEnvironment` hook allows to easily make it per-environment.
|
If a plugin isn't environment aware and has state that isn't keyed on the current environment, the `perEnvironment` hook allows to easily make it per-environment.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import { nonShareablePlugin } from 'non-shareable-plugin'
|
import { nonShareablePlugin } from 'non-shareable-plugin'
|
||||||
@ -162,7 +162,7 @@ export default defineConfig({
|
|||||||
plugins: [
|
plugins: [
|
||||||
{
|
{
|
||||||
name: 'per-environment-plugin',
|
name: 'per-environment-plugin',
|
||||||
applyToEnvironment(environment) {
|
perEnvironment(environment) {
|
||||||
return nonShareablePlugin({ outputName: environment.name })
|
return nonShareablePlugin({ outputName: environment.name })
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -207,7 +207,7 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
|
|||||||
* By default, the plugin is active in all environments
|
* By default, the plugin is active in all environments
|
||||||
* @experimental
|
* @experimental
|
||||||
*/
|
*/
|
||||||
applyToEnvironment?: (
|
perEnvironment?: (
|
||||||
environment: PartialEnvironment,
|
environment: PartialEnvironment,
|
||||||
) => boolean | Promise<boolean> | PluginOption
|
) => boolean | Promise<boolean> | PluginOption
|
||||||
/**
|
/**
|
||||||
@ -334,8 +334,8 @@ export async function resolveEnvironmentPlugins(
|
|||||||
): Promise<Plugin[]> {
|
): Promise<Plugin[]> {
|
||||||
const environmentPlugins: Plugin[] = []
|
const environmentPlugins: Plugin[] = []
|
||||||
for (const plugin of environment.getTopLevelConfig().plugins) {
|
for (const plugin of environment.getTopLevelConfig().plugins) {
|
||||||
if (plugin.applyToEnvironment) {
|
if (plugin.perEnvironment) {
|
||||||
const applied = await plugin.applyToEnvironment(environment)
|
const applied = await plugin.perEnvironment(environment)
|
||||||
if (!applied) {
|
if (!applied) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -358,12 +358,12 @@ export async function resolveEnvironmentPlugins(
|
|||||||
*/
|
*/
|
||||||
export function perEnvironmentPlugin(
|
export function perEnvironmentPlugin(
|
||||||
name: string,
|
name: string,
|
||||||
applyToEnvironment: (
|
perEnvironment: (
|
||||||
environment: PartialEnvironment,
|
environment: PartialEnvironment,
|
||||||
) => boolean | Promise<boolean> | PluginOption,
|
) => boolean | Promise<boolean> | PluginOption,
|
||||||
): Plugin {
|
): Plugin {
|
||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
applyToEnvironment,
|
perEnvironment,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ export function manifestPlugin(): Plugin {
|
|||||||
|
|
||||||
perEnvironmentStartEndDuringDev: true,
|
perEnvironmentStartEndDuringDev: true,
|
||||||
|
|
||||||
applyToEnvironment(environment) {
|
perEnvironment(environment) {
|
||||||
return !!environment.config.build.manifest
|
return !!environment.config.build.manifest
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
|
|||||||
return {
|
return {
|
||||||
name: 'vite:terser',
|
name: 'vite:terser',
|
||||||
|
|
||||||
applyToEnvironment(environment) {
|
perEnvironment(environment) {
|
||||||
// We also need the plugin even if minify isn't 'terser' as we force
|
// We also need the plugin even if minify isn't 'terser' as we force
|
||||||
// terser in plugin-legacy
|
// terser in plugin-legacy
|
||||||
return !!environment.config.build.minify
|
return !!environment.config.build.minify
|
||||||
|
@ -25,7 +25,7 @@ export function ssrManifestPlugin(): Plugin {
|
|||||||
return {
|
return {
|
||||||
name: 'vite:ssr-manifest',
|
name: 'vite:ssr-manifest',
|
||||||
|
|
||||||
applyToEnvironment(environment) {
|
perEnvironment(environment) {
|
||||||
return !!environment.config.build.ssrManifest
|
return !!environment.config.build.ssrManifest
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user