|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import {describe, expect, it} from 'vitest'; |
| 9 | +import postcss from 'postcss'; |
| 10 | +import {PostCssPluginWrapInLayer} from '../postCssPlugin'; |
| 11 | +import type {Plugin} from 'postcss'; |
| 12 | + |
| 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 | + }, |
| 22 | +}; |
| 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 | +}; |
| 33 | + |
| 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 | + // Bug: the inserted fallback isn't flipped |
| 43 | + expect(result.css).toMatchInlineSnapshot( |
| 44 | + `"@layer myLayer{a{left:1px;right:var(--width)}}"`, |
| 45 | + ); |
| 46 | + }); |
| 47 | +}); |
0 commit comments