Skip to content

Commit

Permalink
Fix purging in 1.8 (#2320)
Browse files Browse the repository at this point in the history
* re-add missing `@layer`'s for responsive rules

* use toMatchCss for better diffing

* do not wrap user `@responsive` rules in an `@layer`
  • Loading branch information
RobinMalfait authored Sep 4, 2020
1 parent b399a0b commit 4c4c5a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
8 changes: 4 additions & 4 deletions __tests__/purgeUnusedStyles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ test(
'utf8'
)

expect(result.css).toBe(expected)
expect(result.css).toMatchCss(expected)
})
})
)
Expand All @@ -457,7 +457,7 @@ test('does not purge if the array is empty', () => {
'utf8'
)

expect(result.css).toBe(expected)
expect(result.css).toMatchCss(expected)
})
})
)
Expand All @@ -482,7 +482,7 @@ test('does not purge if explicitly disabled', () => {
'utf8'
)

expect(result.css).toBe(expected)
expect(result.css).toMatchCss(expected)
})
})
)
Expand All @@ -507,7 +507,7 @@ test('does not purge if purge is simply false', () => {
'utf8'
)

expect(result.css).toBe(expected)
expect(result.css).toMatchCss(expected)
})
})
)
Expand Down
36 changes: 30 additions & 6 deletions src/lib/substituteResponsiveAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,31 @@ import cloneNodes from '../util/cloneNodes'
import buildMediaQuery from '../util/buildMediaQuery'
import buildSelectorVariant from '../util/buildSelectorVariant'

function isLayer(node) {
if (Array.isArray(node)) {
return node.length === 1 && isLayer(node[0])
}
return node.type === 'atrule' && node.name === 'layer'
}

function layerNodes(nodes) {
return isLayer(nodes) ? nodes[0].nodes : nodes
}

export default function(config) {
return function(css) {
// Store the `layer` for each responsive at-rule directly on the at-rule for later reference.
// Wrap any `responsive` rules with a copy of their parent `layer` to
// ensure the layer isn't lost when copying to the `screens` location.
css.walkAtRules('layer', layerAtRule => {
const layer = layerAtRule.params
layerAtRule.walkAtRules('responsive', responsiveAtRule => {
responsiveAtRule.__tailwind = {
...responsiveAtRule.__tailwind,
layer,
}
const nestedlayerAtRule = postcss.atRule({
name: 'layer',
params: layer,
})
nestedlayerAtRule.prepend(responsiveAtRule.nodes)
responsiveAtRule.removeAll()
responsiveAtRule.prepend(nestedlayerAtRule)
})
})

Expand All @@ -27,7 +42,16 @@ export default function(config) {
css.walkAtRules('responsive', atRule => {
const nodes = atRule.nodes
responsiveRules.append(...cloneNodes(nodes))
atRule.before(nodes)

// If the parent is already a `layer` (this is true for anything coming from
// a plugin, including core plugins) we don't want to create a double nested
// layer, so only insert the layer children. If there is no parent layer,
// preserve the layer information when inserting the nodes.
if (isLayer(atRule.parent)) {
atRule.before(layerNodes(nodes))
} else {
atRule.before(nodes)
}
atRule.remove()
})

Expand Down

0 comments on commit 4c4c5a3

Please sign in to comment.