/
index.test.ts
405 lines (370 loc) · 13 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import { createGenerator } from '@unocss/core'
import { format as prettier } from 'prettier'
import { describe, expect, it } from 'vitest'
// @ts-expect-error missing types
import prettierSvelte from 'prettier-plugin-svelte'
import presetUno from '@unocss/preset-uno'
import presetIcons from '@unocss/preset-icons'
import { transformClasses } from '.'
describe('transform', () => {
const safelistClassToSkip = 'mr-7'
const uno = createGenerator({
presets: [
presetUno(),
presetIcons({
prefix: 'i-',
extraProperties: {
'display': 'inline-block',
'vertical-align': 'middle',
},
}),
],
shortcuts: [
{ shortcut: 'w-5' },
{ logo: 'i-logos:svelte-icon w-6em' },
],
safelist: [safelistClassToSkip],
})
async function transform(content: string, { combine = true, format = true } = {}) {
const transformed = (await transformClasses({ content, filename: 'Foo.svelte', uno, options: { combine } }))?.code
if (transformed && format) {
return prettier(transformed, {
parser: 'svelte',
plugins: [prettierSvelte],
})
}
return transformed
}
it('simple', async () => {
const code = '<div class="bg-red-500" />'
expect(await transform(code)).toMatchInlineSnapshot(`
"<div class=\\"uno-orrz3z\\" />
<style>
:global(.uno-orrz3z) {
--un-bg-opacity: 1;
background-color: rgba(239, 68, 68, var(--un-bg-opacity));
}
</style>
"
`)
expect(await transform(code, { combine: false })).toMatchInlineSnapshot(`
"<div class=\\"_bg-red-500_7dkb0w\\" />
<style>
:global(._bg-red-500_7dkb0w) {
--un-bg-opacity: 1;
background-color: rgba(239, 68, 68, var(--un-bg-opacity));
}
</style>
"
`)
})
it('also hashes shortcut names', async () => {
const code = `
<div class="shortcut mb-1 foo" />
<div class:shortcut />
`.trim()
expect(await transform(code)).toMatchInlineSnapshot(`
"<div class=\\"uno-azh9r1 foo\\" />
<div class:uno-4cfcv2={shortcut} />
<style>
:global(.uno-azh9r1) {
margin-bottom: 0.25rem;
width: 1.25rem;
}
:global(.uno-4cfcv2) {
width: 1.25rem;
}
</style>
"
`)
expect(await transform(code, { combine: false })).toMatchInlineSnapshot(`
"<div class=\\"_shortcut_7dkb0w _mb-1_7dkb0w foo\\" />
<div class:_shortcut_7dkb0w={shortcut} />
<style>
:global(._mb-1_7dkb0w) {
margin-bottom: 0.25rem;
}
:global(._shortcut_7dkb0w) {
width: 1.25rem;
}
</style>
"
`)
})
it('wraps parent and child dependent classes like rtl: and space-x-1 with :global() wrapper', async () => {
const code = '<div class="mb-1 text-sm rtl:right-0 space-x-1" />'
expect(await transform(code)).toMatchInlineSnapshot(`
"<div class=\\"uno-tlm2ul\\" />
<style>
:global([dir=\\"rtl\\"] .uno-tlm2ul) {
right: 0;
}
:global(.uno-tlm2ul) {
margin-bottom: 0.25rem;
font-size: 0.875rem;
line-height: 1.25rem;
}
:global(.uno-tlm2ul > :not([hidden]) ~ :not([hidden])) {
--un-space-x-reverse: 0;
margin-left: calc(0.25rem * calc(1 - var(--un-space-x-reverse)));
margin-right: calc(0.25rem * var(--un-space-x-reverse));
}
</style>
"
`)
expect(await transform(code, { combine: false })).toMatchInlineSnapshot(`
"<div
class=\\"_mb-1_7dkb0w _text-sm_7dkb0w _rtl:right-0_7dkb0w _space-x-1_7dkb0w\\"
/>
<style>
:global([dir=\\"rtl\\"] ._rtl\\\\:right-0_7dkb0w) {
right: 0;
}
:global(._mb-1_7dkb0w) {
margin-bottom: 0.25rem;
}
:global(._space-x-1_7dkb0w > :not([hidden]) ~ :not([hidden])) {
--un-space-x-reverse: 0;
margin-left: calc(0.25rem * calc(1 - var(--un-space-x-reverse)));
margin-right: calc(0.25rem * var(--un-space-x-reverse));
}
:global(._text-sm_7dkb0w) {
font-size: 0.875rem;
line-height: 1.25rem;
}
</style>
"
`)
})
it('handles class directives, including shorthand syntax; uses same hash for multiple occurrences of same class(es)', async () => {
const result = await transform(`
<div class="flex"/>
<div class:flex={bar} />
<div class:flex />
<div class:flex />
<div class:flex />
<div class:flex class="bar" />
`.trim(), { format: false })
expect(result).toMatchInlineSnapshot(`
"<div class=\\"uno-oo7fkj\\"/>
<div class:uno-oo7fkj={bar} />
<div class:uno-oo7fkj={flex} />
<div class:uno-oo7fkj={flex} />
<div class:uno-oo7fkj={flex} />
<div class:uno-oo7fkj={flex} class=\\"bar\\" />
<style>:global(.uno-oo7fkj){display:flex;}</style>"
`)
})
it('order of utility classes does not styles output (though hash will be different)', async () => {
const order1 = await transform('<div class="flex bg-blue-400 my-awesome-class font-bold"></div>')
const order2 = await transform('<div class="my-awesome-class bg-blue-400 font-bold flex"></div>')
const extractCss = (css: string) => css.match(/{([^}]*)}/)![1]
expect(extractCss(order1!)).toEqual(extractCss(order2!))
})
it(':global() properly handles @media queries', async () => {
const result = await transform(`
<div class="dark:hover:sm:space-x-1" />`.trim())
expect(result).toMatchInlineSnapshot(`
"<div class=\\"uno-1eyzu3\\" />
<style>
@media (min-width: 640px) {
:global(.dark .uno-1eyzu3:hover > :not([hidden]) ~ :not([hidden])) {
--un-space-x-reverse: 0;
margin-left: calc(0.25rem * calc(1 - var(--un-space-x-reverse)));
margin-right: calc(0.25rem * var(--un-space-x-reverse));
}
}
</style>
"
`)
})
it('does not place :global() around animate-bounce keyframe digits', async () => {
const result = await transform('<div class="animate-bounce" />')
expect(result).toMatchInlineSnapshot(`
"<div class=\\"uno-swfyci\\" />
<style>
:global(.uno-swfyci) {
animation: bounce 1s linear infinite;
}
@keyframes bounce {
0%,
100% {
transform: translateY(-25%);
animation-timing-function: cubic-bezier(0.8, 0, 1, 1);
}
50% {
transform: translateY(0);
animation-timing-function: cubic-bezier(0, 0, 0.2, 1);
}
}
</style>
"
`)
})
it('shortcut with icon', async () => {
const result = await transform('<span class="logo" />')
expect(result).toMatchFileSnapshot('./test-outputs/ShortcutWithIcon.svelte')
})
it('handles backticks and single quotes', async () => {
const backticks = await transform('<span class=`font-bold` />', { format: false })
expect(backticks).toMatchInlineSnapshot(`
"<span class=\`uno-k2ufqh\` />
<style>:global(.uno-k2ufqh){font-weight:700;}</style>"
`)
const singleQuotes = await transform(`
<span class='font-bold' />`.trim())
expect(singleQuotes).toMatchInlineSnapshot(`
"<span class=\\"uno-k2ufqh\\" />
<style>
:global(.uno-k2ufqh) {
font-weight: 700;
}
</style>
"
`)
})
it('handles classes in inline conditionals', async () => {
// writing `class:text-red-600={err} class:text-green-600={!err}` is best practice but people commonly use inline conditionals as demoed in this test and supporting them makes for an easier migration from a normal global Uno stylesheet usage or other Tailwind based tools.
const result = await transform(`
<span class="font-bold {bar ? 'text-red-600' : 'text-(green-500 blue-400) font-semibold boo'} underline foo {baz ? 'italic ' : ''}">Hello</span>`.trim())
expect(result).toMatchInlineSnapshot(`
"<span
class=\\"uno-r4l94t foo {bar ? 'uno-ffvc5a' : 'uno-3h14cd boo'} {baz
? 'uno-br1nw8'
: ''}\\">Hello</span
>
<style>
:global(.uno-3h14cd) {
font-weight: 600;
--un-text-opacity: 1;
color: rgba(34, 197, 94, var(--un-text-opacity));
color: rgba(96, 165, 250, var(--un-text-opacity));
}
:global(.uno-r4l94t) {
font-weight: 700;
text-decoration-line: underline;
}
:global(.uno-br1nw8) {
font-style: italic;
}
:global(.uno-ffvc5a) {
--un-text-opacity: 1;
color: rgba(220, 38, 38, var(--un-text-opacity));
}
</style>
"
`)
})
it('no tokens found returns undefined', async () => {
const result = await transform(`
<div class="foo" />
<style global>
.foo {
color: red;
}
</style>`.trim())
expect(result).toBe(undefined)
})
it('in dev, when it only hashes but does not combine, handles classes that fail when coming at the beginning of a shortcut name', async () => {
const code = '<div class="mb-1 !mt-2 md:mr-3 space-x-1" />'
expect(await transform(code)).toMatchInlineSnapshot(`
"<div class=\\"uno-83is87\\" />
<style>
:global(.uno-83is87) {
margin-bottom: 0.25rem;
margin-top: 0.5rem !important;
}
:global(.uno-83is87 > :not([hidden]) ~ :not([hidden])) {
--un-space-x-reverse: 0;
margin-left: calc(0.25rem * calc(1 - var(--un-space-x-reverse)));
margin-right: calc(0.25rem * var(--un-space-x-reverse));
}
@media (min-width: 768px) {
:global(.uno-83is87) {
margin-right: 0.75rem;
}
}
</style>
"
`)
expect(await transform(code, { combine: false })).toMatchInlineSnapshot(`
"<div class=\\"_mb-1_7dkb0w _!mt-2_7dkb0w _md:mr-3_7dkb0w _space-x-1_7dkb0w\\" />
<style>
:global(._\\\\!mt-2_7dkb0w) {
margin-top: 0.5rem !important;
}
:global(._mb-1_7dkb0w) {
margin-bottom: 0.25rem;
}
:global(._space-x-1_7dkb0w > :not([hidden]) ~ :not([hidden])) {
--un-space-x-reverse: 0;
margin-left: calc(0.25rem * calc(1 - var(--un-space-x-reverse)));
margin-right: calc(0.25rem * var(--un-space-x-reverse));
}
@media (min-width: 768px) {
:global(._md\\\\:mr-3_7dkb0w) {
margin-right: 0.75rem;
}
}
</style>
"
`)
})
it('skips generation for classes in safelist as they are set globally and compilation would remove them', async () => {
const code = `<div class="bg-red-500 ${safelistClassToSkip}" />`
const output = await transform(code)
expect(output).toContain(safelistClassToSkip)
expect(output).toMatchInlineSnapshot(`
"<div class=\\"uno-orrz3z mr-7\\" />
<style>
:global(.uno-orrz3z) {
--un-bg-opacity: 1;
background-color: rgba(239, 68, 68, var(--un-bg-opacity));
}
</style>
"
`)
})
it('does not add styles to a commented out style tag', async () => {
const code = `<div class="hidden" />
<!-- <style></style> -->`
const output = await transform(code)
expect(output).toMatchInlineSnapshot(`
"<div class=\\"uno-ssrvwc\\" />
<!-- <style></style> -->
<style>
:global(.uno-ssrvwc) {
display: none;
}
</style>
"
`)
})
it('everything', async () => {
const code = `
<div class="bg-red-500 sm:text-xl dark:hover:bg-green-500 transform scale-5" />
<div class:logo class="foo bar" />
<div class:text-orange-400={foo} class="shortcut" />
<div class="text-center sm:text-left rtl:sm:text-right space-x-1 rtl:space-x-reverse foo">
<div class="text-sm hover:text-red" />
<Button class="hover:text-red text-sm" />
</div>
`.trim()
await expect(await transform(code)).toMatchFileSnapshot('./test-outputs/EverythingProd.svelte')
await expect(await transform(code, { combine: false })).toMatchFileSnapshot('./test-outputs/EverythingDev.svelte')
})
// BUG: When this plugin is run on a component library first, and then in a project second, make sure to use different hashing prefixes because when `uno.parseToken()` checks a previously hashed class like `.uno-ssrvwc` it will add it to uno's cache of non-matches, then when `uno.generate()` runs it will not output the result of that shortcut. I don't know the proper solution to this and I don't think clearing uno's cache of non-matches is right. To see this bug run the following test:
it.skip('BUG: when a hashed style already exists (from an imported component library that was already processed), and style is found again it will not be output', async () => {
const result = await transform(`
<div class="uno-ssrvwc hidden" />`.trim())
expect(result).toMatchInlineSnapshot(`
"<div class=\\"uno-ssrvwc\\" />
<style>
:global(.uno-ssrvwc) {
display: none;
}
</style>
"
`)
})
})