Home / Function/ linkConfigs() — tailwindcss Function Reference

linkConfigs() — tailwindcss Function Reference

Architecture documentation for the linkConfigs() function in link.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  d2577f0e_a6a7_a7ce_6228_851bc0240e63["linkConfigs()"]
  9db14b70_46b7_0974_d9fd_49584e40ff70["link.ts"]
  d2577f0e_a6a7_a7ce_6228_851bc0240e63 -->|defined in| 9db14b70_46b7_0974_d9fd_49584e40ff70
  a81de696_6bb5_40db_26ca_1d707ccebed9["highlight()"]
  d2577f0e_a6a7_a7ce_6228_851bc0240e63 -->|calls| a81de696_6bb5_40db_26ca_1d707ccebed9
  29edd669_83d1_d2aa_eab9_7e8139fc8b20["detectConfigPath()"]
  d2577f0e_a6a7_a7ce_6228_851bc0240e63 -->|calls| 29edd669_83d1_d2aa_eab9_7e8139fc8b20
  5bcf4886_1230_a8ff_7302_a26cc5a9a525["get()"]
  d2577f0e_a6a7_a7ce_6228_851bc0240e63 -->|calls| 5bcf4886_1230_a8ff_7302_a26cc5a9a525
  f1ca9393_701c_e4a7_8303_ed03f028d19b["error()"]
  d2577f0e_a6a7_a7ce_6228_851bc0240e63 -->|calls| f1ca9393_701c_e4a7_8303_ed03f028d19b
  efdaf4fe_1cde_66e0_60e0_5e155e297f6d["relative()"]
  d2577f0e_a6a7_a7ce_6228_851bc0240e63 -->|calls| efdaf4fe_1cde_66e0_60e0_5e155e297f6d
  37b3db30_53ad_adf5_7c3d_6c08e7198514["success()"]
  d2577f0e_a6a7_a7ce_6228_851bc0240e63 -->|calls| 37b3db30_53ad_adf5_7c3d_6c08e7198514
  style d2577f0e_a6a7_a7ce_6228_851bc0240e63 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/@tailwindcss-upgrade/src/codemods/css/link.ts lines 9–122

export async function linkConfigs(
  stylesheets: Stylesheet[],
  { configPath, base }: { configPath: string | null; base: string },
) {
  let rootStylesheets = stylesheets.filter((sheet) => sheet.isTailwindRoot)
  if (rootStylesheets.length === 0) {
    throw new Error(
      `Cannot find any CSS files that reference Tailwind CSS.\nBefore your project can be upgraded you need to create a CSS file that imports Tailwind CSS or uses ${highlight('@tailwind')}.`,
    )
  }
  let withoutAtConfig = rootStylesheets.filter((sheet) => {
    let hasConfig = false
    sheet.root.walkAtRules('config', (node) => {
      let configPath = path.resolve(path.dirname(sheet.file!), node.params.slice(1, -1))
      sheet.linkedConfigPath = configPath
      hasConfig = true
      return false
    })
    return !hasConfig
  })

  // All stylesheets have a `@config` directives
  if (withoutAtConfig.length === 0) return

  // Find the config file path for each stylesheet
  let configPathBySheet = new Map<Stylesheet, string>()
  let sheetByConfigPath = new DefaultMap<string, Set<Stylesheet>>(() => new Set())
  for (let sheet of withoutAtConfig) {
    if (!sheet.file) continue

    let localConfigPath = configPath as string
    if (configPath === null) {
      localConfigPath = await detectConfigPath(path.dirname(sheet.file), base)
    } else if (!path.isAbsolute(localConfigPath)) {
      localConfigPath = path.resolve(base, localConfigPath)
    }

    configPathBySheet.set(sheet, localConfigPath)
    sheetByConfigPath.get(localConfigPath).add(sheet)
  }

  let problematicStylesheets = new Set<Stylesheet>()
  for (let sheets of sheetByConfigPath.values()) {
    if (sheets.size > 1) {
      for (let sheet of sheets) {
        problematicStylesheets.add(sheet)
      }
    }
  }

  // There are multiple "root" files without `@config` directives. Manual
  // intervention is needed to link to the correct Tailwind config files.
  if (problematicStylesheets.size > 1) {
    for (let sheet of problematicStylesheets) {
      error(
        `Could not determine configuration file for: ${highlight(relative(sheet.file!, base))}\nUpdate your stylesheet to use ${highlight('@config')} to specify the correct configuration file explicitly and then run the upgrade tool again.`,
        { prefix: '↳ ' },
      )
    }

    process.exit(1)
  }

  let relativePath = relative
  for (let [sheet, configPath] of configPathBySheet) {
    try {
      if (!sheet || !sheet.file) return
      success(
        `Linked ${highlight(relativePath(configPath, base))} to ${highlight(relativePath(sheet.file, base))}`,
        { prefix: '↳ ' },
      )

      // Link the `@config` directive to the root stylesheets

      // Track the config file path on the stylesheet itself for easy access
      // without traversing the CSS ast and finding the corresponding
      // `@config` later.
      sheet.linkedConfigPath = configPath

      // Create a relative path from the current file to the config file.
      let relative = path.relative(path.dirname(sheet.file), configPath)

Subdomains

Frequently Asked Questions

What does linkConfigs() do?
linkConfigs() is a function in the tailwindcss codebase, defined in packages/@tailwindcss-upgrade/src/codemods/css/link.ts.
Where is linkConfigs() defined?
linkConfigs() is defined in packages/@tailwindcss-upgrade/src/codemods/css/link.ts at line 9.
What does linkConfigs() call?
linkConfigs() calls 6 function(s): detectConfigPath, error, get, highlight, relative, success.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free