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

fix(unplugin): HMR support #69

Merged
merged 1 commit into from
Feb 3, 2025

Conversation

cgatian
Copy link
Collaborator

@cgatian cgatian commented Feb 3, 2025

Sending HMR updates manually with server.moduleGraph and server.hot seems to be the issue. By switching these to server.reloadModule updates appear to be loaded and sent properly to the browser. This works for both outfile and virtual module flavors.

Relevant Vite PR: vitejs/vite#10333

I think we also need to debounce this a bit, maybe 100ms. When loading a large graph (like hundreds of files) I noticed HMR updates happening frequently (as expected). Need to play around with this in a separate PR, but this should fix HMR in general.

Fixes:

  1. Subsequent HMR updates are properly sent.
  2. Initial load is more accurate, reflecting the current state. However, there were some rare cases where it wouldn't load on the initial startup, maybe 10% of the time.

Current State

  1. Styles don't load at all for imported dependencies. (non-entry points)
  2. Subsequent updates may or may not make it through HMR without a full page reload.

hmr-broken

Proposed

  1. Notice that imported styles are now available on load.
  2. Additional updates are properly hot-updated.

hmr-fixed

Also, it looks like @anubra266 was also looking for a solution a while back.
patak-dev/vite-plugin-virtual#6

FYI looks like Vite does the invalidateModule behind the scenes when invoking this function
image

Fixes #67

Copy link

vercel bot commented Feb 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
pandabox-z5kx ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 3, 2025 7:44pm

@cgatian cgatian changed the title fix(unplugin): hrm support for virtual module fix(unplugin): HMR support for virtual module Feb 3, 2025
@cgatian cgatian force-pushed the fix/virtual-module-hmr branch from 2f28bd4 to dc667f3 Compare February 3, 2025 19:42
@cgatian cgatian changed the title fix(unplugin): HMR support for virtual module fix(unplugin): HMR support Feb 3, 2025
@@ -115,15 +115,7 @@ export const unpluginFactory: UnpluginFactory<PandaPluginOptions | undefined> =
if (outfile !== ids.css.resolved) {
getCtx().then((ctx) => fs.writeFile(outfile, ctx.toCss(ctx.panda.createSheet(), options)))
}

const timestamp = Date.now()
server.moduleGraph.invalidateModule(mod, new Set(), timestamp, true)
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a similar line on 247 responding to changes in Vite. It seems like the invalidation here should be enough though, since this code will also get called when files change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah that's for a different code path of module invalidation and didn't want to change too much in scope

Copy link
Owner

Choose a reason for hiding this comment

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

let's release it with this for now; we can always revisit that part later on if needed
I'll try on my end as well in a realworld project

btw we might want to add https://github.com/stackblitz-labs/pkg.pr.new if you guys want to contribute regularly

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh wow. Yeah that would be awesome, didn't know that was a thing.

@astahmer astahmer merged commit 1baa877 into astahmer:main Feb 3, 2025
2 checks passed
@cgatian
Copy link
Collaborator Author

cgatian commented Feb 4, 2025

@astahmer cornflowerblue (imported via recipe and modified virtual:panda.css) is displayed on stackblitz launch now 🎊
https://stackblitz.com/edit/vitejs-vite-ehgcanlx?file=package.json

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.

[unplugin]: styles missing on initial load
3 participants