Skip to content

Commit 4c4c5a3

Browse files
authored
Fix purging in 1.8 (#2320)
* re-add missing `@layer`'s for responsive rules * use toMatchCss for better diffing * do not wrap user `@responsive` rules in an `@layer`
1 parent b399a0b commit 4c4c5a3

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

__tests__/purgeUnusedStyles.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ test(
430430
'utf8'
431431
)
432432

433-
expect(result.css).toBe(expected)
433+
expect(result.css).toMatchCss(expected)
434434
})
435435
})
436436
)
@@ -457,7 +457,7 @@ test('does not purge if the array is empty', () => {
457457
'utf8'
458458
)
459459

460-
expect(result.css).toBe(expected)
460+
expect(result.css).toMatchCss(expected)
461461
})
462462
})
463463
)
@@ -482,7 +482,7 @@ test('does not purge if explicitly disabled', () => {
482482
'utf8'
483483
)
484484

485-
expect(result.css).toBe(expected)
485+
expect(result.css).toMatchCss(expected)
486486
})
487487
})
488488
)
@@ -507,7 +507,7 @@ test('does not purge if purge is simply false', () => {
507507
'utf8'
508508
)
509509

510-
expect(result.css).toBe(expected)
510+
expect(result.css).toMatchCss(expected)
511511
})
512512
})
513513
)

src/lib/substituteResponsiveAtRules.js

+30-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,31 @@ import cloneNodes from '../util/cloneNodes'
44
import buildMediaQuery from '../util/buildMediaQuery'
55
import buildSelectorVariant from '../util/buildSelectorVariant'
66

7+
function isLayer(node) {
8+
if (Array.isArray(node)) {
9+
return node.length === 1 && isLayer(node[0])
10+
}
11+
return node.type === 'atrule' && node.name === 'layer'
12+
}
13+
14+
function layerNodes(nodes) {
15+
return isLayer(nodes) ? nodes[0].nodes : nodes
16+
}
17+
718
export default function(config) {
819
return function(css) {
9-
// Store the `layer` for each responsive at-rule directly on the at-rule for later reference.
20+
// Wrap any `responsive` rules with a copy of their parent `layer` to
21+
// ensure the layer isn't lost when copying to the `screens` location.
1022
css.walkAtRules('layer', layerAtRule => {
1123
const layer = layerAtRule.params
1224
layerAtRule.walkAtRules('responsive', responsiveAtRule => {
13-
responsiveAtRule.__tailwind = {
14-
...responsiveAtRule.__tailwind,
15-
layer,
16-
}
25+
const nestedlayerAtRule = postcss.atRule({
26+
name: 'layer',
27+
params: layer,
28+
})
29+
nestedlayerAtRule.prepend(responsiveAtRule.nodes)
30+
responsiveAtRule.removeAll()
31+
responsiveAtRule.prepend(nestedlayerAtRule)
1732
})
1833
})
1934

@@ -27,7 +42,16 @@ export default function(config) {
2742
css.walkAtRules('responsive', atRule => {
2843
const nodes = atRule.nodes
2944
responsiveRules.append(...cloneNodes(nodes))
30-
atRule.before(nodes)
45+
46+
// If the parent is already a `layer` (this is true for anything coming from
47+
// a plugin, including core plugins) we don't want to create a double nested
48+
// layer, so only insert the layer children. If there is no parent layer,
49+
// preserve the layer information when inserting the nodes.
50+
if (isLayer(atRule.parent)) {
51+
atRule.before(layerNodes(nodes))
52+
} else {
53+
atRule.before(nodes)
54+
}
3155
atRule.remove()
3256
})
3357

0 commit comments

Comments
 (0)