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: hide optimized deps found during scan phase logs #7419

Merged
merged 6 commits into from Mar 24, 2022

Conversation

patak-dev
Copy link
Member

Description

As part of #7379, we modified the logs to make them more concise now that the user will generally not notice the extra time to scan and pre-bundle during cold start.

It now looks like this:

image

The logs can still be a bit noisy for projects like StoryBook, Cypress, Ladle, etc where there are a lot of newly discovered deps as the use explores the app:

image

IMO the only important log here is the last one. Since there is a page reload, we need to communicate to the user why we are doing so. But the rest of the logs feel more like implementation details to me. It isn't that useful for the user to know when a dependency has been pre-bundled if there is not a big noticeable delay in server start or during a rerun.

This PR hides these logs, and only shows them when debugging or when using --force. I think that having them when using force is a good idea since the user is focused on re-bundling the dependencies so it is good feedback in that case.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

@benmccann
Copy link
Collaborator

I agree with moving the logging behind debug flag. My one question about adding logging with --force is then if logs show up will people think something is happening only when they use --force since they don't see the logs without it?

@bluwy
Copy link
Member

bluwy commented Mar 23, 2022

I feel like we should still keep the logs, so that the user knows which imports triggered the on-the-fly bundling. And I think frameworks or end-users should try to keep on-the-fly bundling to the lowest too (ideally bundles once on startup), so having these kind of verbose logs could signal them to improve their setup, though I'm not sure why Ladle is behaving that way currently that causes the multi bundling.

@patak-dev
Copy link
Member Author

patak-dev commented Mar 23, 2022

I agree with moving the logging behind debug flag. My one question about adding logging with --force is then if logs show up will people think something is happening only when they use --force since they don't see the logs without it?

This is a good point. We should give some feedback when using force though. So maybe we should only log the first optimized deps batch with a message like:

12:18:24 AM [vite] ✨ forced drop of optimized dependencies cache
12:18:24 AM [vite] ✨ dependencies optimized: vue, pinia, luxon

But the rest of on-the-fly messages should probably also appear... because they also are there because you used --force. So my vote would go to add the new message and then show all of them. Or only show the new "forced drop" message and nothing more.

I feel like we should still keep the logs, so that the user knows which imports triggered the on-the-fly bundling. And I think frameworks or end-users should try to keep on-the-fly bundling to the lowest too (ideally bundles once on startup), so having these kind of verbose logs could signal them to improve their setup, though I'm not sure why Ladle is behaving that way currently that causes the multi bundling.

Ladle and similar projects have so many on-the-fly bundling because they don't scan the whole app upfront IIUC. Each time you click on a story, new dependencies are discovered. Maybe they should change this. But now that on-the-fly bundling isn't blocking, it may not be an anti-pattern anymore... the only issue is when these dependencies generate a page reload (and it is only the first time). We are showing a message in this case, and we could show something like:

12:18:24 AM [vite] ✨ previous optimized dependencies changed, reloading page
12:18:24 AM [vite] ✨ this reload could be avoided by adding to `optimizeDeps.include` some of the following dependencies: a, b, c, d

Without a message like this, even if we show the deps logs, the user will not know how to fix it. IMO, if they see previous optimized dependencies changed, reloading page they are going to google that and get to our docs where we could guide them in a new section.

Maybe just this:

12:18:24 AM [vite] ✨ newly discovered dependencies forced a page reload (consider using `optimizeDeps.include`)

My vote still goes to hide the logs in the general case, but I see the value on bumping the user to avoid unneeded page reloads.

@benmccann
Copy link
Collaborator

Why can't dependencies be discovered via dependency scanning when using Ladle? Do they manually disable Vite's dependency scanning? Adding dependencies manually to optimizeDeps.include is a bit burdensome and I'm not sure I'd recommend that instead of changing the way the app is written (assuming that's possible)

@patak-dev
Copy link
Member Author

Maybe @tajo could explain a bit more why these dependencies are discovered late. Maybe ladle could add the proper entries to optimizeDeps.entries if they want to have more things optimized upfront. Looks like a more general problem in this kind of apps though (ping @IanVS, @JessicaSachs).

I would still move forward with hiding as much of the deps logs as possible, and only show them when there is a reload (maybe with extra information about how to improve things, but looks quite hard to log something concise...).

@IanVS
Copy link
Contributor

IanVS commented Mar 23, 2022

Yes, the storybook vite builder does attempt to use optimizeDeps.entries based on the user's stories config, which has helped a lot after we added that feature. However, the way storybook works, there are a few packages which are not directly imported by the user's code, for example framework packages like @storybook/react or @storybook/vue. They are used by the storybook cli, but not imported in the user's code directly. More irritatingly, sub-dependencies of these packages also need to be included in optimizeDeps.include.. @bluwy was helping me troubleshoot this in discord, but I haven't yet gotten to the bottom of why it's happening.

@bluwy
Copy link
Member

bluwy commented Mar 23, 2022

I think even if we report using optimizeDeps.include, it could bring false positives for cases where the users imports new deps while the server is running. So it's a hard one to tackle.

Ultimately, if we could prevent multiple on-the-fly bundling by encouraging proper initial bundling, that would help the end-user for faster web page TTI and reduced repeated esbuild bundling, which is an overall plus. Hiding the logs would also hide this potential optimization IMO, unless we can provide correct recommendation in the logs)

@patak-dev
Copy link
Member Author

For the record, looks like vite-plugin-autoimport also ends up generating these late discovered deps: unplugin/unplugin-auto-import#124

@bluwy would you be ok if we remove the log for the first batch scanned deps and only log late discovered deps with a message like:

12:18:24 AM [vite] ✨ late discovered dependencies optimized: vue, pinia, luxon

And if a page reload

12:18:24 AM [vite] ✨ late discovered dependencies optimized: vue, pinia, luxon
12:18:24 AM [vite] ✨ previously optimized dependencies changed, a page reload is required

So at least we don't have logs if every deps is discovered by the scan phase?

I still think that it may be quite hard for the user to understand what is going on, but this would work for me

@bluwy
Copy link
Member

bluwy commented Mar 23, 2022

Yep that sounds fine by me 👍 I dont usually find the initial scanned deps to be important too.

@tajo
Copy link
Contributor

tajo commented Mar 23, 2022

I think it's fine to hide most of these logs as long as it can be enabled for debugging.

Maybe @tajo could explain a bit more why these dependencies are discovered late.

It's because stories are loaded as

const Story = React.lazy(() => import('./foo.stories.js'));

So the dependencies inside of foo.stories.js are not discovered initially, right?

I tried to add all stories into optimizeDeps.entries to force all deps being pre-bundled upfront but it wasn't quite working, it still did some late discoveries. (I could try to create a repro for that and test it again with 2.9)

Ideally, it would be nice to eliminate any reloads since it's not a great experience while keeping the startup instant no matter how many stories and dependencies there are. Maybe with 2.9, it's actually ok to put everything into include/entries. Otherwise, there could be an option like optimizeDeps.entriesPreload - Vite could defer pre-bundling of these deps until the initial phase is done. This would keep the startup fast while all other dependencies would get pre-bundled in background before user clicks on another story.

@IanVS
Copy link
Contributor

IanVS commented Mar 23, 2022

👍 The only nice part of seeing them was knowing whether it was a hot or cold start, and now that the distinction doesn't matter as much, I agree that it doesn't really help to see the list of optimized dependencies.

@benmccann
Copy link
Collaborator

sounds good to me. I might tweak the language a bit to something like:

12:18:24 AM [vite] ✨ newly discovered dependencies optimized: vue, pinia, luxon
12:18:24 AM [vite] ✨ optimized dependencies changed. reloading

@patak-dev
Copy link
Member Author

sounds good to me. I might tweak the language a bit to something like:

12:18:24 AM [vite] ✨ newly discovered dependencies optimized: vue, pinia, luxon
12:18:24 AM [vite] ✨ optimized dependencies changed. reloading

The thing about newly is that the server may restart because of a lock file change and a new import. That is a new dependency, but it will not be logged. So newly doesn't say to me "this is a dep that was not discovered in the scan phase". I think late may be better here. unforeseen sounds too much 👀. But I'm fine with newly, it is what we were using so ok to me.

@patak-dev
Copy link
Member Author

I tried to add all stories into optimizeDeps.entries to force all deps being pre-bundled upfront but it wasn't quite working, it still did some late discoveries. (I could try to create a repro for that and test it again with 2.9)

@tajo I think a repro is a good idea here, IMO we should be able to discover this. Lets create an issue and see if we can find the problem

@patak-dev patak-dev changed the title feat: hide optimized deps logs feat: hide optimized deps found during scan phase logs Mar 23, 2022
@patak-dev
Copy link
Member Author

Implemented suggestions, this is how it looks for the react-emotion playground now
image

benmccann
benmccann previously approved these changes Mar 23, 2022
Copy link
Collaborator

@benmccann benmccann left a comment

Choose a reason for hiding this comment

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

I didn't review the code super closely, but leaving an approval here in terms of the behavior and log messages

colors.green(
`✨ previous optimized dependencies have changed, reloading page`
),
colors.green(`✨ optimized dependencies changed. reloading`),
Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we could use a different emoji for this one. Like 🔄 or ⚙️. Not convinced by other options, probably better to keep the current one

bluwy
bluwy previously approved these changes Mar 24, 2022
packages/vite/src/node/optimizer/registerMissing.ts Outdated Show resolved Hide resolved
patak-dev and others added 2 commits March 24, 2022 07:50
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
@patak-dev patak-dev merged commit f4934e8 into main Mar 24, 2022
@patak-dev patak-dev deleted the feat/clean-up-optimized-dep-logs branch March 24, 2022 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants