Home / Function/ buildDesignSystem() — tailwindcss Function Reference

buildDesignSystem() — tailwindcss Function Reference

Architecture documentation for the buildDesignSystem() function in design-system.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  cebe77e1_f0f2_aeee_417e_2192f5790344["buildDesignSystem()"]
  61c2cd47_b3e6_7a1a_bde9_eafda1b00be1["run()"]
  61c2cd47_b3e6_7a1a_bde9_eafda1b00be1 -->|calls| cebe77e1_f0f2_aeee_417e_2192f5790344
  26086ff1_0d4f_fdb2_3fc4_d0c999f90a8c["parseCss()"]
  26086ff1_0d4f_fdb2_3fc4_d0c999f90a8c -->|calls| cebe77e1_f0f2_aeee_417e_2192f5790344
  737df10e_833f_d962_fea9_0e7dadf43c40["loadDesignSystem()"]
  737df10e_833f_d962_fea9_0e7dadf43c40 -->|calls| cebe77e1_f0f2_aeee_417e_2192f5790344
  bc446739_2392_65cc_e41e_055960e2ef29["simpleDesign()"]
  bc446739_2392_65cc_e41e_055960e2ef29 -->|calls| cebe77e1_f0f2_aeee_417e_2192f5790344
  ba6df4b8_e7cd_66be_ac5e_1d70e8699df2["createUtilities()"]
  cebe77e1_f0f2_aeee_417e_2192f5790344 -->|calls| ba6df4b8_e7cd_66be_ac5e_1d70e8699df2
  7c96535c_85cb_d6bf_efe4_875fba595c4f["createVariants()"]
  cebe77e1_f0f2_aeee_417e_2192f5790344 -->|calls| 7c96535c_85cb_d6bf_efe4_875fba595c4f
  ca76ae68_c9c0_d977_a6d8_8ba86685bf25["parseVariant()"]
  cebe77e1_f0f2_aeee_417e_2192f5790344 -->|calls| ca76ae68_c9c0_d977_a6d8_8ba86685bf25
  53cf41fe_5903_d247_3bb3_38414ba7d631["parseCandidate()"]
  cebe77e1_f0f2_aeee_417e_2192f5790344 -->|calls| 53cf41fe_5903_d247_3bb3_38414ba7d631
  3ea56425_6e9f_0dc3_e68f_304ee638d53d["compileAstNodes()"]
  cebe77e1_f0f2_aeee_417e_2192f5790344 -->|calls| 3ea56425_6e9f_0dc3_e68f_304ee638d53d
  552e707a_ccba_b2a6_5917_15f7a417896b["theme()"]
  cebe77e1_f0f2_aeee_417e_2192f5790344 -->|calls| 552e707a_ccba_b2a6_5917_15f7a417896b
  4bf7802a_6d99_1399_1f81_b8d1aa3ab685["substituteFunctions()"]
  cebe77e1_f0f2_aeee_417e_2192f5790344 -->|calls| 4bf7802a_6d99_1399_1f81_b8d1aa3ab685
  716eb081_2da3_764c_1bd4_89abf211e018["substituteAtVariant()"]
  cebe77e1_f0f2_aeee_417e_2192f5790344 -->|calls| 716eb081_2da3_764c_1bd4_89abf211e018
  e9e50ff6_7df6_110f_a568_3816eef96d3a["extractUsedVariables()"]
  cebe77e1_f0f2_aeee_417e_2192f5790344 -->|calls| e9e50ff6_7df6_110f_a568_3816eef96d3a
  f4b2481e_23ea_2f5c_fdf6_9834ac412e37["markUsedVariable()"]
  cebe77e1_f0f2_aeee_417e_2192f5790344 -->|calls| f4b2481e_23ea_2f5c_fdf6_9834ac412e37
  style cebe77e1_f0f2_aeee_417e_2192f5790344 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/design-system.ts lines 70–255

export function buildDesignSystem(
  theme: Theme,
  utilitiesSrc?: SourceLocation | undefined,
): DesignSystem {
  let utilities = createUtilities(theme)
  let variants = createVariants(theme)

  let parsedVariants = new DefaultMap((variant) => parseVariant(variant, designSystem))
  let parsedCandidates = new DefaultMap((candidate) =>
    Array.from(parseCandidate(candidate, designSystem)),
  )

  let compiledAstNodes = new DefaultMap<number>((flags) => {
    return new DefaultMap<Candidate>((candidate) => {
      let ast = compileAstNodes(candidate, designSystem, flags)

      try {
        // Arbitrary values (`text-[theme(--color-red-500)]`) and arbitrary
        // properties (`[--my-var:theme(--color-red-500)]`) can contain function
        // calls so we need evaluate any functions we find there that weren't in
        // the source CSS.
        substituteFunctions(
          ast.map(({ node }) => node),
          designSystem,
        )

        // JS plugins might contain an `@variant` inside a generated utility
        substituteAtVariant(
          ast.map(({ node }) => node),
          designSystem,
        )
      } catch (err) {
        // If substitution fails then the candidate likely contains a call to
        // `theme()` that is invalid which may be because of incorrect usage,
        // invalid arguments, or a theme key that does not exist.
        return []
      }

      return ast
    })
  })

  let trackUsedVariables = new DefaultMap((raw) => {
    for (let variable of extractUsedVariables(raw)) {
      theme.markUsedVariable(variable)
    }
  })

  function candidatesToAst(classes: string[]): AstNode[][] {
    let result: AstNode[][] = []

    for (let className of classes) {
      let wasValid = true

      let { astNodes } = compileCandidates([className], designSystem, {
        onInvalidCandidate() {
          wasValid = false
        },
      })

      if (utilitiesSrc) {
        walk(astNodes, (node) => {
          // We do this conditionally to preserve source locations from both
          // `@utility` and `@custom-variant`. Even though generated nodes are
          // cached this should be fine because `utilitiesNode.src` should not
          // change without a full rebuild which destroys the cache.
          node.src ??= utilitiesSrc
          return WalkAction.Continue
        })
      }

      // Disable all polyfills to not unnecessarily pollute IntelliSense output
      astNodes = optimizeAst(astNodes, designSystem, Polyfills.None)

      result.push(wasValid ? astNodes : [])
    }

    return result
  }

  function candidatesToCss(classes: string[]): (string | null)[] {
    return candidatesToAst(classes).map((nodes) => {
      return nodes.length > 0 ? toCss(nodes) : null
    })
  }

  let designSystem: DesignSystem = {
    theme,
    utilities,
    variants,

    invalidCandidates: new Set(),
    important: false,

    candidatesToCss,
    candidatesToAst,

    getClassOrder(classes) {
      return getClassOrder(this, classes)
    },
    getClassList() {
      return getClassList(this)
    },
    getVariants() {
      return getVariants(this)
    },

    parseCandidate(candidate: string) {
      return parsedCandidates.get(candidate)
    },
    parseVariant(variant: string) {
      return parsedVariants.get(variant)
    },
    compileAstNodes(candidate: Candidate, flags = CompileAstFlags.RespectImportant) {
      return compiledAstNodes.get(flags).get(candidate)
    },

    printCandidate(candidate: Candidate) {
      return printCandidate(designSystem, candidate)
    },
    printVariant(variant: Variant) {
      return printVariant(variant)
    },

    getVariantOrder() {
      let variants = Array.from(parsedVariants.values())
      variants.sort((a, z) => this.variants.compare(a, z))

      let order = new Map<Variant, number>()
      let prevVariant: Variant | undefined = undefined
      let index: number = 0

      for (let variant of variants) {
        if (variant === null) {
          continue
        }
        // This variant is not the same order as the previous one
        // so it goes into a new group
        if (prevVariant !== undefined && this.variants.compare(prevVariant, variant) !== 0) {
          index++
        }

        order.set(variant, index)
        prevVariant = variant
      }

      return order
    },

    resolveThemeValue(path: `${ThemeKey}` | `${ThemeKey}${string}`, forceInline: boolean = true) {
      // Extract an eventual modifier from the path. e.g.:
      // - "--color-red-500 / 50%" -> "50%"
      let lastSlash = path.lastIndexOf('/')
      let modifier: string | null = null
      if (lastSlash !== -1) {
        modifier = path.slice(lastSlash + 1).trim()
        path = path.slice(0, lastSlash).trim() as ThemeKey
      }

      let themeValue =
        theme.resolve(null, [path], forceInline ? ThemeOptions.INLINE : ThemeOptions.NONE) ??
        undefined

      // Apply the opacity modifier if present
      if (modifier && themeValue) {
        return withAlpha(themeValue, modifier)
      }

      return themeValue
    },

    trackUsedVariables(raw: string) {
      trackUsedVariables.get(raw)
    },

    canonicalizeCandidates(candidates: string[], options?: CanonicalizeOptions) {
      return canonicalizeCandidates(this, candidates, options)
    },

    // General purpose storage, each key has to be a unique symbol to avoid
    // collisions.
    storage: {},
  }

  return designSystem
}

Subdomains

Frequently Asked Questions

What does buildDesignSystem() do?
buildDesignSystem() is a function in the tailwindcss codebase.
What does buildDesignSystem() call?
buildDesignSystem() calls 24 function(s): compare, compileAstNodes, compileCandidates, createUtilities, createVariants, extractUsedVariables, get, getClassList, and 16 more.
What calls buildDesignSystem()?
buildDesignSystem() is called by 4 function(s): loadDesignSystem, parseCss, run, simpleDesign.

Analyze Your Own Codebase

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

Try Supermodel Free