Skip to content

Commit eb667c3

Browse files
committed
css: try to merge adjacent @layer rules together
1 parent f759693 commit eb667c3

3 files changed

Lines changed: 84 additions & 4 deletions

File tree

internal/bundler_tests/bundler_css_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,3 +2537,37 @@ func TestCSSAtLayerBeforeImportBundle(t *testing.T) {
25372537
},
25382538
})
25392539
}
2540+
2541+
func TestCSSAtLayerMergingWithImportConditions(t *testing.T) {
2542+
css_suite.expectBundled(t, bundled{
2543+
files: map[string]string{
2544+
"/entry.css": `
2545+
@import "a.css" supports(color: first);
2546+
2547+
@import "a.css" supports(color: second);
2548+
@import "b.css" supports(color: second);
2549+
2550+
@import "a.css" supports(color: first);
2551+
@import "b.css" supports(color: first);
2552+
2553+
@import "a.css" supports(color: second);
2554+
@import "b.css" supports(color: second);
2555+
2556+
@import "b.css" supports(color: first);
2557+
`,
2558+
"/a.css": `
2559+
@layer a;
2560+
@import "http://example.com/a.css";
2561+
`,
2562+
"/b.css": `
2563+
@layer b;
2564+
@import "http://example.com/b.css";
2565+
`,
2566+
},
2567+
entryPaths: []string{"/entry.css"},
2568+
options: config.Options{
2569+
Mode: config.ModeBundle,
2570+
AbsOutputDir: "/out",
2571+
},
2572+
})
2573+
}

internal/bundler_tests/snapshots/snapshots_css.txt

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ TestCSSAtImportConditionsAtLayerBundleAlternatingLayerInFile
250250
/* case2.css */
251251

252252
---------- /out/case3.css ----------
253-
@layer first;
254-
@layer last;
253+
@layer first, last;
255254

256255
/* a.css */
257256
@layer first {
@@ -289,8 +288,7 @@ TestCSSAtImportConditionsAtLayerBundleAlternatingLayerInFile
289288
/* case4.css */
290289

291290
---------- /out/case5.css ----------
292-
@layer first;
293-
@layer last;
291+
@layer first, last;
294292

295293
/* a.css */
296294
@layer first {
@@ -1614,6 +1612,33 @@ TestCSSAtLayerBeforeImportNoBundle
16141612
@import "b.css";
16151613
@layer layer6.layer7, layer8;
16161614

1615+
================================================================================
1616+
TestCSSAtLayerMergingWithImportConditions
1617+
---------- /out/entry.css ----------
1618+
@supports (color: first) {
1619+
@layer a;
1620+
}
1621+
@import "http://example.com/a.css" supports(color: first);
1622+
@import "http://example.com/a.css" supports(color: second);
1623+
@import "http://example.com/b.css" supports(color: second);
1624+
@import "http://example.com/b.css" supports(color: first);
1625+
@supports (color: second) {
1626+
@layer a, b;
1627+
}
1628+
1629+
/* a.css */
1630+
@supports (color: first) {
1631+
@layer b;
1632+
}
1633+
1634+
/* a.css */
1635+
1636+
/* b.css */
1637+
1638+
/* b.css */
1639+
1640+
/* entry.css */
1641+
16171642
================================================================================
16181643
TestCSSEntryPoint
16191644
---------- /out.css ----------

internal/linker/linker.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,6 +3703,27 @@ func (c *linkerContext) findImportedFilesInCSSOrder(entryPoints []uint32) (order
37033703
wipOrder = append(wipOrder, entry)
37043704
}
37053705

3706+
order, wipOrder = wipOrder, order[:0]
3707+
}
3708+
3709+
// Finally, merge adjacent "@layer" rules with identical conditions together.
3710+
{
3711+
didClone := -1
3712+
for _, entry := range order {
3713+
if entry.kind == cssImportLayers && len(wipOrder) > 0 {
3714+
prevIndex := len(wipOrder) - 1
3715+
prev := wipOrder[prevIndex]
3716+
if prev.kind == cssImportLayers && importConditionsAreEqual(prev.conditions, entry.conditions) {
3717+
if didClone != prevIndex {
3718+
didClone = prevIndex
3719+
prev.layers = append([][]string{}, prev.layers...)
3720+
}
3721+
wipOrder[prevIndex].layers = append(prev.layers, entry.layers...)
3722+
continue
3723+
}
3724+
}
3725+
wipOrder = append(wipOrder, entry)
3726+
}
37063727
order = wipOrder
37073728
}
37083729

0 commit comments

Comments
 (0)