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 c0e6536 commit f13c00d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
20 changes: 15 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,21 @@ test(
import customPlugin from './custom-plugin'
export default {
plugins: [typography, customPlugin],
plugins: [
typography,
customPlugin({
string: 'class',
number: 19,
}),
],
} 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 +187,10 @@ test(
@import 'tailwindcss';
@plugin '@tailwindcss/typography';
@plugin '../custom-plugin';
@plugin '../custom-plugin' {
string: 'class';
number: 19;
}
"
`)

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
15 changes: 14 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,16 @@ 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}: ${typeof value === 'string' ? quoteString(value) : value};\n`
}
css += '}\n'
}
}
if (jsConfigMigration.plugins.length > 0) {
css = css + '\n'
Expand Down Expand Up @@ -149,3 +158,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 | Record<string, string | number> }[]
css: string
}

Expand Down

0 comments on commit f13c00d

Please sign in to comment.