|
8 | 8 | import {describe, expect, it} from 'vitest'; |
9 | 9 | import postcss from 'postcss'; |
10 | 10 | import {PostCssPluginWrapInLayer} from '../postCssPlugin'; |
| 11 | +import type {Plugin} from 'postcss'; |
11 | 12 |
|
12 | | -const layers = { |
13 | | - 'layer.a': (filePath: string) => filePath.endsWith('a.css'), |
14 | | - 'layer.b': (filePath: string) => filePath.endsWith('b.css'), |
| 13 | +// Like postcss-preset-env, inserts a fallback before var() declarations |
| 14 | +const fallbackPlugin: Plugin = { |
| 15 | + postcssPlugin: 'fallback', |
| 16 | + Declaration(decl) { |
| 17 | + const hasFallback = decl.prev()?.type === 'decl'; |
| 18 | + if (decl.value === 'var(--width)' && !hasFallback) { |
| 19 | + decl.cloneBefore({value: '1px'}); |
| 20 | + } |
| 21 | + }, |
15 | 22 | }; |
16 | 23 |
|
17 | | -async function process(css: string, filePath: string): Promise<string> { |
18 | | - const result = await postcss([PostCssPluginWrapInLayer({layers})]).process( |
19 | | - css, |
20 | | - {from: filePath}, |
21 | | - ); |
22 | | - return result.css; |
23 | | -} |
| 24 | +// Like rtlcss, flips the declarations |
| 25 | +const flipPlugin: Plugin = { |
| 26 | + postcssPlugin: 'flip', |
| 27 | + Declaration(decl) { |
| 28 | + if (decl.prop === 'left') { |
| 29 | + decl.prop = 'right'; |
| 30 | + } |
| 31 | + }, |
| 32 | +}; |
24 | 33 |
|
25 | 34 | describe('PostCssPluginWrapInLayer', () => { |
| 35 | + it('lets the next plugins process the nodes they insert', async () => { |
| 36 | + const result = await postcss([ |
| 37 | + PostCssPluginWrapInLayer({layers: {myLayer: () => true}}), |
| 38 | + fallbackPlugin, |
| 39 | + flipPlugin, |
| 40 | + ]).process('a{left:var(--width)}', {from: '/site/file.css'}); |
| 41 | + |
| 42 | + expect(result.css).toMatchInlineSnapshot( |
| 43 | + `"@layer myLayer;@layer myLayer{a{right:1px;right:var(--width)}}"`, |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + const layers = { |
| 48 | + 'layer.a': (filePath: string) => filePath.endsWith('a.css'), |
| 49 | + 'layer.b': (filePath: string) => filePath.endsWith('b.css'), |
| 50 | + }; |
| 51 | + |
| 52 | + async function processCss(css: string, filePath: string): Promise<string> { |
| 53 | + const result = await postcss([PostCssPluginWrapInLayer({layers})]).process( |
| 54 | + css, |
| 55 | + {from: filePath}, |
| 56 | + ); |
| 57 | + return result.css; |
| 58 | + } |
| 59 | + |
26 | 60 | it('wraps the file in its layer, after the layers declaration', async () => { |
27 | 61 | await expect( |
28 | | - process('.a { color: red; }', '/site/b.css'), |
| 62 | + processCss('.a { color: red; }', '/site/b.css'), |
29 | 63 | ).resolves.toMatchInlineSnapshot( |
30 | 64 | `"@layer layer.a, layer.b;@layer layer.b {.a { color: red; } }"`, |
31 | 65 | ); |
32 | 66 | }); |
33 | 67 |
|
34 | 68 | it('does not change a file without layer', async () => { |
35 | 69 | await expect( |
36 | | - process('.a { color: red; }', '/site/c.css'), |
| 70 | + processCss('.a { color: red; }', '/site/c.css'), |
37 | 71 | ).resolves.toMatchInlineSnapshot(`".a { color: red; }"`); |
38 | 72 | }); |
39 | 73 | }); |
0 commit comments