|
| 1 | +import dedent from 'dedent' |
| 2 | +import postcss from 'postcss' |
| 3 | +import { expect, it } from 'vitest' |
| 4 | +import { migrateTailwindDirectives } from './migrate-tailwind-directives' |
| 5 | + |
| 6 | +const css = dedent |
| 7 | + |
| 8 | +function migrate(input: string) { |
| 9 | + return postcss() |
| 10 | + .use(migrateTailwindDirectives()) |
| 11 | + .process(input, { from: expect.getState().testPath }) |
| 12 | + .then((result) => result.css) |
| 13 | +} |
| 14 | + |
| 15 | +it("should not migrate `@import 'tailwindcss'`", async () => { |
| 16 | + expect( |
| 17 | + await migrate(css` |
| 18 | + @import 'tailwindcss'; |
| 19 | + `), |
| 20 | + ).toEqual(css` |
| 21 | + @import 'tailwindcss'; |
| 22 | + `) |
| 23 | +}) |
| 24 | + |
| 25 | +it('should migrate the default @tailwind directives to a single import', async () => { |
| 26 | + expect( |
| 27 | + await migrate(css` |
| 28 | + @tailwind base; |
| 29 | + @tailwind components; |
| 30 | + @tailwind utilities; |
| 31 | + `), |
| 32 | + ).toEqual(css` |
| 33 | + @import 'tailwindcss'; |
| 34 | + `) |
| 35 | +}) |
| 36 | + |
| 37 | +it('should migrate the default @tailwind directives as imports to a single import', async () => { |
| 38 | + expect( |
| 39 | + await migrate(css` |
| 40 | + @import 'tailwindcss/base'; |
| 41 | + @import 'tailwindcss/components'; |
| 42 | + @import 'tailwindcss/utilities'; |
| 43 | + `), |
| 44 | + ).toEqual(css` |
| 45 | + @import 'tailwindcss'; |
| 46 | + `) |
| 47 | +}) |
| 48 | + |
| 49 | +it.each([ |
| 50 | + [ |
| 51 | + // The default order |
| 52 | + css` |
| 53 | + @tailwind base; |
| 54 | + @tailwind components; |
| 55 | + @tailwind utilities; |
| 56 | + `, |
| 57 | + css` |
| 58 | + @import 'tailwindcss'; |
| 59 | + `, |
| 60 | + ], |
| 61 | + |
| 62 | + // @tailwind components moved, but has no effect in v4. Therefore `base` and |
| 63 | + // `utilities` are still in the correct order. |
| 64 | + [ |
| 65 | + css` |
| 66 | + @tailwind base; |
| 67 | + @tailwind utilities; |
| 68 | + @tailwind components; |
| 69 | + `, |
| 70 | + css` |
| 71 | + @import 'tailwindcss'; |
| 72 | + `, |
| 73 | + ], |
| 74 | + |
| 75 | + // Same as previous comment |
| 76 | + [ |
| 77 | + css` |
| 78 | + @tailwind components; |
| 79 | + @tailwind base; |
| 80 | + @tailwind utilities; |
| 81 | + `, |
| 82 | + css` |
| 83 | + @import 'tailwindcss'; |
| 84 | + `, |
| 85 | + ], |
| 86 | + |
| 87 | + // `base` and `utilities` swapped order, thus the `@layer` directives are |
| 88 | + // needed. The `components` directive is still ignored. |
| 89 | + [ |
| 90 | + css` |
| 91 | + @tailwind components; |
| 92 | + @tailwind utilities; |
| 93 | + @tailwind base; |
| 94 | + `, |
| 95 | + css` |
| 96 | + @layer theme, components, utilities, base; |
| 97 | + @import 'tailwindcss'; |
| 98 | + `, |
| 99 | + ], |
| 100 | + [ |
| 101 | + css` |
| 102 | + @tailwind utilities; |
| 103 | + @tailwind base; |
| 104 | + @tailwind components; |
| 105 | + `, |
| 106 | + css` |
| 107 | + @layer theme, components, utilities, base; |
| 108 | + @import 'tailwindcss'; |
| 109 | + `, |
| 110 | + ], |
| 111 | + [ |
| 112 | + css` |
| 113 | + @tailwind utilities; |
| 114 | + @tailwind components; |
| 115 | + @tailwind base; |
| 116 | + `, |
| 117 | + css` |
| 118 | + @layer theme, components, utilities, base; |
| 119 | + @import 'tailwindcss'; |
| 120 | + `, |
| 121 | + ], |
| 122 | +])( |
| 123 | + 'should migrate the default directives (but in different order) to a single import, order %#', |
| 124 | + async (input, expected) => { |
| 125 | + expect(await migrate(input)).toEqual(expected) |
| 126 | + }, |
| 127 | +) |
| 128 | + |
| 129 | +it('should migrate `@tailwind base` to theme and preflight imports', async () => { |
| 130 | + expect( |
| 131 | + await migrate(css` |
| 132 | + @tailwind base; |
| 133 | + `), |
| 134 | + ).toEqual(css` |
| 135 | + @import 'tailwindcss/theme' layer(theme); |
| 136 | + @import 'tailwindcss/preflight' layer(base); |
| 137 | + `) |
| 138 | +}) |
| 139 | + |
| 140 | +it('should migrate `@import "tailwindcss/base"` to theme and preflight imports', async () => { |
| 141 | + expect( |
| 142 | + await migrate(css` |
| 143 | + @import 'tailwindcss/base'; |
| 144 | + `), |
| 145 | + ).toEqual(css` |
| 146 | + @import 'tailwindcss/theme' layer(theme); |
| 147 | + @import 'tailwindcss/preflight' layer(base); |
| 148 | + `) |
| 149 | +}) |
| 150 | + |
| 151 | +it('should migrate `@tailwind utilities` to an import', async () => { |
| 152 | + expect( |
| 153 | + await migrate(css` |
| 154 | + @tailwind utilities; |
| 155 | + `), |
| 156 | + ).toEqual(css` |
| 157 | + @import 'tailwindcss/utilities' layer(utilities); |
| 158 | + `) |
| 159 | +}) |
| 160 | + |
| 161 | +it('should migrate `@import "tailwindcss/utilities"` to an import', async () => { |
| 162 | + expect( |
| 163 | + await migrate(css` |
| 164 | + @import 'tailwindcss/utilities'; |
| 165 | + `), |
| 166 | + ).toEqual(css` |
| 167 | + @import 'tailwindcss/utilities' layer(utilities); |
| 168 | + `) |
| 169 | +}) |
| 170 | + |
| 171 | +it('should not migrate existing imports using a custom layer', async () => { |
| 172 | + expect( |
| 173 | + await migrate(css` |
| 174 | + @import 'tailwindcss/utilities' layer(my-utilities); |
| 175 | + `), |
| 176 | + ).toEqual(css` |
| 177 | + @import 'tailwindcss/utilities' layer(my-utilities); |
| 178 | + `) |
| 179 | +}) |
| 180 | + |
| 181 | +// We don't have a `@layer components` anymore, so omitting it should result |
| 182 | +// in the full import as well. Alternatively, we could expand to: |
| 183 | +// |
| 184 | +// ```css |
| 185 | +// @import 'tailwindcss/theme' layer(theme); |
| 186 | +// @import 'tailwindcss/preflight' layer(base); |
| 187 | +// @import 'tailwindcss/utilities' layer(utilities); |
| 188 | +// ``` |
| 189 | +it('should migrate `@tailwind base` and `@tailwind utilities` to a single import', async () => { |
| 190 | + expect( |
| 191 | + await migrate(css` |
| 192 | + @tailwind base; |
| 193 | + @tailwind utilities; |
| 194 | + `), |
| 195 | + ).toEqual(css` |
| 196 | + @import 'tailwindcss'; |
| 197 | + `) |
| 198 | +}) |
| 199 | + |
| 200 | +it('should drop `@tailwind screens;`', async () => { |
| 201 | + expect( |
| 202 | + await migrate(css` |
| 203 | + @tailwind screens; |
| 204 | + `), |
| 205 | + ).toEqual('') |
| 206 | +}) |
| 207 | + |
| 208 | +it('should drop `@tailwind variants;`', async () => { |
| 209 | + expect( |
| 210 | + await migrate(css` |
| 211 | + @tailwind variants; |
| 212 | + `), |
| 213 | + ).toEqual('') |
| 214 | +}) |
0 commit comments