Skip to content

Commit 9035f92

Browse files
slorberclaude
andcommitted
fix(css-cascade-layers): let the next plugins process the nodes they insert in the layer
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cae1e58 commit 9035f92

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

‎packages/docusaurus-plugin-css-cascade-layers/src/__tests__/postCssPlugin.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ describe('PostCssPluginWrapInLayer', () => {
3939
flipPlugin,
4040
]).process('a{left:var(--width)}', {from: '/site/file.css'});
4141

42-
// Bug: the inserted fallback isn't flipped
4342
expect(result.css).toMatchInlineSnapshot(
44-
`"@layer myLayer{a{left:1px;right:var(--width)}}"`,
43+
`"@layer myLayer{a{right:1px;right:var(--width)}}"`,
4544
);
4645
});
4746
});

‎packages/docusaurus-plugin-css-cascade-layers/src/postCssPlugin.ts‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66
*/
77

88
import {findLayer} from './layers';
9-
import type {Root, PluginCreator} from 'postcss';
9+
import type {Helpers, Root, PluginCreator} from 'postcss';
1010
import type {PluginOptions} from './options';
1111

12-
function wrapCssRootInLayer(root: Root, layer: string): void {
13-
const rootBefore = root.clone();
14-
root.removeAll();
15-
root.append({
16-
type: 'atrule',
17-
name: 'layer',
18-
params: layer,
19-
nodes: rootBefore.nodes,
20-
});
12+
function wrapCssRootInLayer(
13+
root: Root,
14+
layer: string,
15+
{AtRule}: Helpers,
16+
): void {
17+
const layerAtRule = new AtRule({name: 'layer', params: layer});
18+
layerAtRule.append(root.nodes);
19+
root.append(layerAtRule);
2120
}
2221

2322
export const PostCssPluginWrapInLayer: PluginCreator<{
@@ -29,14 +28,14 @@ export const PostCssPluginWrapInLayer: PluginCreator<{
2928
const layers = Object.entries(options.layers);
3029
return {
3130
postcssPlugin: 'postcss-wrap-in-layer',
32-
Once(root) {
31+
Once(root, helpers) {
3332
const filePath = root.source?.input.file;
3433
if (!filePath) {
3534
return;
3635
}
3736
const layer = findLayer(filePath, layers);
3837
if (layer) {
39-
wrapCssRootInLayer(root, layer);
38+
wrapCssRootInLayer(root, layer, helpers);
4039
}
4140
},
4241
};

0 commit comments

Comments
 (0)