Skip to content

Commit

Permalink
Migrate static plugins with options to CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
philipp-spiess committed Oct 17, 2024
1 parent 3c0528a commit ab2514d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add first draft of new wide-gamut color palette ([#14693](https://github.com/tailwindlabs/tailwindcss/pull/14693))
- _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))
- _Upgrade (experimental)_: Migrate `plugins` with options to CSS ([#14700](https://github.com/tailwindlabs/tailwindcss/pull/14700))

### Fixed

Expand Down
44 changes: 39 additions & 5 deletions integrations/upgrade/js-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test(
--font-family-sans: Inter, system-ui, sans-serif;
--font-family-display: Cabinet Grotesk, ui-sans-serif, system-ui, sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
--radius-4xl: 2rem;
Expand Down Expand Up @@ -155,14 +155,33 @@ test(
import customPlugin from './custom-plugin'
export default {
plugins: [typography, customPlugin],
plugins: [
typography,
customPlugin({
'is-null': null,
'is-true': true,
'is-false': false,
'is-int': 1234567,
'is-float': 1.35,
'is-sci': 1.35e-5,
'is-str-null': 'null',
'is-str-true': 'true',
'is-str-false': 'false',
'is-str-int': '1234567',
'is-str-float': '1.35',
'is-str-sci': '1.35e-5',
'is-arr': ['foo', 'bar'],
'is-arr-mixed': [null, true, false, 1234567, 1.35, 'foo', 'bar', 'true'],
}),
],
} satisfies Config
`,
'custom-plugin.js': ts`
export default function ({ addVariant }) {
import plugin from 'tailwindcss/plugin'
export default plugin.withOptions((_options) => ({ addVariant }) => {
addVariant('inverted', '@media (inverted-colors: inverted)')
addVariant('hocus', ['&:focus', '&:hover'])
}
})
`,
'src/input.css': css`
@tailwind base;
Expand All @@ -180,7 +199,22 @@ test(
@import 'tailwindcss';
@plugin '@tailwindcss/typography';
@plugin '../custom-plugin';
@plugin '../custom-plugin' {
is-null: null;
is-true: true;
is-false: false;
is-int: 1234567;
is-float: 1.35;
is-sci: 0.0000135;
is-str-null: 'null';
is-str-true: 'true';
is-str-false: 'false';
is-str-int: '1234567';
is-str-float: '1.35';
is-str-sci: '1.35e-5';
is-arr: 'foo', 'bar';
is-arr-mixed: null, true, false, 1234567, 1.35, 'foo', 'bar', 'true';
}
"
`)

Expand Down
10 changes: 9 additions & 1 deletion packages/@tailwindcss-upgrade/src/codemods/format-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ export function formatNodes(): Plugin {
// Format the nodes
await Promise.all(
nodesToFormat.map(async (node) => {
node.replaceWith(parse(await format(node.toString(), { parser: 'css', semi: true })))
node.replaceWith(
parse(
await format(node.toString(), {
parser: 'css',
semi: true,
singleQuote: true,
}),
),
)
}),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ it('should migrate classes with attribute selectors', async () => {
`),
).toMatchInlineSnapshot(`
"@utility no-scrollbar {
&[data-checked=""] {
&[data-checked=''] {
display: none;
}
}"
Expand Down
21 changes: 20 additions & 1 deletion packages/@tailwindcss-upgrade/src/codemods/migrate-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,22 @@ export function migrateConfig(
plugin.path[0] === '.'
? relativeToStylesheet(sheet, path.resolve(plugin.base, plugin.path))
: plugin.path
css += `@plugin '${relative}';\n`

if (plugin.options === null) {
css += `@plugin '${relative}';\n`
} else {
css += `@plugin '${relative}' {\n`
for (let [property, value] of Object.entries(plugin.options)) {
css += ` ${property}: ${
Array.isArray(value)
? value.map((v) => (typeof v === 'string' ? quoteString(v) : '' + v)).join(', ')
: typeof value === 'string'
? quoteString(value)
: '' + value
};\n`
}
css += '}\n'
}
}
if (jsConfigMigration.plugins.length > 0) {
css = css + '\n'
Expand Down Expand Up @@ -149,3 +164,7 @@ function relativeToStylesheet(sheet: Stylesheet, absolute: string) {
// glob.
return normalizePath(relative)
}

function quoteString(value: string): string {
return `'${value.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`
}
2 changes: 1 addition & 1 deletion packages/@tailwindcss-upgrade/src/migrate-js-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type JSConfigMigration =
// Could not convert the config file, need to inject it as-is in a @config directive
null | {
sources: { base: string; pattern: string }[]
plugins: { base: string; path: string }[]
plugins: { base: string; path: string; options: null | StaticPluginOptions }[]
css: string
}

Expand Down

0 comments on commit ab2514d

Please sign in to comment.