Skip to content

Commit 88c8906

Browse files
siriwatknpRobinMalfaitphilipp-spiess
authored
Prevent modifying CSS variables in plugins (#16103)
closes #16100 --------- Co-authored-by: Robin Malfait <[email protected]> Co-authored-by: Philipp Spiess <[email protected]>
1 parent d85e9cf commit 88c8906

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Refactor gradient implementation to work around [prettier/prettier#17058](https://github.com/prettier/prettier/issues/17058) ([#16072](https://github.com/tailwindlabs/tailwindcss/pull/16072))
1515
- Vite: Ensure hot-reloading works with SolidStart setups ([#16052](https://github.com/tailwindlabs/tailwindcss/pull/16052))
1616
- Vite: Fix a crash when starting the development server in SolidStart setups ([#16052](https://github.com/tailwindlabs/tailwindcss/pull/16052))
17+
- Prevent camelCasing CSS custom properties added by JavaScript plugins ([#16103](https://github.com/tailwindlabs/tailwindcss/pull/16103))
1718

1819
## [4.0.1] - 2025-01-29
1920

packages/tailwindcss/src/compat/plugin-api.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,38 @@ describe('addBase', () => {
15341534
"
15351535
`)
15361536
})
1537+
1538+
test('does not modify CSS variables', async () => {
1539+
let input = css`
1540+
@plugin "my-plugin";
1541+
`
1542+
1543+
let compiler = await compile(input, {
1544+
loadModule: async () => ({
1545+
module: plugin(function ({ addBase }) {
1546+
addBase({
1547+
':root': {
1548+
'--PascalCase': '1',
1549+
'--camelCase': '1',
1550+
'--UPPERCASE': '1',
1551+
},
1552+
})
1553+
}),
1554+
base: '/root',
1555+
}),
1556+
})
1557+
1558+
expect(compiler.build([])).toMatchInlineSnapshot(`
1559+
"@layer base {
1560+
:root {
1561+
--PascalCase: 1;
1562+
--camelCase: 1;
1563+
--UPPERCASE: 1;
1564+
}
1565+
}
1566+
"
1567+
`)
1568+
})
15371569
})
15381570

15391571
describe('addVariant', () => {

packages/tailwindcss/src/compat/plugin-api.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,15 +499,18 @@ export function objectToAst(rules: CssInJs | CssInJs[]): AstNode[] {
499499

500500
for (let [name, value] of entries) {
501501
if (typeof value !== 'object') {
502-
if (!name.startsWith('--') && value === '@slot') {
503-
ast.push(rule(name, [atRule('@slot')]))
504-
} else {
502+
if (!name.startsWith('--')) {
503+
if (value === '@slot') {
504+
ast.push(rule(name, [atRule('@slot')]))
505+
continue
506+
}
507+
505508
// Convert camelCase to kebab-case:
506509
// https://github.com/postcss/postcss-js/blob/b3db658b932b42f6ac14ca0b1d50f50c4569805b/parser.js#L30-L35
507510
name = name.replace(/([A-Z])/g, '-$1').toLowerCase()
508-
509-
ast.push(decl(name, String(value)))
510511
}
512+
513+
ast.push(decl(name, String(value)))
511514
} else if (Array.isArray(value)) {
512515
for (let item of value) {
513516
if (typeof item === 'string') {

0 commit comments

Comments
 (0)