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

Warning with :global(*) in a component with no html elements #153

Closed
Zachiah opened this issue Aug 30, 2021 · 4 comments · Fixed by #154 or #155
Closed

Warning with :global(*) in a component with no html elements #153

Zachiah opened this issue Aug 30, 2021 · 4 comments · Fixed by #154 or #155
Labels
bug Something isn't working documentation Improvements or additions to documentation hmr

Comments

@Zachiah
Copy link

Zachiah commented Aug 30, 2021

Describe the bug

If I have a component like this

<style>
  :global(*) {
    box-sizing: border-box;
  }
</style>

it gives this warning when running the dev server and going to localhost:3000

[vite-plugin-svelte] C:/Users/Justin/Documents/Zachiah/brandons-freelancer-projects/svelte-vite-bug/src/App.svelte:5:1 Unused CSS selector "*"

I think this is a bug here due to the fact that it doesn't show up in the svelte repl, or vs code, although I could be wrong, if so sorry about that.

Reproduction

You can clone this repo https://github.com/Zachiah/vite-plugin-svelte-warning-with-global-selector and just run the dev server and go to localhost:3000

Logs

vite v2.5.1 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 523ms.

11:19:44 AM [vite-plugin-svelte] C:/Users/Justin/Documents/Zachiah/brandons-freelancer-projects/svelte-vite-bug/src/App.svelte:5:1 Unused CSS selector "*"

System Info

System:
    OS: Windows 10 10.0.19043
    CPU: (4) x64 Intel(R) Core(TM) m5-6Y54 CPU @ 1.10GHz
    Memory: 2.04 GB / 7.88 GB
  Binaries:
    Node: 14.17.5 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 7.19.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1023.0), Chromium (92.0.902.84)
    Internet Explorer: 11.0.19041.906

Severity

annoyance

@Zachiah Zachiah added bug Something isn't working triage Awaiting triage by a project member labels Aug 30, 2021
@bluwy
Copy link
Member

bluwy commented Aug 30, 2021

You're right that it's a bug in vite-plugin-svelte, but for a different reason. vite-plugin-svelte actually implicitly adds * {} to the style tag so that CSS HMR doesn't affect JS HMR (IIRC). But this fails if there's no element to select in the markup.

I don't think it can be dynamically added depending on on the markup though, maybe we would have to suppress that warning by default then.

@dominikg
Copy link
Member

dominikg commented Aug 30, 2021

it's complicated. This extra * {} is used to ensure that the svelte compiler adds the components css-hash class to every element of the rendered template. Which in turn means we can get more "css-only" updates when editing classes in template and style sections of a component.

The case of a "component with styles but without a template" wasn't considered when this was implemented. And i'm curious why you would use that. If there is nothing to scope to, you're better off using external css files, right?

To ignore this warning by default, we could add a filter to warnDev in log.ts. The extra selector isn't added on build and i don't want to filter build warnings.
But we should also add a FAQ entry encouraging people to only add scoped styles to style nodes in .svelte components as vite offers far superior tooling for global styles.
Or if you phrase it differently, warn about not abusing global in svelte component styles.

@dominikg dominikg added documentation Improvements or additions to documentation hmr and removed triage Awaiting triage by a project member labels Aug 30, 2021
@bluwy
Copy link
Member

bluwy commented Aug 31, 2021

Besides "global styles without a template", I actually hit this issue a while ago for one of our Svelte components that contains only subcomponents (no HTML elements). So this issue could happen in the wild unintentionally too.

EDIT: Actually my scenario is kinda a code smell too as it would also happen when the style tag has :global, which in most cases should be a scoped global instead.

@dominikg
Copy link
Member

dominikg commented Aug 31, 2021

This warning is specifically only emitted when there is not a single element in the template where a scope can be applied.
As soon as there is a <slot> or any other non-child-component element it does not happen.

So i'd say the warning is a sign for code-smell in 99% of cases it happens. So instead of filtering it we could issue a seaprate code + message so that the user learns about the potential issue and for legitimate usecases they can decide to ignore it via custom onwarn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation hmr
Projects
None yet
3 participants