Home / Function/ migratePreflight() — tailwindcss Function Reference

migratePreflight() — tailwindcss Function Reference

Architecture documentation for the migratePreflight() function in migrate-preflight.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  0838eb2e_5580_246c_b5a8_b68fb91edccc["migratePreflight()"]
  f67a6019_88a0_ffd1_f91c_1a51645f6931["migrate-preflight.ts"]
  0838eb2e_5580_246c_b5a8_b68fb91edccc -->|defined in| f67a6019_88a0_ffd1_f91c_1a51645f6931
  8116723c_3519_f19a_98fa_20694cdfd6fa["migrate()"]
  8116723c_3519_f19a_98fa_20694cdfd6fa -->|calls| 0838eb2e_5580_246c_b5a8_b68fb91edccc
  1be562b7_fe23_7e22_03ec_31b3d101e5e5["migrateContents()"]
  1be562b7_fe23_7e22_03ec_31b3d101e5e5 -->|calls| 0838eb2e_5580_246c_b5a8_b68fb91edccc
  1767219e_c38a_227c_492b_5d47634d54a4["keyPathToCssProperty()"]
  0838eb2e_5580_246c_b5a8_b68fb91edccc -->|calls| 1767219e_c38a_227c_492b_5d47634d54a4
  05d44181_6631_c1fa_4a63_96653e28f99c["toKeyPath()"]
  0838eb2e_5580_246c_b5a8_b68fb91edccc -->|calls| 05d44181_6631_c1fa_4a63_96653e28f99c
  6cf757d4_aaad_b18d_cf21_ade24c6e2a77["isMajor()"]
  0838eb2e_5580_246c_b5a8_b68fb91edccc -->|calls| 6cf757d4_aaad_b18d_cf21_ade24c6e2a77
  49a8c506_c50e_ed4b_5a0e_0393edae2b6f["parse()"]
  0838eb2e_5580_246c_b5a8_b68fb91edccc -->|calls| 49a8c506_c50e_ed4b_5a0e_0393edae2b6f
  07598cf4_0f8c_95f8_901c_b2052b7d14f6["substituteFunctionsInValue()"]
  0838eb2e_5580_246c_b5a8_b68fb91edccc -->|calls| 07598cf4_0f8c_95f8_901c_b2052b7d14f6
  style 0838eb2e_5580_246c_b5a8_b68fb91edccc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/@tailwindcss-upgrade/src/codemods/css/migrate-preflight.ts lines 35–115

export function migratePreflight({
  designSystem,
  userConfig,
}: {
  designSystem: DesignSystem | null
  userConfig?: Config | null
}): Plugin {
  // @ts-expect-error
  let defaultBorderColor = userConfig?.theme?.borderColor?.DEFAULT

  function canResolveThemeValue(path: string) {
    if (!designSystem) return false
    let variable = `--${keyPathToCssProperty(toKeyPath(path))}` as const
    return Boolean(designSystem.theme.get([variable]))
  }

  function migrate(root: Root) {
    // CSS for backwards compatibility with v3 should only injected in v3
    // projects and not v4 projects.
    if (!version.isMajor(3)) return

    let isTailwindRoot = false
    root.walkAtRules('import', (node) => {
      if (
        /['"]tailwindcss['"]/.test(node.params) ||
        /['"]tailwindcss\/preflight['"]/.test(node.params)
      ) {
        isTailwindRoot = true
        return false
      }
    })

    if (!isTailwindRoot) return

    // Figure out the compatibility CSS to inject
    let compatibilityCssString = ''
    if (defaultBorderColor !== DEFAULT_BORDER_COLOR) {
      compatibilityCssString += BORDER_COLOR_COMPATIBILITY_CSS
      compatibilityCssString += '\n\n'
    }

    compatibilityCssString = `\n@tw-bucket compatibility {\n${compatibilityCssString}\n}\n`
    let compatibilityCss = postcss.parse(compatibilityCssString)

    // Replace the `theme(…)` with v3 values if we can't resolve the theme
    // value.
    compatibilityCss.walkDecls((decl) => {
      if (decl.value.includes('theme(')) {
        decl.value = substituteFunctionsInValue(ValueParser.parse(decl.value), (path) => {
          if (canResolveThemeValue(path)) {
            return defaultBorderColor
          } else {
            if (path === 'borderColor.DEFAULT') {
              return 'var(--color-gray-200, currentcolor)'
            }
          }
          return null
        })
      }
    })

    // Cleanup `--border-color` definition in `theme(…)`
    root.walkAtRules('theme', (node) => {
      node.walkDecls('--border-color', (decl) => {
        decl.remove()
      })

      if (node.nodes?.length === 0) {
        node.remove()
      }
    })

    // Inject the compatibility CSS
    root.append(compatibilityCss)
  }

  return {
    postcssPlugin: '@tailwindcss/upgrade/migrate-preflight',
    OnceExit: migrate,
  }
}

Subdomains

Frequently Asked Questions

What does migratePreflight() do?
migratePreflight() is a function in the tailwindcss codebase, defined in packages/@tailwindcss-upgrade/src/codemods/css/migrate-preflight.ts.
Where is migratePreflight() defined?
migratePreflight() is defined in packages/@tailwindcss-upgrade/src/codemods/css/migrate-preflight.ts at line 35.
What does migratePreflight() call?
migratePreflight() calls 5 function(s): isMajor, keyPathToCssProperty, parse, substituteFunctionsInValue, toKeyPath.
What calls migratePreflight()?
migratePreflight() 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