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

bug: Wrongly generated CSS selector upon build, while dev works fine #15912

Closed
7 tasks done
jbeckerdm opened this issue Feb 14, 2024 · 4 comments
Closed
7 tasks done

bug: Wrongly generated CSS selector upon build, while dev works fine #15912

jbeckerdm opened this issue Feb 14, 2024 · 4 comments
Labels
bug: upstream Bug in a dependency of Vite has workaround p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@jbeckerdm
Copy link

Describe the bug

Hello!

In a React TS + Vite project using CSS modules, I stumbled upon an issue
where all written css selectors are correctly applied to the elements in dev mode,
but in the production build, one selector is missing, and - upon further inspection - the generated CSS
differs slightly.

It happens roughly like this:
The following [here simplified] CSS is what I've written in the CSS module, and is used as-is in dev mode and works fine:

.inputContainer {
	.input { ... }
	
	&:after { ... }

	&:after,
	& .input { ... }
}

However, in production, the last css selector ( &:after, & .input ) is wrongly compiled into the following:

.inputContainer .input, .inputContainer .input {}

The .inputContainer:after selector is removed, and instead it compiles to two times the same selector.

Reproduction

https://stackblitz.com/edit/vitejs-vite-k6n2wy?file=src%2FApp.module.css

Steps to reproduce

I've attached a reproduction url using the stock React+Vite template (skipping TS here, it doesnt matter).
If you run that one in stackblitz as is, both .input and the :after Element will be laid on top of each other, which is correct.

However, if you stop that server in the terminal, run npm run build and then npx serve dist, you will see that they are not on top of another anymore, since the :after Element is missing a css rule, due to the css being wrongly compiled.

System Info

System:
    OS: macOS 14.3.1
    CPU: (11) arm64 Apple M3 Pro
    Memory: 159.33 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.6.0 - ~/.nvm/versions/node/v21.6.0/bin/node
    npm: 10.2.4 - ~/.nvm/versions/node/v21.6.0/bin/npm
    pnpm: 8.14.1 - ~/.nvm/versions/node/v21.6.0/bin/pnpm
  Browsers:
    Chrome: 121.0.6167.184
    Edge: 121.0.2277.112
    Safari: 17.3.1
  npmPackages:
    @vitejs/plugin-react: ^4.2.1 => 4.2.1 
    vite: ^5.0.8 => 5.0.12

Used Package Manager

npm

Logs

No response

Validations

Copy link

stackblitz bot commented Feb 14, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sapphi-red
Copy link
Member

It seems to be a bug in esbuild (evanw/esbuild#3648).
The easiest way to workaround is to swap the selector:

.inputContainer {
	& .input,
	&:after, { ... }
}

@jbeckerdm
Copy link
Author

Thank you for your quick help! I can confirm that the workaround works, and it seems that the bug in esbuild has just been fixed as well 🎉

If there's no further discussion necessary, this issue can be closed.

@sapphi-red
Copy link
Member

Note that this won't be included in Vite 5.1.x because Vite 5.1.x uses esbuild 0.19.x and the fix would be included in esbuild 0.20.x.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite has workaround p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

No branches or pull requests

3 participants