Skip to content

Commit

Permalink
fix #2838: bugs due to typos in the css minifier
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 18, 2023
1 parent a6a2f2f commit f8c5139
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## Unreleased

* Fix incorrect CSS minification for certain rules ([#2838](https://github.com/evanw/esbuild/issues/2838))

Certain rules such as `@media` could previously be minified incorrectly. Due to a typo in the duplicate rule checker, two known `@`-rules that share the same hash code were incorrectly considered to be equal. This problem was made worse by the rule hashing code considering two unknown declarations (such as CSS variables) to have the same hash code, which also isn't optimal from a performance perspective. Both of these issues have been fixed:

```css
/* Original input */
@media (prefers-color-scheme: dark) { body { --VAR-1: #000; } }
@media (prefers-color-scheme: dark) { body { --VAR-2: #000; } }

/* Old output (with --minify) */
@media (prefers-color-scheme: dark){body{--VAR-2: #000}}

/* New output (with --minify) */
@media (prefers-color-scheme: dark){body{--VAR-1: #000}}@media (prefers-color-scheme: dark){body{--VAR-2: #000}}
```

## 0.17.2

* Add `onDispose` to the plugin API ([#2140](https://github.com/evanw/esbuild/issues/2140), [#2205](https://github.com/evanw/esbuild/issues/2205))
Expand Down
4 changes: 2 additions & 2 deletions internal/css_ast/css_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ type RKnownAt struct {

func (a *RKnownAt) Equal(rule R) bool {
b, ok := rule.(*RKnownAt)
return ok && a.AtToken == b.AtToken && TokensEqual(a.Prelude, b.Prelude) && RulesEqual(a.Rules, a.Rules)
return ok && a.AtToken == b.AtToken && TokensEqual(a.Prelude, b.Prelude) && RulesEqual(a.Rules, b.Rules)
}

func (r *RKnownAt) Hash() (uint32, bool) {
Expand All @@ -392,7 +392,7 @@ type RUnknownAt struct {

func (a *RUnknownAt) Equal(rule R) bool {
b, ok := rule.(*RUnknownAt)
return ok && a.AtToken == b.AtToken && TokensEqual(a.Prelude, b.Prelude) && TokensEqual(a.Block, a.Block)
return ok && a.AtToken == b.AtToken && TokensEqual(a.Prelude, b.Prelude) && TokensEqual(a.Block, b.Block)
}

func (r *RUnknownAt) Hash() (uint32, bool) {
Expand Down

0 comments on commit f8c5139

Please sign in to comment.