Skip to content

Commit 3ca31c4

Browse files
Migrate static plugins with options to CSS
1 parent aab02ca commit 3ca31c4

File tree

6 files changed

+41
-9
lines changed

6 files changed

+41
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Add first draft of new wide-gamut color palette ([#14693](https://github.com/tailwindlabs/tailwindcss/pull/14693))
1313
- _Upgrade (experimental)_: Migrate `theme(…)` calls to `var(…)` or to the modern `theme(…)` syntax ([#14664](https://github.com/tailwindlabs/tailwindcss/pull/14664), [#14695](https://github.com/tailwindlabs/tailwindcss/pull/14695))
14+
- _Upgrade (experimental)_: Migrate `plugins` with options to CSS ([#14700](https://github.com/tailwindlabs/tailwindcss/pull/14700))
1415

1516
### Fixed
1617

integrations/upgrade/js-config.test.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ test(
106106
107107
--font-family-sans: Inter, system-ui, sans-serif;
108108
--font-family-display: Cabinet Grotesk, ui-sans-serif, system-ui, sans-serif,
109-
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
109+
'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
110110
111111
--radius-4xl: 2rem;
112112
@@ -155,14 +155,21 @@ test(
155155
import customPlugin from './custom-plugin'
156156
157157
export default {
158-
plugins: [typography, customPlugin],
158+
plugins: [
159+
typography,
160+
customPlugin({
161+
string: 'class',
162+
number: 19,
163+
}),
164+
],
159165
} satisfies Config
160166
`,
161167
'custom-plugin.js': ts`
162-
export default function ({ addVariant }) {
168+
import plugin from 'tailwindcss/plugin'
169+
export default plugin.withOptions((_options) => ({ addVariant }) => {
163170
addVariant('inverted', '@media (inverted-colors: inverted)')
164171
addVariant('hocus', ['&:focus', '&:hover'])
165-
}
172+
})
166173
`,
167174
'src/input.css': css`
168175
@tailwind base;
@@ -180,7 +187,10 @@ test(
180187
@import 'tailwindcss';
181188
182189
@plugin '@tailwindcss/typography';
183-
@plugin '../custom-plugin';
190+
@plugin '../custom-plugin' {
191+
string: 'class';
192+
number: 19;
193+
}
184194
"
185195
`)
186196

packages/@tailwindcss-upgrade/src/codemods/format-nodes.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ export function formatNodes(): Plugin {
1818
// Format the nodes
1919
await Promise.all(
2020
nodesToFormat.map(async (node) => {
21-
node.replaceWith(parse(await format(node.toString(), { parser: 'css', semi: true })))
21+
node.replaceWith(
22+
parse(
23+
await format(node.toString(), {
24+
parser: 'css',
25+
semi: true,
26+
singleQuote: true,
27+
}),
28+
),
29+
)
2230
}),
2331
)
2432
}

packages/@tailwindcss-upgrade/src/codemods/migrate-at-layer-utilities.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ it('should migrate classes with attribute selectors', async () => {
413413
`),
414414
).toMatchInlineSnapshot(`
415415
"@utility no-scrollbar {
416-
&[data-checked=""] {
416+
&[data-checked=''] {
417417
display: none;
418418
}
419419
}"

packages/@tailwindcss-upgrade/src/codemods/migrate-config.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,16 @@ export function migrateConfig(
6363
plugin.path[0] === '.'
6464
? relativeToStylesheet(sheet, path.resolve(plugin.base, plugin.path))
6565
: plugin.path
66-
css += `@plugin '${relative}';\n`
66+
67+
if (plugin.options === null) {
68+
css += `@plugin '${relative}';\n`
69+
} else {
70+
css += `@plugin '${relative}' {\n`
71+
for (let [property, value] of Object.entries(plugin.options)) {
72+
css += ` ${property}: ${typeof value === 'string' ? quoteString(value) : value};\n`
73+
}
74+
css += '}\n'
75+
}
6776
}
6877
if (jsConfigMigration.plugins.length > 0) {
6978
css = css + '\n'
@@ -149,3 +158,7 @@ function relativeToStylesheet(sheet: Stylesheet, absolute: string) {
149158
// glob.
150159
return normalizePath(relative)
151160
}
161+
162+
function quoteString(value: string): string {
163+
return `'${value.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`
164+
}

packages/@tailwindcss-upgrade/src/migrate-js-config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type JSConfigMigration =
2424
// Could not convert the config file, need to inject it as-is in a @config directive
2525
null | {
2626
sources: { base: string; pattern: string }[]
27-
plugins: { base: string; path: string }[]
27+
plugins: { base: string; path: string; options: null | Record<string, string | number> }[]
2828
css: string
2929
}
3030

0 commit comments

Comments
 (0)