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
  e7a34553_0273_6202_4792_07409e33d8f0["toCss()"]
  fe420c64_fc5d_8de5_0c9f_c3e614f856a9["rewriteUrls()"]
  fe420c64_fc5d_8de5_0c9f_c3e614f856a9 -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  f4282831_f22b_2be1_4c8f_449ba7a9c8d7["tailwindcss()"]
  f4282831_f22b_2be1_4c8f_449ba7a9c8d7 -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  556f6ab2_af7d_ed90_84f0_6b49e632571a["substituteAtApply()"]
  556f6ab2_af7d_ed90_84f0_6b49e632571a -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  a6e11c3d_c962_0a65_d91f_6fbe955cf4f0["optimizeAst()"]
  a6e11c3d_c962_0a65_d91f_6fbe955cf4f0 -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  d0cd8e91_7dbc_4e3d_f621_705d53f7a49e["applyCompatibilityHooks()"]
  d0cd8e91_7dbc_4e3d_f621_705d53f7a49e -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  c58d3214_88d6_f4fc_257f_8e84def5b24f["buildDesignSystem()"]
  c58d3214_88d6_f4fc_257f_8e84def5b24f -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  9b0f6215_d728_39c5_c24a_2e50640933f4["expand()"]
  9b0f6215_d728_39c5_c24a_2e50640933f4 -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  95cb326e_6b59_0903_0c96_d221fca5c2b1["parseCss()"]
  95cb326e_6b59_0903_0c96_d221fca5c2b1 -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  38bfe51e_e2b6_fcde_5ec2_9e78b758e748["compile()"]
  38bfe51e_e2b6_fcde_5ec2_9e78b758e748 -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  5898a47f_729b_b30c_bf7b_865876b2750a["analyze()"]
  5898a47f_729b_b30c_bf7b_865876b2750a -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  77d1d37f_6021_dc8c_6d04_0c3b07e3ad19["migrateTheme()"]
  77d1d37f_6021_dc8c_6d04_0c3b07e3ad19 -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  f7274245_f1a4_d8b1_30d8_a1497768c1d4["keyframesToCss()"]
  f7274245_f1a4_d8b1_30d8_a1497768c1d4 -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  3b6a2079_7f12_42cd_ba9f_a57ecec4366d["buildPluginApi()"]
  3b6a2079_7f12_42cd_ba9f_a57ecec4366d -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  387b2b72_8509_7339_6444_0aa98e0581bb["replaceNestedClassNameReferences()"]
  387b2b72_8509_7339_6444_0aa98e0581bb -->|calls| e7a34553_0273_6202_4792_07409e33d8f0
  style e7a34553_0273_6202_4792_07409e33d8f0 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
}

Domain

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 18 function(s): analyze, applyCompatibilityHooks, buildDesignSystem, buildPluginApi, compile, createUtilitySignatureCache, createVariantSignatureCache, expand, and 10 more.

Analyze Your Own Codebase

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

Try Supermodel Free