-
-
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
feat: extended applyToEnvironment and perEnvironmentPlugin #18544
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: 翠 / green <green@sapphi.red>
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
One thing to note about this design is that As I said in the description, with this approach, you can't return any kind of plugin from If we want to support any non-environment-aware vite plugin, that could be done by adding several of them in the root plugins: [
{
name: 'per-environment-plugin',
perEnvironment(environment: PartialEnvironment) {
// use environment.config if needed
return nonShareableVitePlugin()
}
}
] These could get We have two options to implement this:
Actually, if we do 2, then this hook could be directly I'll make this PR a draft and try to do the refactoring so we can discuss with something concrete in the table. |
I tried to implement this, but it doesn't work. If we call This is similar to why we never wanted to allow plugins to return other plugins in the
Ideally, we should move |
Taking a step back, does it make sense to ditch extending For example: export default defineConfig({
plugins: [
// create a new plugin per environment. do we need to pass the environment name?
// i think they can already retrieve that information from `this.environment`?
// these kinds of plugins likely wouldn't care about environments if needed. if they did,
// they could directly support shared instance in environments in vite instead
() => nonSharedPlugin()
]
}) I think we've discussed this before in the past, and maybe it is the easier way to solve this problem. Maybe only enabling this behaviour if If we don't want to plainly support functions (which could be (ab)use by third-party plugins), we could have |
I really like the idea of having functional plugins. They are the most ergonomic way to do per-environment plugins, by a fair margin. We implemented it and later on decided to remove because:
I can work on a PR to see how functional plugins would look like with the current state of Vite 6. We would need to keep |
Ah, if we want to be able to use these plugins internally, that would be a breaking change as everybody will see functional plugins in their pipeline (for example if we want to add |
@bluwy I started implementing functional plugins, but I think it is easier to just check the implementation change from functional to I don't think we should go with functional plugins. If you check the PR, the types for |
Pushed refactor: use PartialEnvironment in applyToEnvironmnet, so even without a refactoring, we are opening the door to move the Also after feat: call configResolved hook on new plugins, the plugins returned from I'm not sure if we should add support for this plugins to use |
After further thought about this, I think your implementation still seem to be the best middleground. However, personally, I wouldn't support I also think still that we should probably avoid supporting returning plugins from |
I think we need this hook. I agree with the sentiment (regarding other hooks like
The current { ...plugin, applyToEnvironment(environment) => environment.config.foo } So I think adding { ...plugin, branch(environment) => environment.config.foo } and {
name: 'per-environment-rollup-plugins',
branch(environment) => environment.build.rollupOptions.plugins
} Or other names like |
I wouldn't support |
This reverts commit 0e552f5.
Ok, I reverted the last commit, so |
I'm thinking that we should disallow any way for plugins to add plugins in the first place, or at least not make it easy to do so. Regardless if it's exposed via If we make it part of the hooks, it makes it easy to use the escape hatch when instead what they should really do is to use the To be clear in case it got confusing, I think |
Would you like to discuss this in the next Vite team meeting? If not, I think we could move forward with it |
I'm ok with merging if we remove the types for returning plugins in |
I think we shouldn't have only the helper function. We should follow the standard way hooks work in Rollup. We can discuss tomorrow then. |
Description
Start from this discussion for reference.
We need to allow users to use plugins that aren't prepared to be run concurrently for multiple environments (many rollup build plugins for example, that keep cross hooks state). This was easy with some of the previous iterations of the Environment API (for example when we could pass functional plugins, or when we had the
environmentPlugins
hook that added plugins after the current one).This PR proposes an extension to the current design that I think feels more integrated. The
applyToEnvironment
hook currently only returns a boolean to decide if a plugin is active or not in an environment. The idea is to also allow returnPluginOption
from it, in which case, this plugins are used instead.We could make the
PluginOption
more strict, but I don't know if it is worthy.The returned plugins are inserted replacing the current plugin.
enforce
will be ignored. If a user would like to rearrange them, they can use per hookorder
or aenforce
in the plugin that has theapplyToEnvironment
.We also ignore the
config
,configEnvironment
,configureServer
,configResolved
hooks because these have already been called whenapplyToEnvironment
is run.We also ignore
applyToEnvironment
in these.I took the opportunity to also allow this hook to be async.
There is also a new
perEnvironmentPlugin
helper (themakeEnvironmentAwarePlugin
that @sapphi-red proposed).With this PR, we can also remove two TODOs and support per-environment
build.commonjsOptions
andbuild.rollupOptions.plugins
.I renamed
usePerEnvironmentState
toperEnvironmentState
and also expose it.