Skip to content

Commit 3da49f9

Browse files
Migrate static plugins with options to CSS (#14700)
This PR extends our JS configuration to CSS migration by also allowing `plugins` with options. An example of such config would be: ```js import { type Config } from 'tailwindcss' import myPlugin from "./myPlugin"; export default { plugins: [ myPlugin({ class: "tw", }), ], } satisfies Config; ``` If the option object contains only values allowed in our CSS API, we can convert this to CSS entirely: ```css @plugin './myPlugin' { class: 'tw'; } ```
1 parent 3e7695f commit 3da49f9

File tree

6 files changed

+78
-10
lines changed

6 files changed

+78
-10
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Added
11+
12+
- _Upgrade (experimental)_: Migrate `plugins` with options to CSS ([#14700](https://github.com/tailwindlabs/tailwindcss/pull/14700))
1113

1214
## [4.0.0-alpha.28] - 2024-10-17
1315

integrations/upgrade/js-config.test.ts

+39-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,33 @@ test(
155155
import customPlugin from './custom-plugin'
156156
157157
export default {
158-
plugins: [typography, customPlugin],
158+
plugins: [
159+
typography,
160+
customPlugin({
161+
'is-null': null,
162+
'is-true': true,
163+
'is-false': false,
164+
'is-int': 1234567,
165+
'is-float': 1.35,
166+
'is-sci': 1.35e-5,
167+
'is-str-null': 'null',
168+
'is-str-true': 'true',
169+
'is-str-false': 'false',
170+
'is-str-int': '1234567',
171+
'is-str-float': '1.35',
172+
'is-str-sci': '1.35e-5',
173+
'is-arr': ['foo', 'bar'],
174+
'is-arr-mixed': [null, true, false, 1234567, 1.35, 'foo', 'bar', 'true'],
175+
}),
176+
],
159177
} satisfies Config
160178
`,
161179
'custom-plugin.js': ts`
162-
export default function ({ addVariant }) {
180+
import plugin from 'tailwindcss/plugin'
181+
export default plugin.withOptions((_options) => ({ addVariant }) => {
163182
addVariant('inverted', '@media (inverted-colors: inverted)')
164183
addVariant('hocus', ['&:focus', '&:hover'])
165-
}
184+
})
166185
`,
167186
'src/input.css': css`
168187
@tailwind base;
@@ -180,7 +199,22 @@ test(
180199
@import 'tailwindcss';
181200
182201
@plugin '@tailwindcss/typography';
183-
@plugin '../custom-plugin';
202+
@plugin '../custom-plugin' {
203+
is-null: null;
204+
is-true: true;
205+
is-false: false;
206+
is-int: 1234567;
207+
is-float: 1.35;
208+
is-sci: 0.0000135;
209+
is-str-null: 'null';
210+
is-str-true: 'true';
211+
is-str-false: 'false';
212+
is-str-int: '1234567';
213+
is-str-float: '1.35';
214+
is-str-sci: '1.35e-5';
215+
is-arr: 'foo', 'bar';
216+
is-arr-mixed: null, true, false, 1234567, 1.35, 'foo', 'bar', 'true';
217+
}
184218
"
185219
`)
186220

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

+25-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,27 @@ 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+
let cssValue = ''
73+
if (typeof value === 'string') {
74+
cssValue = quoteString(value)
75+
} else if (Array.isArray(value)) {
76+
cssValue = value
77+
.map((v) => (typeof v === 'string' ? quoteString(v) : '' + v))
78+
.join(', ')
79+
} else {
80+
cssValue = '' + value
81+
}
82+
83+
css += ` ${property}: ${cssValue};\n`
84+
}
85+
css += '}\n'
86+
}
6787
}
6888
if (jsConfigMigration.plugins.length > 0) {
6989
css = css + '\n'
@@ -149,3 +169,7 @@ function relativeToStylesheet(sheet: Stylesheet, absolute: string) {
149169
// glob.
150170
return normalizePath(relative)
151171
}
172+
173+
function quoteString(value: string): string {
174+
return `'${value.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`
175+
}

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 | StaticPluginOptions }[]
2828
css: string
2929
}
3030

0 commit comments

Comments
 (0)