Skip to content

Commit

Permalink
fix #1945: initial lowering code for css nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 26, 2023
1 parent 96e09b4 commit 72c8379
Show file tree
Hide file tree
Showing 11 changed files with 780 additions and 98 deletions.
60 changes: 60 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,66 @@

## Unreleased

* Implement preliminary lowering for CSS nesting ([#1945](https://github.com/evanw/esbuild/issues/1945))

Chrome has [implemented the new CSS nesting specification](https://developer.chrome.com/articles/css-nesting/) in version 112, which is currently in beta but will become stable very soon. So CSS nesting is now a part of the web platform!

This release of esbuild can now transform nested CSS syntax into non-nested CSS syntax for older browsers. The transformation relies on the `:is()` pseudo-class in many cases, so the transformation is only guaranteed to work when targeting browsers that support `:is()` (e.g. Chrome 88+). You'll need to set esbuild's [`target`](https://esbuild.github.io/api/#target) to the browsers you intend to support to tell esbuild to do this transformation. You will get a warning if you use CSS nesting syntax with a `target` which includes older browsers that don't support `:is()`.

The lowering transformation looks like this:

```css
/* Original input */
a.btn {
color: #333;
&:hover { color: #444 }
&:active { color: #555 }
}

/* New output (with --target=chrome88) */
a.btn {
color: #333;
}
a.btn:hover {
color: #444;
}
a.btn:active {
color: #555;
}
```

More complex cases may generate the `:is()` pseudo-class:

```css
/* Original input */
div, p {
.warning, .error {
padding: 20px;
}
}

/* New output (with --target=chrome88) */
:is(div, p) :is(.warning, .error) {
padding: 20px;
}
```

In addition, esbuild now has a special warning message for nested style rules that start with an identifier. This isn't allowed in CSS because the syntax would be ambiguous with the existing declaration syntax. The new warning message looks like this:

```
▲ [WARNING] A nested style rule cannot start with "p" because it looks like the start of a declaration [css-syntax-error]

<stdin>:1:7:
1 │ main { p { margin: auto } }
│ ^
╵ :is(p)

To start a nested style rule with an identifier, you need to wrap the identifier in ":is(...)" to
prevent the rule from being parsed as a declaration.
```

Keep in mind that the transformation in this release is a preliminary implementation. CSS has many features that interact in complex ways, and there may be some edge cases that don't work correctly yet.

* Minification now removes unnecessary `&` CSS nesting selectors

This release introduces the following CSS minification optimizations:
Expand Down
2 changes: 1 addition & 1 deletion internal/bundler_tests/bundler_css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ func TestCSSNestingOldBrowser(t *testing.T) {
options: config.Options{
Mode: config.ModeBundle,
AbsOutputDir: "/out",
UnsupportedCSSFeatures: compat.Nesting,
UnsupportedCSSFeatures: compat.Nesting | compat.IsPseudoClass,
OriginalTargetEnv: "chrome10",
},
expectedScanLog: `media-ampersand-first.css: WARNING: CSS nesting syntax is not supported in the configured target environment (chrome10)
Expand Down
64 changes: 23 additions & 41 deletions internal/bundler_tests/snapshots/snapshots_css.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,92 +182,74 @@ console.log(void 0);
TestCSSNestingOldBrowser
---------- /out/[email protected] ----------
/* [email protected] */
a {
@layer base {
@layer base {
a {
color: red;
}
}

---------- /out/[email protected] ----------
/* [email protected] */
a {
@media screen {
@media screen {
a {
color: red;
}
}

---------- /out/nested-ampersand-twice.css ----------
/* nested-ampersand-twice.css */
a,
a {
&,
& {
color: red;
}
color: red;
}

---------- /out/nested-ampersand-first.css ----------
/* nested-ampersand-first.css */
a {
&,
b {
color: red;
}
a,
a b {
color: red;
}

---------- /out/nested-attribute.css ----------
/* nested-attribute.css */
a {
[href] {
color: red;
}
a [href] {
color: red;
}

---------- /out/nested-colon.css ----------
/* nested-colon.css */
a {
:hover {
color: red;
}
a :hover {
color: red;
}

---------- /out/nested-dot.css ----------
/* nested-dot.css */
a {
.cls {
color: red;
}
a .cls {
color: red;
}

---------- /out/nested-greaterthan.css ----------
/* nested-greaterthan.css */
a {
> b {
color: red;
}
a > b {
color: red;
}

---------- /out/nested-hash.css ----------
/* nested-hash.css */
a {
#id {
color: red;
}
a #id {
color: red;
}

---------- /out/nested-plus.css ----------
/* nested-plus.css */
a {
+ b {
color: red;
}
a + b {
color: red;
}

---------- /out/nested-tilde.css ----------
/* nested-tilde.css */
a {
~ b {
color: red;
}
a ~ b {
color: red;
}

---------- /out/toplevel-ampersand-twice.css ----------
Expand Down
24 changes: 18 additions & 6 deletions internal/compat/css_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ const (

InsetProperty
Nesting
IsPseudoClass
)

var StringToCSSFeature = map[string]CSSFeature{
"hex-rgba": HexRGBA,
"inline-style": InlineStyle,
"rebecca-purple": RebeccaPurple,
"modern-rgb-hsl": Modern_RGB_HSL,
"inset-property": InsetProperty,
"nesting": Nesting,
"hex-rgba": HexRGBA,
"inline-style": InlineStyle,
"rebecca-purple": RebeccaPurple,
"modern-rgb-hsl": Modern_RGB_HSL,
"inset-property": InsetProperty,
"nesting": Nesting,
"is-pseudo-class": IsPseudoClass,
}

func (features CSSFeature) Has(feature CSSFeature) bool {
Expand Down Expand Up @@ -77,6 +79,16 @@ var cssTable = map[CSSFeature]map[Engine][]versionRange{
Nesting: {
Chrome: {{start: v{112, 0, 0}}},
},

// Data from: https://caniuse.com/css-matches-pseudo
IsPseudoClass: {
Chrome: {{start: v{88, 0, 0}}},
Edge: {{start: v{88, 0, 0}}},
Firefox: {{start: v{78, 0, 0}}},
IOS: {{start: v{14, 0, 0}}},
Opera: {{start: v{75, 0, 0}}},
Safari: {{start: v{14, 0, 0}}},
},
}

// Return all features that are not available in at least one environment
Expand Down
Loading

0 comments on commit 72c8379

Please sign in to comment.