Skip to content

Commit

Permalink
css: try to merge adjacent @layer rules together
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 12, 2023
1 parent f759693 commit eb667c3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
34 changes: 34 additions & 0 deletions internal/bundler_tests/bundler_css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2537,3 +2537,37 @@ func TestCSSAtLayerBeforeImportBundle(t *testing.T) {
},
})
}

func TestCSSAtLayerMergingWithImportConditions(t *testing.T) {
css_suite.expectBundled(t, bundled{
files: map[string]string{
"/entry.css": `
@import "a.css" supports(color: first);
@import "a.css" supports(color: second);
@import "b.css" supports(color: second);
@import "a.css" supports(color: first);
@import "b.css" supports(color: first);
@import "a.css" supports(color: second);
@import "b.css" supports(color: second);
@import "b.css" supports(color: first);
`,
"/a.css": `
@layer a;
@import "http://example.com/a.css";
`,
"/b.css": `
@layer b;
@import "http://example.com/b.css";
`,
},
entryPaths: []string{"/entry.css"},
options: config.Options{
Mode: config.ModeBundle,
AbsOutputDir: "/out",
},
})
}
33 changes: 29 additions & 4 deletions internal/bundler_tests/snapshots/snapshots_css.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ TestCSSAtImportConditionsAtLayerBundleAlternatingLayerInFile
/* case2.css */

---------- /out/case3.css ----------
@layer first;
@layer last;
@layer first, last;

/* a.css */
@layer first {
Expand Down Expand Up @@ -289,8 +288,7 @@ TestCSSAtImportConditionsAtLayerBundleAlternatingLayerInFile
/* case4.css */

---------- /out/case5.css ----------
@layer first;
@layer last;
@layer first, last;

/* a.css */
@layer first {
Expand Down Expand Up @@ -1614,6 +1612,33 @@ TestCSSAtLayerBeforeImportNoBundle
@import "b.css";
@layer layer6.layer7, layer8;

================================================================================
TestCSSAtLayerMergingWithImportConditions
---------- /out/entry.css ----------
@supports (color: first) {
@layer a;
}
@import "http://example.com/a.css" supports(color: first);
@import "http://example.com/a.css" supports(color: second);
@import "http://example.com/b.css" supports(color: second);
@import "http://example.com/b.css" supports(color: first);
@supports (color: second) {
@layer a, b;
}

/* a.css */
@supports (color: first) {
@layer b;
}

/* a.css */

/* b.css */

/* b.css */

/* entry.css */

================================================================================
TestCSSEntryPoint
---------- /out.css ----------
Expand Down
21 changes: 21 additions & 0 deletions internal/linker/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3703,6 +3703,27 @@ func (c *linkerContext) findImportedFilesInCSSOrder(entryPoints []uint32) (order
wipOrder = append(wipOrder, entry)
}

order, wipOrder = wipOrder, order[:0]
}

// Finally, merge adjacent "@layer" rules with identical conditions together.
{
didClone := -1
for _, entry := range order {
if entry.kind == cssImportLayers && len(wipOrder) > 0 {
prevIndex := len(wipOrder) - 1
prev := wipOrder[prevIndex]
if prev.kind == cssImportLayers && importConditionsAreEqual(prev.conditions, entry.conditions) {
if didClone != prevIndex {
didClone = prevIndex
prev.layers = append([][]string{}, prev.layers...)
}
wipOrder[prevIndex].layers = append(prev.layers, entry.layers...)
continue
}
}
wipOrder = append(wipOrder, entry)
}
order = wipOrder
}

Expand Down

0 comments on commit eb667c3

Please sign in to comment.