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: add hasUnscopedGlobalCss to compile metadata #15450

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

paoloricciuti
Copy link
Member

Closes #15449

From my tests it should be basically it (it's missing the --global keyframes but i'm gonna add them now) but please try to break it.

No test because we don't have a test suite for the compiled output...should we add it?

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

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Mar 5, 2025

🦋 Changeset detected

Latest commit: ee4869f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Minor

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

@svelte-docs-bot
Copy link

Copy link
Contributor

github-actions bot commented Mar 5, 2025

Playground

pnpm add https://pkg.pr.new/svelte@15450

@Rich-Harris
Copy link
Member

This feels very specific to vite-plugin-svelte, and given that zimmerframe is already in the dependency graph for anyone using it, the cost of doing the work inside vite-plugin-svelte feels very small, especially with the .includes(':global') check.

Given that, do we really want to add it here? It adds more code to svelte than vite-plugin-svelte would be able to remove, and the 'other tools' argument feels a bit hypothetical.

If the answer is 'yes' then I'd at least suggest changing the API so that it's result.css?.hasGlobal rather than adding metadata.

@paoloricciuti
Copy link
Member Author

If the answer is 'yes' then I'd at least suggest changing the API so that it's result.css?.hasGlobal rather than adding metadata

Initially I added it there but it felt a bit wrong since we already have the metadata object. But if we want to move it it's fine too.

For the question of "should we do it" this could save one walk since we are already walking to analyze css but effectively does feels a bit specific.

At the end of the day the PR it's here, up to discuss if we want to do it, otherwise we can close it 😌

@dominikg
Copy link
Member

dominikg commented Mar 10, 2025

if this gets added in svelte itself i think the right place would be inside css. My motivation for putting it here was perf/logistics for the most part. svelte is writing css selectors without a scope class at some point so it must know already when output contains unscoped css and if its just a simple flip of a boolean it should be both less code and more performant. If it's the same song and dance as in vite-plugin-svelte with walking the ast again fine with me to keep it there.

Ultimately i hope we start to warn against using unscoped global styles in .svelte files. In my mind styles in <style> blocks of .svelte files should always come with some form of scoping to that component. independent styles should be defined in a separate file and imported as needed.

And if thats the case this PR could help to implement such a warning too.

@Rich-Harris
Copy link
Member

Theoretically it is more efficient to do it here since it doesn't involve an additional walk, but we're talking about fractions of milliseconds for all but the largest <style> blocks (and in a tiny minority of components). So for me performance/efficiency doesn't really enter into the equation here — it's purely a question of whether the API belongs in the core. And that rests on whether things other than vite-plugin-svelte would realistically make use of it, which seems very doubtful to me (but if that was proven wrong we would always have the option of adding it in future). So my vote is to close this.

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.

add "hasUnscopedGlobalCss" metadata to compiler ouput
3 participants