-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Option to emit CSS Module <style> tags after js-imported CSS #19797
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
Comments
It is working for me in both dev and build. Changing the order of the imports is correctly changing the behavior ( |
Hello @functasti. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with |
@sapphi-red You're right—my bad. I should've provided a reproduction earlier. I encountered the issue in a React app. Here's my import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.jsx";
import './main.css';
createRoot(document.getElementById("app")).render(
<StrictMode>
<App />
</StrictMode>
); The This works as expected: import './main.css';
import App from "./App.jsx"; It would be great if there were some kind of warning for this, since it’s such an easy thing to overlook. Or maybe even an option to ensure CSS modules are always loaded last, regardless of import order? But I don’t think this is necessarily a "problem" in Vite. |
I'm not sure if Vite can detect if the import order is intended. Do you have any ideas how Vite can detect that?
I think that should be checked by a linter. Adding an option that bends the semantics to be different doesn't sound right to me. |
Description
In CSS, the first occurrence of a
@layer
name establishes its position in the cascade. If an explicit@layer
ordering is declared after a declaration, that earlier declaration takes precedence—even if the later ordering includes the layer name.Example:
The above renders the text in green because
components
was already defined before the ordering. If we move the@layer components
block after the@layer
ordering declaration, it renders in red as expected.This behavior also applies when layers are split across multiple
<style>
tags.In Vite (v6.2.0) development mode, CSS Modules are injected first, followed by JS-imported CSS. This means that any
@layer
declarations in CSS Modules will lock in their position before the@layer
ordering declared in regular CSS, breaking the expected cascade behavior.Suggested solution
Please add an option to render
<style>
tags from JS-imported CSS before those from CSS Modules, to allow proper@layer
ordering to take effect.Alternative
No response
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: