Skip to content

Commit 5b19a41

Browse files
committed
Convert layers to control comments before processing @apply rules
1 parent b69e46c commit 5b19a41

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import postcss from 'postcss'
2+
3+
export default function convertLayerAtRulesToControlComments() {
4+
return function(css) {
5+
css.walkAtRules('layer', atRule => {
6+
const layer = atRule.params
7+
atRule.before(postcss.comment({ text: `tailwind start ${layer}` }))
8+
atRule.before(atRule.nodes)
9+
atRule.before(postcss.comment({ text: `tailwind end ${layer}` }))
10+
atRule.remove()
11+
})
12+
}
13+
}

src/lib/purgeUnusedStyles.js

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,6 @@ import chalk from 'chalk'
55
import { log } from '../cli/utils'
66
import * as emoji from '../cli/emoji'
77

8-
function convertLayersToControlComments(css) {
9-
css.walkAtRules('layer', atRule => {
10-
const layer = atRule.params
11-
atRule.before(postcss.comment({ text: `tailwind start ${layer}` }))
12-
atRule.before(atRule.nodes)
13-
atRule.before(postcss.comment({ text: `tailwind end ${layer}` }))
14-
atRule.remove()
15-
})
16-
}
17-
18-
function convertControlCommentsToPurgeIgnoreComments(config) {
19-
return function(css) {
20-
const mode = _.get(config, 'purge.mode', 'conservative')
21-
22-
if (mode === 'conservative') {
23-
css.prepend(postcss.comment({ text: 'purgecss start ignore' }))
24-
css.append(postcss.comment({ text: 'purgecss end ignore' }))
25-
26-
css.walkComments(comment => {
27-
switch (comment.text.trim()) {
28-
case 'tailwind start utilities':
29-
comment.text = 'purgecss end ignore'
30-
break
31-
case 'tailwind end utilities':
32-
comment.text = 'purgecss start ignore'
33-
break
34-
default:
35-
break
36-
}
37-
})
38-
}
39-
}
40-
}
41-
428
function removeTailwindComments(css) {
439
css.walkComments(comment => {
4410
switch (comment.text.trim()) {
@@ -62,7 +28,7 @@ export default function purgeUnusedUtilities(config) {
6228
)
6329

6430
if (!purgeEnabled) {
65-
return postcss([convertLayersToControlComments, removeTailwindComments])
31+
return removeTailwindComments
6632
}
6733

6834
// Skip if `purge: []` since that's part of the default config
@@ -86,8 +52,27 @@ export default function purgeUnusedUtilities(config) {
8652
}
8753

8854
return postcss([
89-
convertLayersToControlComments,
90-
convertControlCommentsToPurgeIgnoreComments(config),
55+
function(css) {
56+
const mode = _.get(config, 'purge.mode', 'conservative')
57+
58+
if (mode === 'conservative') {
59+
css.prepend(postcss.comment({ text: 'purgecss start ignore' }))
60+
css.append(postcss.comment({ text: 'purgecss end ignore' }))
61+
62+
css.walkComments(comment => {
63+
switch (comment.text.trim()) {
64+
case 'tailwind start utilities':
65+
comment.text = 'purgecss end ignore'
66+
break
67+
case 'tailwind end utilities':
68+
comment.text = 'purgecss start ignore'
69+
break
70+
default:
71+
break
72+
}
73+
})
74+
}
75+
},
9176
removeTailwindComments,
9277
purgecss({
9378
content: Array.isArray(config.purge) ? config.purge : config.purge.content,

src/processTailwindFeatures.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import substituteTailwindAtRules from './lib/substituteTailwindAtRules'
55
import evaluateTailwindFunctions from './lib/evaluateTailwindFunctions'
66
import substituteVariantsAtRules from './lib/substituteVariantsAtRules'
77
import substituteResponsiveAtRules from './lib/substituteResponsiveAtRules'
8+
import convertLayerAtRulesToControlComments from './lib/convertLayerAtRulesToControlComments'
89
import substituteScreenAtRules from './lib/substituteScreenAtRules'
910
import substituteClassApplyAtRules from './lib/substituteClassApplyAtRules'
1011
import purgeUnusedStyles from './lib/purgeUnusedStyles'
@@ -22,6 +23,7 @@ export default function(getConfig) {
2223
evaluateTailwindFunctions(config),
2324
substituteVariantsAtRules(config, processedPlugins),
2425
substituteResponsiveAtRules(config),
26+
convertLayerAtRulesToControlComments(config),
2527
substituteScreenAtRules(config),
2628
substituteClassApplyAtRules(config, processedPlugins.utilities),
2729
purgeUnusedStyles(config),

0 commit comments

Comments
 (0)