-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1639 from tailwindcss/purgecss
Integrate PurgeCSS directly into Tailwind
- Loading branch information
Showing
10 changed files
with
468 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!-- Basic HTML --> | ||
<div class="bg-red-500 md:bg-blue-300 w-1/2"></div> | ||
|
||
<!-- Vue dynamic classes --> | ||
<span :class="{ block: enabled, 'md:flow-root': !enabled }"></span> | ||
|
||
<!-- JSX with template strings --> | ||
<script> | ||
function Component() { | ||
return <div class={`h-screen`}></div> | ||
} | ||
</script> | ||
|
||
<!-- Custom classes with really weird characters --> | ||
<div class="min-h-(screen-4) bg-black! font-%#$@ w-(1/2+8)"></div> | ||
|
||
<!-- Pug --> | ||
span.inline-grid.grid-cols-3(class="px-1.5") | ||
.col-span-2 | ||
Hello | ||
.col-span-1.text-center | ||
World! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,274 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import postcss from 'postcss' | ||
import tailwind from '../src/index' | ||
import defaultConfig from '../stubs/defaultConfig.stub.js' | ||
|
||
const config = { | ||
...defaultConfig, | ||
theme: { | ||
extend: { | ||
colors: { | ||
'black!': '#000', | ||
}, | ||
spacing: { | ||
'1.5': '0.375rem', | ||
'(1/2+8)': 'calc(50% + 2rem)', | ||
}, | ||
minHeight: { | ||
'(screen-4)': 'calc(100vh - 1rem)', | ||
}, | ||
fontFamily: { | ||
'%#$@': 'Comic Sans', | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
test('purges unused classes', () => { | ||
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).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' | ||
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`) | ||
const input = fs.readFileSync(inputPath, 'utf8') | ||
|
||
return postcss([ | ||
tailwind({ | ||
...defaultConfig, | ||
purge: [path.resolve(`${__dirname}/fixtures/**/*.html`)], | ||
}), | ||
]) | ||
.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' | ||
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`) | ||
const input = fs.readFileSync(inputPath, 'utf8') | ||
|
||
return postcss([ | ||
tailwind({ | ||
...config, | ||
purge: { enabled: true, content: [path.resolve(`${__dirname}/fixtures/**/*.html`)] }, | ||
}), | ||
]) | ||
.process(input, { from: inputPath }) | ||
.then(result => { | ||
process.env.NODE_ENV = OLD_NODE_ENV | ||
|
||
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('purgecss options can be provided', () => { | ||
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`) | ||
const input = fs.readFileSync(inputPath, 'utf8') | ||
|
||
return postcss([ | ||
tailwind({ | ||
...config, | ||
purge: { | ||
enabled: true, | ||
options: { | ||
content: [path.resolve(`${__dirname}/fixtures/**/*.html`)], | ||
whitelist: ['md:bg-green-500'], | ||
}, | ||
}, | ||
}), | ||
]) | ||
.process(input, { from: inputPath }) | ||
.then(result => { | ||
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('.md\\:bg-green-500') | ||
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('can purge all CSS, not just Tailwind classes', () => { | ||
const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`) | ||
const input = fs.readFileSync(inputPath, 'utf8') | ||
|
||
return postcss([ | ||
tailwind({ | ||
...config, | ||
purge: { | ||
enabled: true, | ||
mode: 'all', | ||
content: [path.resolve(`${__dirname}/fixtures/**/*.html`)], | ||
}, | ||
}), | ||
function(css) { | ||
// Remove any comments to avoid accidentally asserting against them | ||
// instead of against real CSS rules. | ||
css.walkComments(c => c.remove()) | ||
}, | ||
]) | ||
.process(input, { from: inputPath }) | ||
.then(result => { | ||
expect(result.css).not.toContain('html') | ||
expect(result.css).not.toContain('body') | ||
expect(result.css).not.toContain('button') | ||
expect(result.css).not.toContain('legend') | ||
expect(result.css).not.toContain('progress') | ||
|
||
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('the `conservative` mode can be set explicitly', () => { | ||
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: { | ||
mode: 'conservative', | ||
content: [path.resolve(`${__dirname}/fixtures/**/*.html`)], | ||
}, | ||
}), | ||
]) | ||
.process(input, { from: inputPath }) | ||
.then(result => { | ||
process.env.NODE_ENV = OLD_NODE_ENV | ||
|
||
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.