|
| 1 | +import fs from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import postcss from 'postcss' |
| 4 | +import tailwind from '../src/index' |
| 5 | +import defaultConfig from '../stubs/defaultConfig.stub.js' |
| 6 | + |
| 7 | +const config = { |
| 8 | + ...defaultConfig, |
| 9 | + theme: { |
| 10 | + extend: { |
| 11 | + colors: { |
| 12 | + 'black!': '#000', |
| 13 | + }, |
| 14 | + spacing: { |
| 15 | + '1.5': '0.375rem', |
| 16 | + '(1/2+8)': 'calc(50% + 2rem)', |
| 17 | + }, |
| 18 | + minHeight: { |
| 19 | + '(screen-4)': 'calc(100vh - 1rem)', |
| 20 | + }, |
| 21 | + fontFamily: { |
| 22 | + '%#$@': 'Comic Sans', |
| 23 | + }, |
| 24 | + }, |
| 25 | + }, |
| 26 | +} |
| 27 | + |
| 28 | +test('purges unused classes', () => { |
| 29 | + const OLD_NODE_ENV = process.env.NODE_ENV |
| 30 | + process.env.NODE_ENV = 'production' |
| 31 | + const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`) |
| 32 | + const input = fs.readFileSync(inputPath, 'utf8') |
| 33 | + |
| 34 | + return postcss([ |
| 35 | + tailwind({ |
| 36 | + ...config, |
| 37 | + purge: [path.resolve(`${__dirname}/fixtures/**/*.html`)], |
| 38 | + }), |
| 39 | + ]) |
| 40 | + .process(input, { from: inputPath }) |
| 41 | + .then(result => { |
| 42 | + process.env.NODE_ENV = OLD_NODE_ENV |
| 43 | + |
| 44 | + expect(result.css).not.toContain('.bg-red-600') |
| 45 | + expect(result.css).not.toContain('.w-1\\/3') |
| 46 | + expect(result.css).not.toContain('.flex') |
| 47 | + expect(result.css).not.toContain('.font-sans') |
| 48 | + expect(result.css).not.toContain('.text-right') |
| 49 | + expect(result.css).not.toContain('.px-4') |
| 50 | + expect(result.css).not.toContain('.h-full') |
| 51 | + |
| 52 | + expect(result.css).toContain('.bg-red-500') |
| 53 | + expect(result.css).toContain('.md\\:bg-blue-300') |
| 54 | + expect(result.css).toContain('.w-1\\/2') |
| 55 | + expect(result.css).toContain('.block') |
| 56 | + expect(result.css).toContain('.md\\:flow-root') |
| 57 | + expect(result.css).toContain('.h-screen') |
| 58 | + expect(result.css).toContain('.min-h-\\(screen-4\\)') |
| 59 | + expect(result.css).toContain('.bg-black\\!') |
| 60 | + expect(result.css).toContain('.font-\\%\\#\\$\\@') |
| 61 | + expect(result.css).toContain('.w-\\(1\\/2\\+8\\)') |
| 62 | + expect(result.css).toContain('.inline-grid') |
| 63 | + expect(result.css).toContain('.grid-cols-3') |
| 64 | + expect(result.css).toContain('.px-1\\.5') |
| 65 | + expect(result.css).toContain('.col-span-2') |
| 66 | + expect(result.css).toContain('.col-span-1') |
| 67 | + expect(result.css).toContain('.text-center') |
| 68 | + }) |
| 69 | +}) |
| 70 | + |
| 71 | +test('does not purge except in production', () => { |
| 72 | + const OLD_NODE_ENV = process.env.NODE_ENV |
| 73 | + process.env.NODE_ENV = 'development' |
| 74 | + const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`) |
| 75 | + const input = fs.readFileSync(inputPath, 'utf8') |
| 76 | + |
| 77 | + return postcss([ |
| 78 | + tailwind({ |
| 79 | + ...defaultConfig, |
| 80 | + purge: [path.resolve(`${__dirname}/fixtures/**/*.html`)], |
| 81 | + }), |
| 82 | + ]) |
| 83 | + .process(input, { from: inputPath }) |
| 84 | + .then(result => { |
| 85 | + process.env.NODE_ENV = OLD_NODE_ENV |
| 86 | + const expected = fs.readFileSync( |
| 87 | + path.resolve(`${__dirname}/fixtures/tailwind-output.css`), |
| 88 | + 'utf8' |
| 89 | + ) |
| 90 | + |
| 91 | + expect(result.css).toBe(expected) |
| 92 | + }) |
| 93 | +}) |
| 94 | + |
| 95 | +test('purges outside of production if explicitly enabled', () => { |
| 96 | + const OLD_NODE_ENV = process.env.NODE_ENV |
| 97 | + process.env.NODE_ENV = 'development' |
| 98 | + const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`) |
| 99 | + const input = fs.readFileSync(inputPath, 'utf8') |
| 100 | + |
| 101 | + return postcss([ |
| 102 | + tailwind({ |
| 103 | + ...config, |
| 104 | + purge: { enabled: true, content: [path.resolve(`${__dirname}/fixtures/**/*.html`)] }, |
| 105 | + }), |
| 106 | + ]) |
| 107 | + .process(input, { from: inputPath }) |
| 108 | + .then(result => { |
| 109 | + process.env.NODE_ENV = OLD_NODE_ENV |
| 110 | + |
| 111 | + expect(result.css).not.toContain('.bg-red-600') |
| 112 | + expect(result.css).not.toContain('.w-1\\/3') |
| 113 | + expect(result.css).not.toContain('.flex') |
| 114 | + expect(result.css).not.toContain('.font-sans') |
| 115 | + expect(result.css).not.toContain('.text-right') |
| 116 | + expect(result.css).not.toContain('.px-4') |
| 117 | + expect(result.css).not.toContain('.h-full') |
| 118 | + |
| 119 | + expect(result.css).toContain('.bg-red-500') |
| 120 | + expect(result.css).toContain('.md\\:bg-blue-300') |
| 121 | + expect(result.css).toContain('.w-1\\/2') |
| 122 | + expect(result.css).toContain('.block') |
| 123 | + expect(result.css).toContain('.md\\:flow-root') |
| 124 | + expect(result.css).toContain('.h-screen') |
| 125 | + expect(result.css).toContain('.min-h-\\(screen-4\\)') |
| 126 | + expect(result.css).toContain('.bg-black\\!') |
| 127 | + expect(result.css).toContain('.font-\\%\\#\\$\\@') |
| 128 | + expect(result.css).toContain('.w-\\(1\\/2\\+8\\)') |
| 129 | + expect(result.css).toContain('.inline-grid') |
| 130 | + expect(result.css).toContain('.grid-cols-3') |
| 131 | + expect(result.css).toContain('.px-1\\.5') |
| 132 | + expect(result.css).toContain('.col-span-2') |
| 133 | + expect(result.css).toContain('.col-span-1') |
| 134 | + expect(result.css).toContain('.text-center') |
| 135 | + }) |
| 136 | +}) |
| 137 | + |
| 138 | +test('purgecss options can be provided', () => { |
| 139 | + const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`) |
| 140 | + const input = fs.readFileSync(inputPath, 'utf8') |
| 141 | + |
| 142 | + return postcss([ |
| 143 | + tailwind({ |
| 144 | + ...config, |
| 145 | + purge: { |
| 146 | + enabled: true, |
| 147 | + options: { |
| 148 | + content: [path.resolve(`${__dirname}/fixtures/**/*.html`)], |
| 149 | + whitelist: ['md:bg-green-500'], |
| 150 | + }, |
| 151 | + }, |
| 152 | + }), |
| 153 | + ]) |
| 154 | + .process(input, { from: inputPath }) |
| 155 | + .then(result => { |
| 156 | + expect(result.css).not.toContain('.bg-red-600') |
| 157 | + expect(result.css).not.toContain('.w-1\\/3') |
| 158 | + expect(result.css).not.toContain('.flex') |
| 159 | + expect(result.css).not.toContain('.font-sans') |
| 160 | + expect(result.css).not.toContain('.text-right') |
| 161 | + expect(result.css).not.toContain('.px-4') |
| 162 | + expect(result.css).not.toContain('.h-full') |
| 163 | + |
| 164 | + expect(result.css).toContain('.md\\:bg-green-500') |
| 165 | + expect(result.css).toContain('.bg-red-500') |
| 166 | + expect(result.css).toContain('.md\\:bg-blue-300') |
| 167 | + expect(result.css).toContain('.w-1\\/2') |
| 168 | + expect(result.css).toContain('.block') |
| 169 | + expect(result.css).toContain('.md\\:flow-root') |
| 170 | + expect(result.css).toContain('.h-screen') |
| 171 | + expect(result.css).toContain('.min-h-\\(screen-4\\)') |
| 172 | + expect(result.css).toContain('.bg-black\\!') |
| 173 | + expect(result.css).toContain('.font-\\%\\#\\$\\@') |
| 174 | + expect(result.css).toContain('.w-\\(1\\/2\\+8\\)') |
| 175 | + expect(result.css).toContain('.inline-grid') |
| 176 | + expect(result.css).toContain('.grid-cols-3') |
| 177 | + expect(result.css).toContain('.px-1\\.5') |
| 178 | + expect(result.css).toContain('.col-span-2') |
| 179 | + expect(result.css).toContain('.col-span-1') |
| 180 | + expect(result.css).toContain('.text-center') |
| 181 | + }) |
| 182 | +}) |
| 183 | + |
| 184 | +test('can purge all CSS, not just Tailwind classes', () => { |
| 185 | + const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`) |
| 186 | + const input = fs.readFileSync(inputPath, 'utf8') |
| 187 | + |
| 188 | + return postcss([ |
| 189 | + tailwind({ |
| 190 | + ...config, |
| 191 | + purge: { |
| 192 | + enabled: true, |
| 193 | + mode: 'all', |
| 194 | + content: [path.resolve(`${__dirname}/fixtures/**/*.html`)], |
| 195 | + }, |
| 196 | + }), |
| 197 | + function(css) { |
| 198 | + // Remove any comments to avoid accidentally asserting against them |
| 199 | + // instead of against real CSS rules. |
| 200 | + css.walkComments(c => c.remove()) |
| 201 | + }, |
| 202 | + ]) |
| 203 | + .process(input, { from: inputPath }) |
| 204 | + .then(result => { |
| 205 | + expect(result.css).not.toContain('html') |
| 206 | + expect(result.css).not.toContain('body') |
| 207 | + expect(result.css).not.toContain('button') |
| 208 | + expect(result.css).not.toContain('legend') |
| 209 | + expect(result.css).not.toContain('progress') |
| 210 | + |
| 211 | + expect(result.css).toContain('.bg-red-500') |
| 212 | + expect(result.css).toContain('.md\\:bg-blue-300') |
| 213 | + expect(result.css).toContain('.w-1\\/2') |
| 214 | + expect(result.css).toContain('.block') |
| 215 | + expect(result.css).toContain('.md\\:flow-root') |
| 216 | + expect(result.css).toContain('.h-screen') |
| 217 | + expect(result.css).toContain('.min-h-\\(screen-4\\)') |
| 218 | + expect(result.css).toContain('.bg-black\\!') |
| 219 | + expect(result.css).toContain('.font-\\%\\#\\$\\@') |
| 220 | + expect(result.css).toContain('.w-\\(1\\/2\\+8\\)') |
| 221 | + expect(result.css).toContain('.inline-grid') |
| 222 | + expect(result.css).toContain('.grid-cols-3') |
| 223 | + expect(result.css).toContain('.px-1\\.5') |
| 224 | + expect(result.css).toContain('.col-span-2') |
| 225 | + expect(result.css).toContain('.col-span-1') |
| 226 | + expect(result.css).toContain('.text-center') |
| 227 | + }) |
| 228 | +}) |
| 229 | + |
| 230 | +test('the `conservative` mode can be set explicitly', () => { |
| 231 | + const OLD_NODE_ENV = process.env.NODE_ENV |
| 232 | + process.env.NODE_ENV = 'production' |
| 233 | + const inputPath = path.resolve(`${__dirname}/fixtures/tailwind-input.css`) |
| 234 | + const input = fs.readFileSync(inputPath, 'utf8') |
| 235 | + |
| 236 | + return postcss([ |
| 237 | + tailwind({ |
| 238 | + ...config, |
| 239 | + purge: { |
| 240 | + mode: 'conservative', |
| 241 | + content: [path.resolve(`${__dirname}/fixtures/**/*.html`)], |
| 242 | + }, |
| 243 | + }), |
| 244 | + ]) |
| 245 | + .process(input, { from: inputPath }) |
| 246 | + .then(result => { |
| 247 | + process.env.NODE_ENV = OLD_NODE_ENV |
| 248 | + |
| 249 | + expect(result.css).not.toContain('.bg-red-600') |
| 250 | + expect(result.css).not.toContain('.w-1\\/3') |
| 251 | + expect(result.css).not.toContain('.flex') |
| 252 | + expect(result.css).not.toContain('.font-sans') |
| 253 | + expect(result.css).not.toContain('.text-right') |
| 254 | + expect(result.css).not.toContain('.px-4') |
| 255 | + expect(result.css).not.toContain('.h-full') |
| 256 | + |
| 257 | + expect(result.css).toContain('.bg-red-500') |
| 258 | + expect(result.css).toContain('.md\\:bg-blue-300') |
| 259 | + expect(result.css).toContain('.w-1\\/2') |
| 260 | + expect(result.css).toContain('.block') |
| 261 | + expect(result.css).toContain('.md\\:flow-root') |
| 262 | + expect(result.css).toContain('.h-screen') |
| 263 | + expect(result.css).toContain('.min-h-\\(screen-4\\)') |
| 264 | + expect(result.css).toContain('.bg-black\\!') |
| 265 | + expect(result.css).toContain('.font-\\%\\#\\$\\@') |
| 266 | + expect(result.css).toContain('.w-\\(1\\/2\\+8\\)') |
| 267 | + expect(result.css).toContain('.inline-grid') |
| 268 | + expect(result.css).toContain('.grid-cols-3') |
| 269 | + expect(result.css).toContain('.px-1\\.5') |
| 270 | + expect(result.css).toContain('.col-span-2') |
| 271 | + expect(result.css).toContain('.col-span-1') |
| 272 | + expect(result.css).toContain('.text-center') |
| 273 | + }) |
| 274 | +}) |
0 commit comments