-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.test.js
481 lines (420 loc) · 15.2 KB
/
index.test.js
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* eslint-disable import/no-extraneous-dependencies */
import { resolve } from 'path';
import postcss from 'postcss';
import { test } from 'vitest';
import plugin from '.';
const parserOpts = {
from: resolve(__dirname, 'fixtures/from.css'),
to: resolve(__dirname, 'fixtures/to.css'),
};
function run(t, input, output, opts = {}, extraPlugins = []) {
return postcss([
...extraPlugins,
plugin(opts),
]).process(input, parserOpts).then((result) => {
t.expect(result.css).toBe(output);
t.expect(result.warnings().length).toBe(0);
});
}
// Simple plugin that replaces "black" with "purple" within @value declarations,
// used to test preprocess value transformation
const blackToPurplePlugin = () => ({
postcssPlugin: 'testBlackToPurple',
Once(root) {
root.walkAtRules('value', (atRule) => {
atRule.replaceWith(atRule.clone({
params: atRule.params.replace('black', 'purple'),
}));
});
},
});
blackToPurplePlugin.postcss = true;
test('should pass through an empty string', async (t) => {
await run(t, '', '');
});
test('should leave exports as is', async (t) => {
await run(t, '@value red blue;', '@value red blue;');
});
test('should leave other at rules alone if noEmitExports is true', async (t) => {
await run(t, '@font-face {}', '@font-face {}', { noEmitExports: true });
});
test('should remove exports if noEmitExports is true', async (t) => {
await run(t, '@value red blue;', '', { noEmitExports: true });
});
test('gives an error when there is no semicolon between lines', async (t) => {
const input = '@value red blue\n@value green yellow';
const processor = postcss([plugin]);
const result = await processor.process(input, { from: undefined });
const warnings = result.warnings();
t.expect(warnings.length).toBe(1);
t.expect(warnings[0].text).toBe('Invalid value definition: red blue\n@value green yellow');
});
test('gives an error when path to imported file is wrong', async (t) => {
const input = '@value red from "./non-existent-file.css"';
const processor = postcss([plugin]);
await t.expect(processor.process(input, parserOpts)).rejects.toThrow("Can't resolve './non-existent-file.css'");
});
test('gives an error when @value statement is invalid', async (t) => {
const input = '@value , from "./colors.css"';
const processor = postcss([plugin]);
await t.expect(processor.process(input, parserOpts)).rejects.toThrow('@value statement "" is invalid!');
});
test('shouldn\'t break on draft spec syntax', async (t) => {
await run(
t,
'.foo { width: calc(2+2); }',
'.foo { width: calc(2+2); }',
);
});
test('should replace constants within the file', async (t) => {
await run(
t,
'@value blue red; .foo { color: blue; }',
'@value blue red; .foo { color: red; }',
);
});
test('shouldn\'t replace number-like values', async (t) => {
await run(
t,
'@value 3char #000; .foo { color: 3char; }',
'@value 3char #000; .foo { color: 3char; }',
);
});
test('shouldn\'t replace selector', async (t) => {
await run(
t,
'@value blue red; .blue { color: blue; }',
'@value blue red; .blue { color: red; }',
);
});
test('shouldn\'t replace inside url', async (t) => {
await run(
t,
'@value blue red; .blue { background-image: url(blue.png); }',
'@value blue red; .blue { background-image: url(blue.png); }',
);
});
test('should replace within calc', async (t) => {
await run(
t,
'@value base: 10px;\n.a { margin: calc(base * 2); }',
'@value base: 10px;\n.a { margin: calc(10px * 2); }',
);
});
test('should replace within calc without spaces', async (t) => {
await run(
t,
'@value base: 10px;\n.a { margin: calc(base*2); }',
'@value base: 10px;\n.a { margin: calc(10px*2); }',
);
});
test('should replace two constants with same name within the file and the latter should win', async (t) => {
await run(
t,
'@value blue red; @value blue green; .foo { color: blue; }',
'@value blue red; @value blue green; .foo { color: green; }',
);
});
test('should replace an import', async (t) => {
await run(
t,
'@value red from "./colors.css";\n.foo { color: red; }',
'@value red from "./colors.css";\n.foo { color: #FF0000; }',
);
});
test('should replace a constant and an import with same name within the file and the latter should win', async (t) => {
await run(
t,
'@value red from "./colors.css"; @value red green; \n.foo { color: red; }',
'@value red from "./colors.css"; @value red green; \n.foo { color: green; }',
);
});
test('should replace an import from several files', async (t) => {
await run(
t,
`@value red from "./colors.css";
@value base from "./level1.css";
@value level2base from "./level2.css";
.a { margin: base; }
.b { margin: level2base; }
.foo { color: red; }`,
`@value red from "./colors.css";
@value base from "./level1.css";
@value level2base from "./level2.css";
.a { margin: 10px; }
.b { margin: 20px; }
.foo { color: #FF0000; }`,
);
});
test('should replace a constant and an import with same name within the file and the latter should win', async (t) => {
await run(
t,
'@value red green; @value red from "./colors.css";\n.foo { color: red; }',
'@value red green; @value red from "./colors.css";\n.foo { color: #FF0000; }',
);
});
test('should import and alias a constant and replace usages', async (t) => {
await run(
t,
'@value blue as green from "./colors.css";\n.foo { color: green; }',
'@value blue as green from "./colors.css";\n.foo { color: #0000FF; }',
);
});
test('should import and alias a constant (using a name from imported file) and replace usages', async (t) => {
await run(
t,
'@value blue as red from "./colors.css";\n.foo { color: red; }',
'@value blue as red from "./colors.css";\n.foo { color: #0000FF; }',
);
});
test('should import multiple from a single file', async (t) => {
await run(
t,
`@value blue, red from "./colors.css";
.foo { color: red; }
.bar { color: blue }`,
`@value blue, red from "./colors.css";
.foo { color: #FF0000; }
.bar { color: #0000FF }`,
);
});
test('should import from a definition and replace', async (t) => {
await run(
t,
'@value colors: "./colors.css"; @value red from colors;\n.foo { color: red; }',
'@value colors: "./colors.css"; @value red from colors;\n.foo { color: #FF0000; }',
);
});
test('should only allow values for paths if defined in the right order', async (t) => {
await run(
t,
' @value red from colors; @value colors: "./colors.css";\n.foo { color: red; }',
' @value red from colors; @value colors: "./colors.css";\n.foo { color: red; }',
);
});
test('should allow transitive values', async (t) => {
await run(
t,
'@value aaa: red;\n@value bbb: aaa;\n.a { color: bbb; }',
'@value aaa: red;\n@value bbb: red;\n.a { color: red; }',
);
});
test('shouldn\'t allow transitive values in urls', async (t) => {
await run(
t,
'@value aaa: red;\n@value bbb: url(aaa.png); \n.a { background-image: url(aaa.png); }',
'@value aaa: red;\n@value bbb: url(aaa.png); \n.a { background-image: url(aaa.png); }',
);
});
test('should allow transitive values within calc', async (t) => {
await run(
t,
'@value base: 10px;\n@value large: calc(base * 2);\n.a { margin: large; }',
'@value base: 10px;\n@value large: calc(10px * 2);\n.a { margin: calc(10px * 2); }',
);
});
test('should allow transitive values within calc without spaces', async (t) => {
await run(
t,
'@value base: 10px;\n@value large: calc(base*2);\n.a { margin: large; }',
'@value base: 10px;\n@value large: calc(10px*2);\n.a { margin: calc(10px*2); }',
);
});
test('should replace inside custom properties', async (t) => {
await run(
t,
'@value path: test.png;\n:root {--path: path};\n.foo { background-image: url(var(--path)); }',
'@value path: test.png;\n:root {--path: test.png};\n.foo { background-image: url(var(--path)); }',
);
});
test('should replace inside media queries by default, without specifying custom at-rules', async (t) => {
await run(
t,
'@value base: 10px;\n@media (min-width: calc(base * 200)) {}',
'@value base: 10px;\n@media (min-width: calc(10px * 200)) {}',
);
});
test('should replace inside media queries when it is specified as a custom at-rule', async (t) => {
await run(
t,
'@value base: 10px;\n@media (min-width: calc(base * 200)) {}',
'@value base: 10px;\n@media (min-width: calc(10px * 200)) {}',
{ atRules: ['media'] },
);
});
test('should replace inside media and container queries when they are specified as a custom at-rules', async (t) => {
await run(
t,
'@value base: 10px;\n@media (min-width: calc(base * 200)) {}\n@container (min-width: calc(base * 200)) {}',
'@value base: 10px;\n@media (min-width: calc(10px * 200)) {}\n@container (min-width: calc(10px * 200)) {}',
{ atRules: ['media', 'container'] },
);
});
test('should allow custom-property-style names', async (t) => {
await run(
t,
'@value --red from "./colors.css";\n.foo { color: --red; }',
'@value --red from "./colors.css";\n.foo { color: #FF0000; }',
);
});
test('should allow all colour types', async (t) => {
await run(
t,
'@value named: red; @value hex3char #0f0; @value hex6char #00ff00; @value rgba rgba(34, 12, 64, 0.3); @value hsla hsla(220, 13.0%, 18.0%, 1);\n'
+ '.foo { color: named; background-color: hex3char; border-top-color: hex6char; border-bottom-color: rgba; outline-color: hsla; }',
'@value named: red; @value hex3char #0f0; @value hex6char #00ff00; @value rgba rgba(34, 12, 64, 0.3); @value hsla hsla(220, 13.0%, 18.0%, 1);\n'
+ '.foo { color: red; background-color: #0f0; border-top-color: #00ff00; border-bottom-color: rgba(34, 12, 64, 0.3); outline-color: hsla(220, 13.0%, 18.0%, 1); }',
);
});
test('should import multiple from a single file on multiple lines', async (t) => {
await run(
t,
`@value (
blue,
red
) from "./colors.css";
.foo { color: red; }
.bar { color: blue }`,
`@value (
blue,
red
) from "./colors.css";
.foo { color: #FF0000; }
.bar { color: #0000FF }`,
);
});
test('should allow definitions with commas in them', async (t) => {
await run(
t,
'@value coolShadow: 0 11px 15px -7px rgba(0,0,0,.2),0 24px 38px 3px rgba(0,0,0,.14) ;\n'
+ '.foo { box-shadow: coolShadow; }',
'@value coolShadow: 0 11px 15px -7px rgba(0,0,0,.2),0 24px 38px 3px rgba(0,0,0,.14) ;\n'
+ '.foo { box-shadow: 0 11px 15px -7px rgba(0,0,0,.2),0 24px 38px 3px rgba(0,0,0,.14); }',
);
});
test('should allow values with nested parantheses', async (t) => {
await run(
t,
'@value aaa: color(red lightness(50%));\n.foo { color: aaa; }',
'@value aaa: color(red lightness(50%));\n.foo { color: color(red lightness(50%)); }',
);
});
test('should import and replace values transitively', async (t) => {
await run(
t,
'@value level2base from "./level1.css";\n.foo { prop: level2base; }',
'@value level2base from "./level1.css";\n.foo { prop: 20px; }',
);
});
test('should not import and replace not re-exported values', async (t) => {
await run(
t,
'@value level2hidden from "./level1.css";\n.foo { prop: level2hidden; }',
'@value level2hidden from "./level1.css";\n.foo { prop: level2hidden; }',
);
});
test('should replace a constant and an import with same name within the file and the latter should win in the middle of dependency tree', async (t) => {
await run(
t,
'@value level1shadow from "./level1.css";\n.foo { prop: level1shadow; }',
'@value level1shadow from "./level1.css";\n.foo { prop: level1shadow-value=level1; }',
);
});
test('should replace a constant and an import with same name within the file and the latter should win in the middle of dependency tree', async (t) => {
await run(
t,
'@value level2shadow from "./level1.css";\n.foo { prop: level2shadow; }',
'@value level2shadow from "./level1.css";\n.foo { prop: level2shadow-value=level2; }',
);
});
test('should allow imported transitive values within calc', async (t) => {
await run(
t,
'@value base from "./level1.css";\n@value large: calc(base * 2);\n.a { margin: large; }',
'@value base from "./level1.css";\n@value large: calc(10px * 2);\n.a { margin: calc(10px * 2); }',
);
});
test('should allow import of complex transitive values with calc', async (t) => {
await run(
t,
'@value huge from "./level1.css";\n.a { margin: huge; }',
'@value huge from "./level1.css";\n.a { margin: calc(10px * 4); }',
);
});
test('should allow imported transitive values within calc', async (t) => {
await run(
t,
'@value enormous from "./level1.css";\n.a { margin: enormous; }',
'@value enormous from "./level1.css";\n.a { margin: calc(20px * 4); }',
);
});
test('should replace an import from modules', async (t) => {
await run(
t,
'@value module from "module/module.css";\n.a { color: module; }',
'@value module from "module/module.css";\n.a { color: black; }',
);
});
test('should apply extra plugins to inner processing', async (t) => {
await run(
t,
'@value module from "module/module.css";\n.a { color: module; }',
'@value module from "module/module.css";\n.a { color: purple; }',
{ preprocessValues: true },
[blackToPurplePlugin()],
);
});
test('should replace an import from main file of module', async (t) => {
await run(
t,
'@value module from "module";\n.a { color: module; }',
'@value module from "module";\n.a { color: black; }',
);
});
test('should replace an import from scoped modules', async (t) => {
await run(
t,
'@value scoped-module from "@scope/module/module.css";\n.a { color: scoped-module; }',
'@value scoped-module from "@scope/module/module.css";\n.a { color: purple; }',
);
});
test('should resolve imports as module requests', async (t) => {
await run(
t,
'@value scoped-module from "~@scope/module/module.css";\n@value base from "level1.css";\n.a { color: scoped-module; width: base; }',
'@value scoped-module from "~@scope/module/module.css";\n@value base from "level1.css";\n.a { color: purple; width: 10px; }',
{ importsAsModuleRequests: true },
);
});
test('should replace values within rule selectors', async (t) => {
await run(
t,
'@value selectorValue: .exampleClass;\nselectorValue a { color: purple; }',
'@value selectorValue: .exampleClass;\n.exampleClass a { color: purple; }',
{ replaceInSelectors: true },
);
});
test('variables are also present in messages', async (t) => {
const input = '@value myColor: blue; @value myColor2: myColor';
const processor = postcss([plugin]);
const result = await processor.process(input, { from: undefined });
const { values, type } = result.messages[0];
t.expect(type).toBe('values');
t.expect(values.myColor2).toBe('blue');
});
test('tailwind', async (t) => {
const tailwind = () => ({
postcssPlugin: 'tailwind',
Once(root) {
root.walkAtRules('tailwind', (atRule) => {
atRule.replaceWith(postcss.decl({ prop: '--tw-props', value: ' ' }));
});
},
});
tailwind.postcss = true;
const input = '@tailwind base;';
const processor = postcss([tailwind, plugin]);
const result = await processor.process(input, { from: undefined });
t.expect(result.css).toBe('--tw-props: ;');
t.expect(result.warnings().length).toBe(0);
});