Home / Function/ findRoots() — tailwindcss Function Reference

findRoots() — tailwindcss Function Reference

Architecture documentation for the findRoots() function in candidate.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  12046d5a_fe45_6a31_232f_f0f54b577fe6["findRoots()"]
  53cf41fe_5903_d247_3bb3_38414ba7d631["parseCandidate()"]
  53cf41fe_5903_d247_3bb3_38414ba7d631 -->|calls| 12046d5a_fe45_6a31_232f_f0f54b577fe6
  ca76ae68_c9c0_d977_a6d8_8ba86685bf25["parseVariant()"]
  ca76ae68_c9c0_d977_a6d8_8ba86685bf25 -->|calls| 12046d5a_fe45_6a31_232f_f0f54b577fe6
  style 12046d5a_fe45_6a31_232f_f0f54b577fe6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/candidate.ts lines 861–905

function* findRoots(input: string, exists: (input: string) => boolean): Iterable<Root> {
  // If there is an exact match, then that's the root.
  if (exists(input)) {
    yield [input, null]
  }

  // Otherwise test every permutation of the input by iteratively removing
  // everything after the last dash.
  let idx = input.lastIndexOf('-')

  // Determine the root and value by testing permutations of the incoming input.
  //
  // In case of a candidate like `bg-red-500`, this looks like:
  //
  // `bg-red-500` -> No match
  // `bg-red`     -> No match
  // `bg`         -> Match
  while (idx > 0) {
    let maybeRoot = input.slice(0, idx)

    if (exists(maybeRoot)) {
      let root: Root = [maybeRoot, input.slice(idx + 1)]

      // If the leftover value is an empty string, it means that the value is an
      // invalid named value, e.g.: `bg-`. This makes the candidate invalid and we
      // can skip any further parsing.
      if (root[1] === '') break

      // Edge case: `@-…` is not valid as a variant or a utility so we want to
      // skip if an `@` is followed by a `-`. Otherwise `@-2xl:flex` and
      // `@-2xl:flex` would be considered the same.
      if (root[0] === '@' && exists('@') && input[idx] === '-') break

      yield root
    }

    idx = input.lastIndexOf('-', idx - 1)
  }

  // Try '@' variant after permutations. This allows things like `@max` of `@max-foo-bar`
  // to match before looking for `@`.
  if (input[0] === '@' && exists('@')) {
    yield ['@', input.slice(1)]
  }
}

Subdomains

Frequently Asked Questions

What does findRoots() do?
findRoots() is a function in the tailwindcss codebase.
What calls findRoots()?
findRoots() is called by 2 function(s): parseCandidate, parseVariant.

Analyze Your Own Codebase

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

Try Supermodel Free