Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,19 @@ export function wordpressThemeJson(config: ThemeJsonConfig = {}): VitePlugin {
// the theme_file_path filter in Sage doesn't point to
// a non-existent file (passes through the base theme.json)

/** CSS-wide keywords that represent resets, not actual values */
const cssWideKeywords = [
'initial',
'inherit',
'unset',
'revert',
'revert-layer',
];

/**
* Helper to extract CSS variables using a regex pattern
* Helper to extract CSS variables using a regex pattern.
* Filters out wildcard namespace resets (e.g. --font-*: initial)
* and CSS-wide keywords.
*/
const extractVariables = (
regex: RegExp,
Expand All @@ -1140,25 +1151,33 @@ export function wordpressThemeJson(config: ThemeJsonConfig = {}): VitePlugin {
while ((match = regex.exec(content)) !== null) {
const [, name, value] = match;

if (name && value) variables.push([name, value.trim()]);
if (
name &&
value &&
!name.includes('*') &&
!cssWideKeywords.includes(value.trim())
)
variables.push([name, value.trim()]);
}

return variables;
};

const patterns = {
COLOR: /--color-([^:]+):\s*([^;}]+)[;}]?/g,
FONT_FAMILY: /--font-([^:]+):\s*([^;}]+)[;}]?/g,
FONT_SIZE: /--text-([^:]+):\s*([^;}]+)[;}]?/g,
BORDER_RADIUS: /--radius-([^:]+):\s*([^;}]+)[;}]?/g,
COLOR: /(?:^|[;{}])\s*--color-([^:]+):\s*([^;}]+)[;}]?/gm,
FONT_FAMILY:
/(?:^|[;{}])\s*--font-([^:]+):\s*([^;}]+)[;}]?/gm,
FONT_SIZE:
/(?:^|[;{}])\s*--text-([^:]+):\s*([^;}]+)[;}]?/gm,
BORDER_RADIUS:
/(?:^|[;{}])\s*--radius-([^:]+):\s*([^;}]+)[;}]?/gm,
} as const;

// Process colors from either @theme block or Tailwind config
const colorEntries = !disableTailwindColors
? [
// Process @theme block colors if available
...extractVariables(patterns.COLOR, themeContent)
.filter(([name]) => !name.endsWith('-*'))
.map(([name, value]) => {
const parts = name.split('-');
const colorName = parts[0];
Expand Down
45 changes: 45 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1752,4 +1752,49 @@ describe('wordpressThemeJson', () => {
expect(slugs.indexOf('lg')).toBeLessThan(slugs.indexOf('relative'));
expect(slugs.indexOf('lg')).toBeLessThan(slugs.indexOf('viewport'));
});

it('should filter out wildcard namespace resets and CSS-wide keywords', () => {
const plugin = wordpressThemeJson({
tailwindConfig: mockTailwindConfigPath,
});

const cssContent = `
@theme {
--font-*: initial;
--font-sans: 'Inter', ui-sans-serif, system-ui, sans-serif;
--color-*: initial;
--color-primary: #000000;
--text-*: inherit;
--text-lg: 1.125rem;
}
`;

(plugin.transform as any)(cssContent, 'app.css');
const emitFile = vi.fn();
(plugin.generateBundle as any).call({ emitFile });

const themeJson = JSON.parse(emitFile.mock.calls[0][0].source);

// Font families should only contain sans, not wildcard
const fontSlugs = themeJson.settings.typography.fontFamilies.map(
(f: { slug: string }) => f.slug
);
expect(fontSlugs).toContain('sans');
expect(fontSlugs).not.toContain('*');
expect(fontSlugs).toHaveLength(1);

// Colors should only contain primary, not wildcard
const colorSlugs = themeJson.settings.color.palette.map(
(c: { slug: string }) => c.slug
);
expect(colorSlugs).toContain('primary');
expect(colorSlugs).toHaveLength(1);

// Font sizes should only contain lg, not wildcard
const sizeSlugs = themeJson.settings.typography.fontSizes.map(
(s: { slug: string }) => s.slug
);
expect(sizeSlugs).toContain('lg');
expect(sizeSlugs).toHaveLength(1);
});
});
Loading