Skip to content

Commit 45cf3c5

Browse files
authored
Fix issue where inserting extra PurgeCSS control comments could break integrated PurgeCSS support (#2331)
1 parent 6a39e36 commit 45cf3c5

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing new yet!
10+
- Fix [issue](https://github.com/tailwindlabs/tailwindcss/issues/2258) where inserting extra PurgeCSS control comments could break integrated PurgeCSS support
1111

1212
## [1.8.3] - 2020-09-05
1313

__tests__/purgeUnusedStyles.test.js

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,19 @@ test('custom css in a layer is purged by default when using layers mode', () =>
195195
])
196196
.process(
197197
`
198-
@tailwind base;
198+
@tailwind base;
199199
200-
@tailwind components;
200+
@tailwind components;
201201
202-
@layer components {
203-
.example {
204-
@apply font-bold;
205-
color: theme('colors.red.500');
202+
@layer components {
203+
.example {
204+
@apply font-bold;
205+
color: theme('colors.red.500');
206+
}
206207
}
207-
}
208208
209-
@tailwind utilities;
210-
`,
209+
@tailwind utilities;
210+
`,
211211
{ from: null }
212212
)
213213
.then(result => {
@@ -411,6 +411,43 @@ test('does not purge components when mode is conservative', () => {
411411
)
412412
})
413413

414+
test('extra purgecss control comments can be added manually', () => {
415+
return inProduction(
416+
suppressConsoleLogs(() => {
417+
const input = `
418+
@tailwind base;
419+
420+
/* purgecss start ignore */
421+
.btn {
422+
background: red;
423+
}
424+
/* purgecss end ignore */
425+
426+
@tailwind components;
427+
@tailwind utilities;
428+
`
429+
430+
return postcss([
431+
tailwind({
432+
...config,
433+
purge: {
434+
layers: ['utilities'],
435+
content: [path.resolve(`${__dirname}/fixtures/**/*.html`)],
436+
},
437+
}),
438+
])
439+
.process(input, { from: null })
440+
.then(result => {
441+
const rules = extractRules(result.root)
442+
443+
expect(rules).toContain('.btn')
444+
expect(rules).toContain('.container')
445+
assertPurged(result)
446+
})
447+
})
448+
)
449+
})
450+
414451
test(
415452
'does not purge except in production',
416453
suppressConsoleLogs(() => {

src/lib/purgeUnusedStyles.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,18 @@ export default function purgeUnusedUtilities(config, configChanged) {
7777
? ['utilities']
7878
: _.get(config, 'purge.layers', ['base', 'components', 'utilities'])
7979

80-
css.prepend(postcss.comment({ text: 'purgecss start ignore' }))
81-
css.append(postcss.comment({ text: 'purgecss end ignore' }))
82-
8380
css.walkComments(comment => {
81+
switch (comment.text.trim()) {
82+
case `purgecss start ignore`:
83+
comment.before(postcss.comment({ text: 'purgecss end ignore' }))
84+
break
85+
case `purgecss end ignore`:
86+
comment.before(postcss.comment({ text: 'purgecss end ignore' }))
87+
comment.text = 'purgecss start ignore'
88+
break
89+
default:
90+
break
91+
}
8492
layers.forEach(layer => {
8593
switch (comment.text.trim()) {
8694
case `tailwind start ${layer}`:
@@ -94,6 +102,9 @@ export default function purgeUnusedUtilities(config, configChanged) {
94102
}
95103
})
96104
})
105+
106+
css.prepend(postcss.comment({ text: 'purgecss start ignore' }))
107+
css.append(postcss.comment({ text: 'purgecss end ignore' }))
97108
},
98109
removeTailwindMarkers,
99110
purgecss({

0 commit comments

Comments
 (0)