Home / Function/ toCss() — tailwindcss Function Reference

toCss() — tailwindcss Function Reference

Architecture documentation for the toCss() function in ast.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  af90c185_29a2_6c4c_ef06_b18f00f7655c["toCss()"]
  ef1f735b_b130_6631_a9cc_e465ace5e479["rewriteUrls()"]
  ef1f735b_b130_6631_a9cc_e465ace5e479 -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  f3a29437_b991_0a27_2561_a76b3f340642["tailwindcss()"]
  f3a29437_b991_0a27_2561_a76b3f340642 -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  5f3acb43_b93f_4293_caaa_25ba26d38178["substituteAtApply()"]
  5f3acb43_b93f_4293_caaa_25ba26d38178 -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  ec867cf3_916b_0d16_65ec_c715e69fee03["optimizeAst()"]
  ec867cf3_916b_0d16_65ec_c715e69fee03 -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  9e2f12c0_83ef_256b_739b_9ec6ebc69b08["applyCompatibilityHooks()"]
  9e2f12c0_83ef_256b_739b_9ec6ebc69b08 -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  cebe77e1_f0f2_aeee_417e_2192f5790344["buildDesignSystem()"]
  cebe77e1_f0f2_aeee_417e_2192f5790344 -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  8ea56f72_5e23_1ddc_863d_757d2deb5a0f["expand()"]
  8ea56f72_5e23_1ddc_863d_757d2deb5a0f -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  26086ff1_0d4f_fdb2_3fc4_d0c999f90a8c["parseCss()"]
  26086ff1_0d4f_fdb2_3fc4_d0c999f90a8c -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  214d4226_762c_0b6a_7c0b_f917da98dda0["compile()"]
  214d4226_762c_0b6a_7c0b_f917da98dda0 -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  b5142dec_f0ec_66d6_59a1_82d3dbd6f965["analyze()"]
  b5142dec_f0ec_66d6_59a1_82d3dbd6f965 -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  ad196438_55f7_af7b_1604_1d75c1c27d8e["buildPluginApi()"]
  ad196438_55f7_af7b_1604_1d75c1c27d8e -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  5c9381d6_815c_d899_eaab_849d755be47e["createCssUtility()"]
  5c9381d6_815c_d899_eaab_849d755be47e -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  1e85b11a_69b5_68fc_237d_f0167751c10b["substituteFunctionsInValue()"]
  1e85b11a_69b5_68fc_237d_f0167751c10b -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  24d8d1d0_89ac_76a1_956a_704ad43fcba6["modernizeArbitraryValuesVariant()"]
  24d8d1d0_89ac_76a1_956a_704ad43fcba6 -->|calls| af90c185_29a2_6c4c_ef06_b18f00f7655c
  style af90c185_29a2_6c4c_ef06_b18f00f7655c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/tailwindcss/src/ast.ts lines 682–887

export function toCss(ast: AstNode[], track?: boolean) {
  let pos = 0

  let source: Source = {
    file: null,
    code: '',
  }

  function stringify(node: AstNode, depth = 0): string {
    let css = ''
    let indent = '  '.repeat(depth)

    // Declaration
    if (node.kind === 'declaration') {
      css += `${indent}${node.property}: ${node.value}${node.important ? ' !important' : ''};\n`

      if (track) {
        // indent
        pos += indent.length

        // node.property
        let start = pos
        pos += node.property.length

        // `: `
        pos += 2

        // node.value
        pos += node.value?.length ?? 0

        // !important
        if (node.important) {
          pos += 11
        }

        let end = pos

        // `;\n`
        pos += 2

        node.dst = [source, start, end]
      }
    }

    // Rule
    else if (node.kind === 'rule') {
      css += `${indent}${node.selector} {\n`

      if (track) {
        // indent
        pos += indent.length

        // node.selector
        let start = pos
        pos += node.selector.length

        // ` `
        pos += 1

        let end = pos
        node.dst = [source, start, end]

        // `{\n`
        pos += 2
      }

      for (let child of node.nodes) {
        css += stringify(child, depth + 1)
      }

      css += `${indent}}\n`

      if (track) {
        // indent
        pos += indent.length

        // `}\n`
        pos += 2
      }
    }

    // AtRule
    else if (node.kind === 'at-rule') {
      // Print at-rules without nodes with a `;` instead of an empty block.
      //
      // E.g.:
      //
      // ```css
      // @layer base, components, utilities;
      // ```
      if (node.nodes.length === 0) {
        let css = `${indent}${node.name} ${node.params};\n`

        if (track) {
          // indent
          pos += indent.length

          // node.name
          let start = pos
          pos += node.name.length

          // ` `
          pos += 1

          // node.params
          pos += node.params.length
          let end = pos

          // `;\n`
          pos += 2

          node.dst = [source, start, end]
        }

        return css
      }

      css += `${indent}${node.name}${node.params ? ` ${node.params} ` : ' '}{\n`

      if (track) {
        // indent
        pos += indent.length

        // node.name
        let start = pos
        pos += node.name.length

        if (node.params) {
          // ` `
          pos += 1

          // node.params
          pos += node.params.length
        }

        // ` `
        pos += 1

        let end = pos
        node.dst = [source, start, end]

        // `{\n`
        pos += 2
      }

      for (let child of node.nodes) {
        css += stringify(child, depth + 1)
      }

      css += `${indent}}\n`

      if (track) {
        // indent
        pos += indent.length

        // `}\n`
        pos += 2
      }
    }

    // Comment
    else if (node.kind === 'comment') {
      css += `${indent}/*${node.value}*/\n`

      if (track) {
        // indent
        pos += indent.length

        // The comment itself. We do this instead of just the inside because
        // it seems more useful to have the entire comment span tracked.
        let start = pos
        pos += 2 + node.value.length + 2
        let end = pos

        node.dst = [source, start, end]

        // `\n`
        pos += 1
      }
    }

    // These should've been handled already by `optimizeAst` which
    // means we can safely ignore them here. We return an empty string
    // immediately to signal that something went wrong.
    else if (node.kind === 'context' || node.kind === 'at-root') {
      return ''
    }

    // Unknown
    else {
      node satisfies never
    }

    return css
  }

  let css = ''

  for (let node of ast) {
    css += stringify(node, 0)
  }

  source.code = css

  return css
}

Subdomains

Calls

Frequently Asked Questions

What does toCss() do?
toCss() is a function in the tailwindcss codebase.
What does toCss() call?
toCss() calls 1 function(s): toCss.
What calls toCss()?
toCss() is called by 17 function(s): analyze, applyCompatibilityHooks, buildDesignSystem, buildPluginApi, compile, createCssUtility, createUtilitySignatureCache, createVariantSignatureCache, and 9 more.

Analyze Your Own Codebase

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

Try Supermodel Free