Skip to content

Commit

Permalink
Don't purge components, don't purge if paths empty, add empty purge t…
Browse files Browse the repository at this point in the history
…o config stub
  • Loading branch information
adamwathan committed Apr 29, 2020
1 parent 3a53a8c commit 77d3f75
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
69 changes: 69 additions & 0 deletions __tests__/purgeUnusedStyles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,51 @@ test('purges unused classes', () => {
})
})

test('does not purge components', () => {
const OLD_NODE_ENV = process.env.NODE_ENV
process.env.NODE_ENV = 'production'
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`)
const input = fs.readFileSync(inputPath, 'utf8')

return postcss([
tailwind({
...config,
purge: [path.resolve(`${__dirname}/fixtures/**/*.html`)],
}),
])
.process(input, { from: inputPath })
.then(result => {
process.env.NODE_ENV = OLD_NODE_ENV

expect(result.css).toContain('.container')

expect(result.css).not.toContain('.bg-red-600')
expect(result.css).not.toContain('.w-1\\/3')
expect(result.css).not.toContain('.flex')
expect(result.css).not.toContain('.font-sans')
expect(result.css).not.toContain('.text-right')
expect(result.css).not.toContain('.px-4')
expect(result.css).not.toContain('.h-full')

expect(result.css).toContain('.bg-red-500')
expect(result.css).toContain('.md\\:bg-blue-300')
expect(result.css).toContain('.w-1\\/2')
expect(result.css).toContain('.block')
expect(result.css).toContain('.md\\:flow-root')
expect(result.css).toContain('.h-screen')
expect(result.css).toContain('.min-h-\\(screen-4\\)')
expect(result.css).toContain('.bg-black\\!')
expect(result.css).toContain('.font-\\%\\#\\$\\@')
expect(result.css).toContain('.w-\\(1\\/2\\+8\\)')
expect(result.css).toContain('.inline-grid')
expect(result.css).toContain('.grid-cols-3')
expect(result.css).toContain('.px-1\\.5')
expect(result.css).toContain('.col-span-2')
expect(result.css).toContain('.col-span-1')
expect(result.css).toContain('.text-center')
})
})

test('does not purge except in production', () => {
const OLD_NODE_ENV = process.env.NODE_ENV
process.env.NODE_ENV = 'development'
Expand All @@ -92,6 +137,30 @@ test('does not purge except in production', () => {
})
})

test('does not purge if the array is empty', () => {
const OLD_NODE_ENV = process.env.NODE_ENV
process.env.NODE_ENV = 'production'
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`)
const input = fs.readFileSync(inputPath, 'utf8')

return postcss([
tailwind({
...defaultConfig,
purge: [],
}),
])
.process(input, { from: inputPath })
.then(result => {
process.env.NODE_ENV = OLD_NODE_ENV
const expected = fs.readFileSync(
path.resolve(`${__dirname}/fixtures/tailwind-output.css`),
'utf8'
)

expect(result.css).toBe(expected)
})
})

test('purges outside of production if explicitly enabled', () => {
const OLD_NODE_ENV = process.env.NODE_ENV
process.env.NODE_ENV = 'development'
Expand Down
8 changes: 6 additions & 2 deletions src/lib/purgeUnusedStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export default function purgeUnusedUtilities(config) {
return removeTailwindComments
}

// Skip if `purge: []` since that's part of the default config
if (Array.isArray(config.purge) && config.purge.length === 0) {
console.log('Skipping purge because no template paths provided...')
return removeTailwindComments
}

return postcss([
function(css) {
const mode = _.get(config, 'purge.mode', 'conservative')
Expand All @@ -38,12 +44,10 @@ export default function purgeUnusedUtilities(config) {

css.walkComments(comment => {
switch (comment.text.trim()) {
case 'tailwind start components':
case 'tailwind start utilities':
case 'tailwind start screens':
comment.text = 'purgecss end ignore'
break
case 'tailwind end components':
case 'tailwind end utilities':
case 'tailwind end screens':
comment.text = 'purgecss start ignore'
Expand Down
1 change: 1 addition & 0 deletions stubs/defaultConfig.stub.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
purge: [],
prefix: '',
important: false,
separator: ':',
Expand Down
1 change: 1 addition & 0 deletions stubs/simpleConfig.stub.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
purge: [],
theme: {
extend: {},
},
Expand Down

0 comments on commit 77d3f75

Please sign in to comment.