-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
docs: missing changes guides #18491
docs: missing changes guides #18491
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
Give us feedback at [Environment API feedback discussion](https://github.com/vitejs/vite/discussions/16358) | ||
::: | ||
|
||
// TODO: | ||
See [Shared plugins during build](/guide/api-environment.md#shared-plugins-during-build). | ||
|
||
Affect scope: `Vite Plugin Authors` | ||
|
@@ -15,8 +14,68 @@ Affect scope: `Vite Plugin Authors` | |
|
||
## Motivation | ||
|
||
// TODO: | ||
Align dev and build plugin pipelines. | ||
|
||
## Migration Guide | ||
|
||
// TODO: | ||
To be able to share plugins across environments, plugin state must be keyed by the current environment. A plugin of the following form will count the number of transformed modules across all environments. | ||
|
||
```js | ||
function CountTransformedModulesPlugin() { | ||
let transformedModules | ||
return { | ||
name: 'count-transformed-modules', | ||
buildStart() { | ||
transformedModules = 0 | ||
}, | ||
transform(id) { | ||
transformedModules++ | ||
}, | ||
buildEnd() { | ||
console.log(transformedModules) | ||
}, | ||
} | ||
} | ||
``` | ||
|
||
If we instead want to count the number of transformed modules for each environment, we need to keep a map: | ||
|
||
```js | ||
function PerEnvironmentCountTransformedModulesPlugin() { | ||
const state = new Map<Environment, { count: number }>() | ||
return { | ||
name: 'count-transformed-modules', | ||
perEnvironmentBuildStartEnd: true, | ||
buildStart() { | ||
state.set(this.environment, { count: 0 }) | ||
} | ||
transform(id) { | ||
state.get(this.environment).count++ | ||
}, | ||
buildEnd() { | ||
console.log(this.environment.name, state.get(this.environment).count) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
To simplify this pattern, internally in Vite, we use a `useEnvironmentState` helper: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is somewhat related to The implementation was like: I'm not sure if this would be needed commonly, but maybe something similar to this is needed when using some of the rollup plugins in npm. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We discussed with Anthony about a similar helper when we were trying to see how to refactor the reporter plugin: https://github.com/vitejs/vite/pull/17292/files The
The user now needs to know that a per-env plugin can't be in the main pipeline. And that a shared plugin (that actually shares data across env) needs to be always used as a regular plugin. I wonder if we should extend to {
name: 'per-environment-alias',
applyToEnvironment(environment) => aliasPlugin(environment.config.alias)
} If an object is returned instead of a boolean, it replaces the current plugin with it for this environment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, the user might sometime need to understand it. But I think that doesn't happen much and the decision of making shared plugins by default was good.
That would work too. I went with makeEnvironmentAwarePlugin('per-environment-alias', env => aliasPlugin(env)) because it felt more intuitive from a plugin user standpoint for me (the metal model is that this function makes the plugin env-aware). |
||
|
||
```js | ||
function PerEnvironmentCountTransformedModulesPlugin() { | ||
const state = usePerEnvironmentState<{ count: number }>(() => ({ count: 0 })) | ||
return { | ||
name: 'count-transformed-modules', | ||
perEnvironmentBuildStartEnd: true, | ||
buildStart() { | ||
state(this).count = 0 | ||
} | ||
transform(id) { | ||
state(this).count++ | ||
}, | ||
buildEnd() { | ||
console.log(this.environment.name, state(this).count) | ||
} | ||
} | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
perEnvironmentBuildStartEnd
still exist? The search result does not return any usage only the declarations: https://github.com/search?q=repo%3Avitejs%2Fvite+perEnvironmentBuildStartEnd&type=codeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, it is actually called
perEnvironmentStartEndDuringDev
. We need a better name for this. Any recommendations?About this option, the problem we have is that plugins my be expecting a single
buildStart
call during dev, even if there are multiple environments, so I don't think we can start calling it multiple times in Vite 6. A lot of times it will work though, because it will end up reseting multiple times the state, but it could fail too. Moving to multiplebuildStart
calls is something we haven't discuss much but I think it is a good idea to align dev and buildThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that was it. I don't have any better naming 😅. But I guess we should rename
perEnvironmentBuildStartEnd
toperEnvironmentStartEndDuringDev
and use the settings in the pluginContainer.