|
| 1 | +import dedent from 'dedent' |
| 2 | +import { describe, expect } from 'vitest' |
| 3 | +import { css, fetchStyles, html, retryAssertion, test, ts, txt } from '../utils' |
| 4 | + |
| 5 | +function createSetup(transformer: 'postcss' | 'lightningcss') { |
| 6 | + return { |
| 7 | + fs: { |
| 8 | + 'package.json': txt` |
| 9 | + { |
| 10 | + "type": "module", |
| 11 | + "dependencies": { |
| 12 | + "@tailwindcss/vite": "workspace:^", |
| 13 | + "tailwindcss": "workspace:^" |
| 14 | + }, |
| 15 | + "devDependencies": { |
| 16 | + ${transformer === 'lightningcss' ? `"lightningcss": "^1.26.0",` : ''} |
| 17 | + "vite": "^5.3.5" |
| 18 | + } |
| 19 | + } |
| 20 | + `, |
| 21 | + 'vite.config.ts': ts` |
| 22 | + import tailwindcss from '@tailwindcss/vite' |
| 23 | + import { defineConfig } from 'vite' |
| 24 | +
|
| 25 | + export default defineConfig({ |
| 26 | + css: ${transformer === 'postcss' ? '{}' : "{ transformer: 'lightningcss' }"}, |
| 27 | + build: { cssMinify: false }, |
| 28 | + plugins: [ |
| 29 | + tailwindcss(), |
| 30 | + { |
| 31 | + name: 'recolor', |
| 32 | + transform(code, id) { |
| 33 | + if (id.includes('.css')) { |
| 34 | + return code.replace(/red;/g, 'blue;') |
| 35 | + } |
| 36 | + }, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }) |
| 40 | + `, |
| 41 | + 'index.html': html` |
| 42 | + <head> |
| 43 | + <link rel="stylesheet" href="./src/index.css" /> |
| 44 | + </head> |
| 45 | + <body> |
| 46 | + <div class="foo [background-color:red]">Hello, world!</div> |
| 47 | + </body> |
| 48 | + `, |
| 49 | + 'src/index.css': css` |
| 50 | + @import 'tailwindcss/theme' theme(reference); |
| 51 | + @import 'tailwindcss/utilities'; |
| 52 | +
|
| 53 | + .foo { |
| 54 | + color: red; |
| 55 | + } |
| 56 | + `, |
| 57 | + }, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +for (let transformer of ['postcss', 'lightningcss'] as const) { |
| 62 | + describe(transformer, () => { |
| 63 | + test(`production build`, createSetup(transformer), async ({ fs, exec }) => { |
| 64 | + await exec('pnpm vite build') |
| 65 | + |
| 66 | + let files = await fs.glob('dist/**/*.css') |
| 67 | + expect(files).toHaveLength(1) |
| 68 | + let [filename] = files[0] |
| 69 | + |
| 70 | + await fs.expectFileToContain(filename, [ |
| 71 | + css` |
| 72 | + .foo { |
| 73 | + color: blue; |
| 74 | + } |
| 75 | + `, |
| 76 | + // Running the transforms on utilities generated by Tailwind might change in the future |
| 77 | + dedent` |
| 78 | + .\[background-color\:red\] { |
| 79 | + background-color: blue; |
| 80 | + } |
| 81 | + `, |
| 82 | + ]) |
| 83 | + }) |
| 84 | + |
| 85 | + test(`dev mode`, createSetup(transformer), async ({ spawn, getFreePort, fs }) => { |
| 86 | + let port = await getFreePort() |
| 87 | + await spawn(`pnpm vite dev --port ${port}`) |
| 88 | + |
| 89 | + await retryAssertion(async () => { |
| 90 | + let styles = await fetchStyles(port, '/index.html') |
| 91 | + expect(styles).toContain(css` |
| 92 | + .foo { |
| 93 | + color: blue; |
| 94 | + } |
| 95 | + `) |
| 96 | + // Running the transforms on utilities generated by Tailwind might change in the future |
| 97 | + expect(styles).toContain(dedent` |
| 98 | + .\[background-color\:red\] { |
| 99 | + background-color: blue; |
| 100 | + } |
| 101 | + `) |
| 102 | + }) |
| 103 | + |
| 104 | + await retryAssertion(async () => { |
| 105 | + await fs.write( |
| 106 | + 'src/index.css', |
| 107 | + css` |
| 108 | + @import 'tailwindcss/theme' theme(reference); |
| 109 | + @import 'tailwindcss/utilities'; |
| 110 | +
|
| 111 | + .foo { |
| 112 | + background-color: red; |
| 113 | + } |
| 114 | + `, |
| 115 | + ) |
| 116 | + |
| 117 | + let styles = await fetchStyles(port) |
| 118 | + expect(styles).toContain(css` |
| 119 | + .foo { |
| 120 | + background-color: blue; |
| 121 | + } |
| 122 | + `) |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + test('watch mode', createSetup(transformer), async ({ spawn, fs }) => { |
| 127 | + await spawn(`pnpm vite build --watch`) |
| 128 | + |
| 129 | + await retryAssertion(async () => { |
| 130 | + let files = await fs.glob('dist/**/*.css') |
| 131 | + expect(files).toHaveLength(1) |
| 132 | + let [, styles] = files[0] |
| 133 | + |
| 134 | + expect(styles).toContain(css` |
| 135 | + .foo { |
| 136 | + color: blue; |
| 137 | + } |
| 138 | + `) |
| 139 | + // Running the transforms on utilities generated by Tailwind might change in the future |
| 140 | + expect(styles).toContain(dedent` |
| 141 | + .\[background-color\:red\] { |
| 142 | + background-color: blue; |
| 143 | + } |
| 144 | + `) |
| 145 | + }) |
| 146 | + |
| 147 | + await retryAssertion(async () => { |
| 148 | + await fs.write( |
| 149 | + 'src/index.css', |
| 150 | + css` |
| 151 | + @import 'tailwindcss/theme' theme(reference); |
| 152 | + @import 'tailwindcss/utilities'; |
| 153 | +
|
| 154 | + .foo { |
| 155 | + background-color: red; |
| 156 | + } |
| 157 | + `, |
| 158 | + ) |
| 159 | + |
| 160 | + let files = await fs.glob('dist/**/*.css') |
| 161 | + expect(files).toHaveLength(1) |
| 162 | + let [, styles] = files[0] |
| 163 | + |
| 164 | + expect(styles).toContain(css` |
| 165 | + .foo { |
| 166 | + background-color: blue; |
| 167 | + } |
| 168 | + `) |
| 169 | + }) |
| 170 | + }) |
| 171 | + }) |
| 172 | +} |
0 commit comments