Robin Malfait b94720aef3
Refactor codemod structure (#17484)
This PR is an internal refactor of the codemods package structure that
will make a few follow-up PRs easier.

Essentially what happens is:


1. Moved `./src/template/` into `src/codemods/template/`
2. Moved `./src/codemods` into `./src/codemods/css` (because the CSS
related codemods already)
3. Moved the migration files for the JS config, PostCSS config and
Prettier config into `./src/codemods/config/`.
4. Made filenames with actual migrations consistent by prefixing them
with `migrate-`.
5. Made sure that all the migration functions also use `migrate…`

When looking at this PR, go commit by commit, it will be easier. In a
lot of cases, it's just moving files around but those commits also come
with changes to the code just to update the imports.

[ci-all]
2025-04-01 17:12:22 +02:00

54 lines
1.7 KiB
TypeScript

import { type Plugin, type Root } from 'postcss'
import { resolveConfig } from '../../../../tailwindcss/src/compat/config/resolve-config'
import type { Config } from '../../../../tailwindcss/src/compat/plugin-api'
import { buildMediaQuery } from '../../../../tailwindcss/src/compat/screens-config'
import type { DesignSystem } from '../../../../tailwindcss/src/design-system'
import { DefaultMap } from '../../../../tailwindcss/src/utils/default-map'
export function migrateMediaScreen({
designSystem,
userConfig,
}: {
designSystem?: DesignSystem
userConfig?: Config
} = {}): Plugin {
function migrate(root: Root) {
if (!designSystem || !userConfig) return
let { resolvedConfig } = resolveConfig(designSystem, [
{ base: '', config: userConfig, reference: false },
])
let screens = resolvedConfig?.theme?.screens || {}
let mediaQueries = new DefaultMap<string, string | null>((name) => {
let value = designSystem?.resolveThemeValue(`--breakpoint-${name}`, true) ?? screens?.[name]
if (typeof value === 'string') return `(width >= theme(--breakpoint-${name}))`
return value ? buildMediaQuery(value) : null
})
// First migrate `@screen md` to `@media screen(md)`
root.walkAtRules('screen', (node) => {
node.name = 'media'
node.params = `screen(${node.params})`
})
// Then migrate the `screen(…)` function
root.walkAtRules((rule) => {
if (rule.name !== 'media') return
let screen = rule.params.match(/screen\(([^)]+)\)/)
if (!screen) return
let value = mediaQueries.get(screen[1])
if (!value) return
rule.params = value
})
}
return {
postcssPlugin: '@tailwindcss/upgrade/migrate-media-screen',
OnceExit: migrate,
}
}