Skip to content
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(plugin-api): support async configResolved hooks (fixes #2949) #2951

Merged
merged 3 commits into from
Apr 13, 2021

Conversation

dominikg
Copy link
Contributor

Description

see #2949

Additional context

Should docs contain an example?

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@Shinigami92 Shinigami92 linked an issue Apr 11, 2021 that may be closed by this pull request
@Shinigami92 Shinigami92 added the p2-nice-to-have Not breaking anything but nice to have (priority) label Apr 11, 2021
@@ -376,11 +376,11 @@ export async function resolveConfig(
)

// call configResolved hooks
userPlugins.forEach((p) => {
for (const p of userPlugins) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use Promise.all here, as configResolved should not change the configure object anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@antfu I think it is better to avoid Promise.all as all other hooks are sequential, and plugins could set up an internal state that will be used by following plugins. For example for presets where you return a plugin that is an array of plugins. I think the performance gains here may not justify the possible subtle issues. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had Promise.all first but didn't want to change the documented sequential behavior.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With Promise.all they are still sequential for sync functions and async functions before their first await. I just thought configResolved is designed for plugins to grab the configuration and store it, and that's it. I am not sure if it's ideal to let all the plugins wait for each other for each hook.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a point, and in Rollup, there are a lot of parallel async hooks so we need to choose here.
I think that it was a good idea to make the config hook be sequential async (since the options hook in rollup is also sequential https://rollupjs.org/guide/en/#options). But for this one, I think there is no equivalent hook in rollup so we have to decide. I am fine with this one being parallel, we need to document this in the same way as rollup does in the Plugin API section. There is not mention of hooks being sequential or parallel in our docs at this point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the docs do mention sequential, that was the reason i didn't go with Promise.all because i didn't want to change it in case anyone already relies on it. But antfu is right, for sync functions all behaves the same, so i'm indifferent.

Docs: https://github.com/vitejs/vite/pull/2951/files#diff-4f815d9b4fc689178afacaefc7f464c1a2493e0896aad5916f2b025891dfb889R193

My main reasoning for having this hook as async too is that ResolvedConfig passed to configResolved has a lot more information available than UserConfig passed to config. eg isProduction and command.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, sorry, I only looked for parallel. I think Evan used sequential because it was sync here. @antfu if you think we can go ahead with making this one parallel, I am fine with it. If not, we should request a review from Evan to check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe it's better to have Evan's feedback first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could still merge this one as parallel, because later if it is decided to change it to sequential it wouldnt be a breacking change, no? It would be breacking if we changre from sequential to parallel. Or should this be a minor in both cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebased main and changed to parallel with Promise.all

@dominikg dominikg force-pushed the feat/async-configResolved branch from 06e008e to fac226c Compare April 12, 2021 18:51
patak-dev
patak-dev previously approved these changes Apr 12, 2021
@antfu antfu merged commit 8b38168 into vitejs:main Apr 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support async configResolved hook in plugins
4 participants