-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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: universal injected css #12374
feat: universal injected css #12374
Conversation
There are various use cases where this continues to be necessary/nice to have: - rendering OG cards - rendering emails - basically anything where you use `render` manually and want to quickly stitch together the CSS without setting up an elaborate tooling chain
🦋 Changeset detectedLatest commit: 5f971fa The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
the failing tests relate to custom elements (naturally 🙄). investigating |
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.
looks good!
While testing this I noticed two things with HMR, which are separate to the PR (happen on main aswell):
- when running
pnpm ssr
withinjected
, and then changing something on the client, you get a runtime error inside HMR - when updating a color with
external
, the instance reruns. AFAIK CSS-only changes are handled better by v-p-s (cc @dominikg )
Co-authored-by: Simon H <[email protected]>
Yeah, I noticed the I noticed that v-p-s isn't doing its normal stable hashing thing, which could be related to the second point you mentioned |
stable hashing in v-p-s is done by passing in a custom cssHash function to compileOptions. css-only hmr relies on the fact that the css is an external module and update is handled by vite (v-p-s appends an import to the virtual css module of the externalized css) with "injected", i don't think we can avoid the js update altogether, best we can do is maybe somehow figure out that only css changed and update the style node without rerunning the instances. Maybe it's possible to do this with custom hmr accept code. Or partial accept can help? cc @rixo |
The thing is, it also doesn't preserve component state with |
Thank you guys for this! 😘😘😘 |
This implements #12294 (comment). If the
css
compiler option is"injected"
, styles will be added to thehead
when yourender(...)
.This makes it trivial to include styles when rendering things like emails and OG cards, as well as massively simplifying toy setups where you can't be bothered to figure out how to get CSS from the compiler into your server-rendered HTML.
append_styles
works largely as it did before, meaning that as components with injected CSS are rendered to the DOM, a<style>
element will be created if it doesn't already exist.This works with HMR by removing the
<style>
element for the component being reloaded. In an ideal world we'd check to see if the contents had changed, but that would involve more complexity and this solution is Good Enough™.Not pictured (yet): per-component options with
<svelte:options css="injected" />
.Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.Tests and linting
pnpm test
and lint the project withpnpm lint