Skip to content

Commit 769891b

Browse files
committed
Merge remote-tracking branch 'origin/main' into claude/native-css-webpack-rspack-2c916c
# Conflicts: # packages/docusaurus-plugin-css-cascade-layers/src/__tests__/postCssPlugin.test.ts # packages/docusaurus-plugin-css-cascade-layers/src/postCssPlugin.ts
2 parents 88ffce8 + 4f26b5a commit 769891b

9 files changed

Lines changed: 70 additions & 57 deletions

File tree

‎argos/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ The workflow execute the 3 main steps below:
2424

2525
### Infima CSS
2626

27-
- Upload the CSS files compiled by [`@docusaurus/infima`](../packages/docusaurus-infima) (LTR and RTL) to diff them on Argos, in a separate `infima` build, using `pnpm argos:upload:infima`
27+
- Upload the CSS files compiled by [`@docusaurus/infima`](../packages/docusaurus-infima) to diff them on Argos, in a separate `infima` build, using `pnpm argos:upload:infima`
2828

2929
## Run manually
3030

‎knip.config.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ const config: KnipConfig = ({
163163
'packages/docusaurus-theme-classic': themePackageConfig({
164164
entry: ['src/nprogress.ts!', 'src/prism-include-languages.ts!'],
165165
ignoreDependencies: [
166-
// Resolved with require.resolve(`@docusaurus/infima/...`)
167-
'@docusaurus/infima',
168166
// Knip doesn't detect imports in "declare module" blocks
169167
'@docusaurus/mdx-loader',
170168
'utility-types',

‎packages/docusaurus-infima/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Don't depend on it directly: it has no stability guarantee, except for the `--if
66

77
## Sources
88

9-
The `styles` folder contains plain CSS. `scripts/build.mjs` only inlines the `@import` rules of `styles/infima.css` into `dist/infima.css`, and flips it with rtlcss into `dist/infima-rtl.css`. The site bundler does the rest: vendor prefixes and fallbacks for the site's target browsers, and minification.
9+
The `styles` folder contains plain CSS. `scripts/build.mjs` only inlines the `@import` rules of `styles/infima.css` into `dist/infima.css`. The site bundler does the rest: vendor prefixes and fallbacks for the site's target browsers, and minification.
1010

1111
The rule order is the cascade order: moving a rule can change which one wins.
1212

‎packages/docusaurus-infima/package.json‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
],
88
"exports": {
99
"./infima.css": "./dist/infima.css",
10-
"./infima-rtl.css": "./dist/infima-rtl.css",
1110
"./package.json": "./package.json"
1211
},
1312
"files": [
@@ -25,9 +24,6 @@
2524
"directory": "packages/docusaurus-infima"
2625
},
2726
"license": "MIT",
28-
"devDependencies": {
29-
"rtlcss": "catalog:"
30-
},
3127
"engines": {
3228
"node": ">=26.13"
3329
}

‎packages/docusaurus-infima/scripts/build.mjs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77

88
import fs from 'node:fs/promises';
99
import path from 'node:path';
10-
import rtlcss from 'rtlcss';
1110

1211
const packageDir = path.dirname(import.meta.dirname);
1312
const entryFile = path.join(packageDir, 'styles/infima.css');
1413
const outputFile = path.join(packageDir, 'dist/infima.css');
15-
const rtlOutputFile = path.join(packageDir, 'dist/infima-rtl.css');
1614

1715
// Matches the imports of the entry file, like @import url('common/base.css');
1816
const ImportRegExp = /^@import url\('(?<importPath>.+)'\);$/gm;
@@ -23,7 +21,6 @@ async function main() {
2321
const css = await inlineImports(entryFile);
2422
await fs.mkdir(path.dirname(outputFile), {recursive: true});
2523
await fs.writeFile(outputFile, css);
26-
await fs.writeFile(rtlOutputFile, rtlcss.process(css));
2724
}
2825

2926
async function inlineImports(file) {

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

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,66 @@
88
import {describe, expect, it} from 'vitest';
99
import postcss from 'postcss';
1010
import {PostCssPluginWrapInLayer} from '../postCssPlugin';
11+
import type {Plugin} from 'postcss';
1112

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+
},
1522
};
1623

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+
};
2433

2534
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+
2660
it('wraps the file in its layer, after the layers declaration', async () => {
2761
await expect(
28-
process('.a { color: red; }', '/site/b.css'),
62+
processCss('.a { color: red; }', '/site/b.css'),
2963
).resolves.toMatchInlineSnapshot(
3064
`"@layer layer.a, layer.b;@layer layer.b {.a { color: red; } }"`,
3165
);
3266
});
3367

3468
it('does not change a file without layer', async () => {
3569
await expect(
36-
process('.a { color: red; }', '/site/c.css'),
70+
processCss('.a { color: red; }', '/site/c.css'),
3771
).resolves.toMatchInlineSnapshot(`".a { color: red; }"`);
3872
});
3973
});

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,28 @@
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

1212
// The layer order is the order of first appearance in the CSS bundle
1313
// Declaring it in each file makes it independent of the bundler CSS order
1414
// See https://github.com/meta/docusaurus/pull/13032
15-
function prependLayersDeclaration(root: Root, layerNames: string[]): void {
16-
root.prepend({type: 'atrule', name: 'layer', params: layerNames.join(', ')});
15+
function prependLayersDeclaration(
16+
root: Root,
17+
layerNames: string[],
18+
{AtRule}: Helpers,
19+
): void {
20+
root.prepend(new AtRule({name: 'layer', params: layerNames.join(', ')}));
1721
}
1822

19-
function wrapCssRootInLayer(root: Root, layer: string): void {
20-
const rootBefore = root.clone();
21-
root.removeAll();
22-
root.append({
23-
type: 'atrule',
24-
name: 'layer',
25-
params: layer,
26-
nodes: rootBefore.nodes,
27-
});
23+
function wrapCssRootInLayer(
24+
root: Root,
25+
layer: string,
26+
{AtRule}: Helpers,
27+
): void {
28+
const layerAtRule = new AtRule({name: 'layer', params: layer});
29+
layerAtRule.append(root.nodes);
30+
root.append(layerAtRule);
2831
}
2932

3033
export const PostCssPluginWrapInLayer: PluginCreator<{
@@ -37,15 +40,15 @@ export const PostCssPluginWrapInLayer: PluginCreator<{
3740
const layerNames = Object.keys(options.layers);
3841
return {
3942
postcssPlugin: 'postcss-wrap-in-layer',
40-
Once(root) {
43+
Once(root, helpers) {
4144
const filePath = root.source?.input.file;
4245
if (!filePath) {
4346
return;
4447
}
4548
const layer = findLayer(filePath, layers);
4649
if (layer) {
47-
wrapCssRootInLayer(root, layer);
48-
prependLayersDeclaration(root, layerNames);
50+
wrapCssRootInLayer(root, layer, helpers);
51+
prependLayersDeclaration(root, layerNames, helpers);
4952
}
5053
},
5154
};

‎packages/docusaurus-theme-classic/src/index.ts‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ import type {ThemeConfig} from '@docusaurus/theme-common';
2020
import type {Plugin as PostCssPlugin} from 'postcss';
2121
import type {PluginOptions} from '@docusaurus/theme-classic';
2222

23-
function getInfimaCSSFile(direction: string) {
24-
return `@docusaurus/infima/infima${direction === 'rtl' ? '-rtl' : ''}.css`;
25-
}
26-
2723
export default function themeClassic(
2824
context: LoadContext,
2925
options: PluginOptions,
@@ -69,7 +65,7 @@ export default function themeClassic(
6965

7066
getClientModules() {
7167
const modules = [
72-
require.resolve(getInfimaCSSFile(direction)),
68+
require.resolve('@docusaurus/infima/infima.css'),
7369
'./prism-include-languages',
7470
'./nprogress',
7571
];
@@ -99,17 +95,10 @@ export default function themeClassic(
9995

10096
configurePostCss(postCssOptions) {
10197
if (direction === 'rtl') {
102-
const resolvedInfimaFile = require.resolve(getInfimaCSSFile(direction));
10398
const plugin: PostCssPlugin = {
10499
postcssPlugin: 'RtlCssPlugin',
105-
prepare: (result) => {
106-
const file = result.root.source?.input.file;
107-
// Skip Infima as we are using the its RTL version.
108-
if (file === resolvedInfimaFile) {
109-
return {};
110-
}
111-
return rtlcss(result.root as unknown as rtlcss.ConfigOptions);
112-
},
100+
// rtlcss() returns a plugin, but its type is Plugin | Processor
101+
prepare: () => rtlcss() as PostCssPlugin,
113102
};
114103
postCssOptions.plugins.push(plugin);
115104
}

‎pnpm-lock.yaml‎

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)