-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
CSS transforms can break nested selectors #3620
Comments
I found that Edit: updating all our dependencies also made esbuild stop transforming nested selectors without needing a workaround. |
I don't think this case is supposed to do anything because the nesting selector cannot represent pseudo-elements. I believe this is because the behavior of the nesting selector is defined to be the same as the behavior of <!DOCTYPE html>
<style>
section {
width: 300px;
height: 300px;
overflow-y: scroll;
display: inline-block;
}
div {
height: 900px;
}
section::-webkit-scrollbar {
background-color: yellow;
}
/* Styling the scrollbar with non-nested CSS works */
#a::-webkit-scrollbar-thumb {
background: green;
}
#a::-webkit-scrollbar-thumb:window-inactive {
background: red;
}
#a::-webkit-scrollbar-thumb:hover {
background: blue;
}
/* Attempting to style the scrollbar with nested CSS doesn't work */
#b::-webkit-scrollbar-thumb {
& {
background: green;
}
&:window-inactive {
background: red;
}
&:hover {
background: blue;
}
}
</style>
<section id="a"><div></div></section>
<section id="b"><div></div></section> If you try that in a browser, the example on the left that doesn't use CSS nesting works but the example on the right that uses CSS nesting doesn't work. So it doesn't necessarily seem like a bug if esbuild is transforming nested CSS into non-nested CSS that has the same effect (which is no effect, since the input CSS is meaningless). |
@evanw what about the first post though?
This is a bug and I have run into it because unfortunately we have to support Chrome 117 in a project I'm working on. This means CSS nesting is not supported and esbuild will transform it causing some rules not to apply anymore, due to this bug. Are you planning on fixing this? Asking because its been open for a while. If I find the time I will look into this a bit more and try to find the code causing this. Would you accept a PR for this? In the meantime my workaround is manually expanding those CSS rules. I use this RegExp to find occurrences in the esbuild output: |
I think this: .parent {
> .a,
> .b1 > .b2 {
/* ... */
}
} should be transformed into this instead: .parent > .a,
.parent > .b1 > .b2 {
/* ... */
} as I don't think there is a way to make this more compact with the esbuild/internal/css_parser/css_nesting.go Lines 162 to 191 in 9c13ae1
So I think part of the fix would be to detect the |
Same problem here. |
https://esbuild.github.io/try/#YgAwLjIwLjAALS1zdXBwb3J0ZWQ6bmVzdGluZz1mYWxzZQBlAGVudHJ5LmNzcwBkaXYgewogID4gaW5wdXQsCiAgPiBsYWJlbCA+IGlucHV0IHsKICAgIG1hcmdpbjogMWVtOwogIH0KfQ
Esbuild transforms this:
into this:
Looks good right? No!
The following
actually means "select an
input
that is an immediate child oflabel
AND is also an immediate child ofdiv
", which is just not possible.I use esbuild through Vite, but I couldn't figure out a way to avoid this issue without disablingbuild.cssMinify
.Is there an esbuild option to never transform nested selectors?
In Vite I've triedwithout success, the broken selectors are still using:is()
.The text was updated successfully, but these errors were encountered: