Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix purging in 1.8 #2320

Merged
merged 3 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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