Skip to content

Conversation

@fehnomenal
Copy link
Contributor

@fehnomenal fehnomenal commented Oct 6, 2025

While debugging an unrelated problem it came to my mind that the current way of getting global styles into the html in sveltekit is rather involved by needing to have a certain hook setup that replaces a string.
Instead we can just replace the string without needing to replace on each page render.

It works by replacing the placeholder %unocss-svelte-scoped.global% during build (in any file) with a reference to the emitted css file. For dev the dev server gets a fixed route that serves the global css file.

Actually this is a breaking change: the placeholder %unocss-svelte-scoped.global% is also replaced in existing hooks.server.(ts|js) files. This results in the built application to replace the <link ...> tag to be replaced with unocss_svelte_scoped_global_styles. Which is the old value which was replaced with the tag.

cc @jacob-8

@fehnomenal fehnomenal requested review from antfu and zyyv as code owners October 6, 2025 23:52
@netlify
Copy link

netlify bot commented Oct 6, 2025

Deploy Preview for unocss ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit ab70667
🔍 Latest deploy log https://app.netlify.com/projects/unocss/deploys/690cd607fe8de00008fd8f6e
😎 Deploy Preview https://deploy-preview-4942--unocss.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@fehnomenal fehnomenal changed the title refactor!(svelte-scoped): simplify user facing setup refactor(svelte-scoped)!: simplify user facing setup Oct 6, 2025
@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 6, 2025

Open in StackBlitz

commit: ab70667

@henrikvilhelmberglund
Copy link
Contributor

It seems like --at-apply is broken (not related to this PR), was this what you were debugging?

Also since we're using Svelte 5 now I would like the example updated to use the new style rules like no self closing non void elements.

I'm all for less user facing setup but I didn't write Svelte Scoped so I would like to see @jacob-8 's input as well.

@fehnomenal
Copy link
Contributor Author

I can't remember the issue I debugged but the fix for --at-apply lives at #4795.

@henrikvilhelmberglund
Copy link
Contributor

I can't remember the issue I debugged but the fix for --at-apply lives at #4795.

Perfect, anyway waiting for Jacob's opinion, if he doesn't appear it will be merged in a few days.

Copy link
Contributor

@henrikvilhelmberglund henrikvilhelmberglund left a comment

Choose a reason for hiding this comment

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

There should probably be an error/warning if there is an hooks.server.js|ts file with the previous Svelte-scoped config since you will end up with unocss_svelte_scoped_global_styles text in the top of the page.

@henrikvilhelmberglund
Copy link
Contributor

Does the requested change seem possible?

You can see the issue when using this PR in the existing Svelte-scoped example with the old setup.

If it doesn't seem possible you can add a note in the documentation stating that the previous hooks.server.ts setup is not required anymore.

@fehnomenal
Copy link
Contributor Author

Sorry for the delay. I just added a warning if the old placeholder appears in a server hook file. @henrikvilhelmberglund

Copy link
Contributor

@henrikvilhelmberglund henrikvilhelmberglund left a comment

Choose a reason for hiding this comment

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

No worries! A typo, otherwise LGTM

@fehnomenal
Copy link
Contributor Author

Updated the file. Thanks for reading this stuff in such detail 😅

@henrikvilhelmberglund henrikvilhelmberglund added this pull request to the merge queue Nov 6, 2025
Merged via the queue into unocss:main with commit 7e52a16 Nov 6, 2025
11 checks passed
@fehnomenal fehnomenal deleted the push-kqsmumoykkom branch November 6, 2025 17:57
This was referenced Nov 7, 2025
@jacob-8
Copy link
Contributor

jacob-8 commented Dec 27, 2025

@henrikvilhelmberglund @fehnomenal I'm well aware that there have been a few simple svelte-scoped questions here and there this last year in regards to Svelte 5 and all. I'm still using this package and want to shave off a few of the rough edges but haven't had a chance to do anything. Just moved overseas and started a new business. 😀 So your contributions and ownership have been much appreciated. I'm still not chipping in yet, but things are trending towards calming down.

@henrikvilhelmberglund
Copy link
Contributor

@henrikvilhelmberglund @fehnomenal I'm well aware that there have been a few simple svelte-scoped questions here and there this last year in regards to Svelte 5 and all. I'm still using this package and want to shave off a few of the rough edges but haven't had a chance to do anything. Just moved overseas and started a new business. 😀 So your contributions and ownership have been much appreciated. I'm still not chipping in yet, but things are trending towards calming down.

Nice and no worries!

@fehnomenal
Copy link
Contributor Author

@henrikvilhelmberglund @fehnomenal I'm well aware that there have been a few simple svelte-scoped questions here and there this last year in regards to Svelte 5 and all. I'm still using this package and want to shave off a few of the rough edges but haven't had a chance to do anything. Just moved overseas and started a new business. 😀 So your contributions and ownership have been much appreciated. I'm still not chipping in yet, but things are trending towards calming down.

All good, take your time!

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.

3 participants