migrateTailwindDirectives() — tailwindcss Function Reference
Architecture documentation for the migrateTailwindDirectives() function in migrate-tailwind-directives.ts from the tailwindcss codebase.
Entity Profile
Dependency Diagram
graph TD 2aaf5161_ac26_f74e_9d1a_9d3e64f884b9["migrateTailwindDirectives()"] b614008a_e4aa_722c_910a_3c58e4d28cef["migrate()"] b614008a_e4aa_722c_910a_3c58e4d28cef -->|calls| 2aaf5161_ac26_f74e_9d1a_9d3e64f884b9 b1aa79a5_bad0_a9e2_1c70_8e14489ee8dd["migrateContents()"] b1aa79a5_bad0_a9e2_1c70_8e14489ee8dd -->|calls| 2aaf5161_ac26_f74e_9d1a_9d3e64f884b9 6d984fc2_1a6f_9618_c0a8_29a957c675ab["findTargetNode()"] 2aaf5161_ac26_f74e_9d1a_9d3e64f884b9 -->|calls| 6d984fc2_1a6f_9618_c0a8_29a957c675ab style 2aaf5161_ac26_f74e_9d1a_9d3e64f884b9 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
packages/@tailwindcss-upgrade/src/codemods/css/migrate-tailwind-directives.ts lines 5–128
export function migrateTailwindDirectives(options: { newPrefix: string | null }): Plugin {
let prefixParams = options.newPrefix ? ` prefix(${options.newPrefix})` : ''
function migrate(root: Root) {
let baseNode = null as AtRule | null
let utilitiesNode = null as AtRule | null
let orderedNodes: AtRule[] = []
let defaultImportNode = null as AtRule | null
let utilitiesImportNode = null as AtRule | null
let preflightImportNode = null as AtRule | null
let themeImportNode = null as AtRule | null
let layerOrder: string[] = []
root.walkAtRules((node) => {
// Migrate legacy `@import "tailwindcss/tailwind.css"`
if (node.name === 'import' && node.params.match(/^["']tailwindcss\/tailwind\.css["']$/)) {
node.params = node.params.replace('tailwindcss/tailwind.css', 'tailwindcss')
}
// Append any new prefix() param to existing `@import 'tailwindcss'` directives
if (node.name === 'import' && node.params.match(/^["']tailwindcss["']/)) {
node.params += prefixParams
}
// Track old imports and directives
else if (
(node.name === 'tailwind' && node.params === 'base') ||
(node.name === 'import' && node.params.match(/^["']tailwindcss\/base["']$/))
) {
layerOrder.push('base')
orderedNodes.push(node)
baseNode = node
} else if (
(node.name === 'tailwind' && node.params === 'utilities') ||
(node.name === 'import' && node.params.match(/^["']tailwindcss\/utilities["']$/))
) {
layerOrder.push('utilities')
orderedNodes.push(node)
utilitiesNode = node
}
// Remove directives that are not needed anymore
else if (
(node.name === 'tailwind' && node.params === 'components') ||
(node.name === 'tailwind' && node.params === 'screens') ||
(node.name === 'tailwind' && node.params === 'variants') ||
(node.name === 'import' && node.params.match(/^["']tailwindcss\/components["']$/))
) {
node.remove()
}
// Replace Tailwind CSS v2 directives that still worked in v3.
else if (node.name === 'responsive') {
if (node.nodes) {
for (let child of node.nodes) {
child.raws.tailwind_pretty = true
}
node.replaceWith(node.nodes)
} else {
node.remove()
}
}
})
// Insert default import if all directives are present
if (baseNode !== null && utilitiesNode !== null) {
if (!defaultImportNode) {
findTargetNode(orderedNodes).before(
new AtRule({ name: 'import', params: `'tailwindcss'${prefixParams}` }),
)
}
baseNode?.remove()
utilitiesNode?.remove()
}
// Insert individual imports if not all directives are present
else if (utilitiesNode !== null) {
if (!utilitiesImportNode) {
findTargetNode(orderedNodes).before(
new AtRule({ name: 'import', params: "'tailwindcss/utilities' layer(utilities)" }),
)
}
utilitiesNode?.remove()
} else if (baseNode !== null) {
if (!themeImportNode) {
findTargetNode(orderedNodes).before(
new AtRule({ name: 'import', params: `'tailwindcss/theme' layer(theme)${prefixParams}` }),
)
}
if (!preflightImportNode) {
findTargetNode(orderedNodes).before(
new AtRule({ name: 'import', params: "'tailwindcss/preflight' layer(base)" }),
)
}
baseNode?.remove()
}
// Insert `@layer …;` at the top when the order in the CSS was different
// from the default.
{
// Determine if the order is different from the default.
let sortedLayerOrder = layerOrder.toSorted((a, z) => {
return DEFAULT_LAYER_ORDER.indexOf(a) - DEFAULT_LAYER_ORDER.indexOf(z)
})
if (layerOrder.some((layer, index) => layer !== sortedLayerOrder[index])) {
// Create a new `@layer` rule with the sorted order.
let newLayerOrder = DEFAULT_LAYER_ORDER.toSorted((a, z) => {
return layerOrder.indexOf(a) - layerOrder.indexOf(z)
})
root.prepend({ name: 'layer', params: newLayerOrder.join(', ') })
}
}
}
return {
postcssPlugin: '@tailwindcss/upgrade/migrate-tailwind-directives',
OnceExit: migrate,
}
}
Domain
Subdomains
Calls
Called By
Source
Frequently Asked Questions
What does migrateTailwindDirectives() do?
migrateTailwindDirectives() is a function in the tailwindcss codebase.
What does migrateTailwindDirectives() call?
migrateTailwindDirectives() calls 1 function(s): findTargetNode.
What calls migrateTailwindDirectives()?
migrateTailwindDirectives() is called by 2 function(s): migrate, migrateContents.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free