Skip to content

Commit

Permalink
refactor: applyToEnvironment -> perEnvironment hook
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Nov 13, 2024
1 parent 95c4b3c commit beb2ff9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions docs/guide/api-environment-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ The hook can choose to:
## 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
const UnoCssPlugin = () => {
Expand All @@ -141,7 +141,7 @@ const UnoCssPlugin = () => {
configureServer() {
// use global hooks normally
},
applyToEnvironment(environment) {
perEnvironment(environment) {
// return true if this plugin should be active in this environment,
// or return a new plugin to replace it.
// if the hook is not used, the plugin is active in all environments
Expand All @@ -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
import { nonShareablePlugin } from 'non-shareable-plugin'
Expand All @@ -162,7 +162,7 @@ export default defineConfig({
plugins: [
{
name: 'per-environment-plugin',
applyToEnvironment(environment) {
perEnvironment(environment) {
return nonShareablePlugin({ outputName: environment.name })
},
},
Expand Down
10 changes: 5 additions & 5 deletions packages/vite/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export interface Plugin<A = any> extends RollupPlugin<A> {
* By default, the plugin is active in all environments
* @experimental
*/
applyToEnvironment?: (
perEnvironment?: (
environment: PartialEnvironment,
) => boolean | Promise<boolean> | PluginOption
/**
Expand Down Expand Up @@ -334,8 +334,8 @@ export async function resolveEnvironmentPlugins(
): Promise<Plugin[]> {
const environmentPlugins: Plugin[] = []
for (const plugin of environment.getTopLevelConfig().plugins) {
if (plugin.applyToEnvironment) {
const applied = await plugin.applyToEnvironment(environment)
if (plugin.perEnvironment) {
const applied = await plugin.perEnvironment(environment)
if (!applied) {
continue
}
Expand All @@ -358,12 +358,12 @@ export async function resolveEnvironmentPlugins(
*/
export function perEnvironmentPlugin(
name: string,
applyToEnvironment: (
perEnvironment: (
environment: PartialEnvironment,
) => boolean | Promise<boolean> | PluginOption,
): Plugin {
return {
name,
applyToEnvironment,
perEnvironment,
}
}
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function manifestPlugin(): Plugin {

perEnvironmentStartEndDuringDev: true,

applyToEnvironment(environment) {
perEnvironment(environment) {
return !!environment.config.build.manifest
},

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/terser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
return {
name: 'vite:terser',

applyToEnvironment(environment) {
perEnvironment(environment) {
// We also need the plugin even if minify isn't 'terser' as we force
// terser in plugin-legacy
return !!environment.config.build.minify
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/ssrManifestPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function ssrManifestPlugin(): Plugin {
return {
name: 'vite:ssr-manifest',

applyToEnvironment(environment) {
perEnvironment(environment) {
return !!environment.config.build.ssrManifest
},

Expand Down

0 comments on commit beb2ff9

Please sign in to comment.