Home / Function/ cssAstToPostCssAst() — tailwindcss Function Reference

cssAstToPostCssAst() — tailwindcss Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  81a639ec_bc2c_cf5e_48e3_3ae33426ae0a["cssAstToPostCssAst()"]
  f4282831_f22b_2be1_4c8f_449ba7a9c8d7["tailwindcss()"]
  f4282831_f22b_2be1_4c8f_449ba7a9c8d7 -->|calls| 81a639ec_bc2c_cf5e_48e3_3ae33426ae0a
  85813fc9_ab7d_8f75_33b7_a217e8daf5ee["createLineTable()"]
  81a639ec_bc2c_cf5e_48e3_3ae33426ae0a -->|calls| 85813fc9_ab7d_8f75_33b7_a217e8daf5ee
  0aa64a1c_efd8_a69d_48ed_649b7a86c854["get()"]
  81a639ec_bc2c_cf5e_48e3_3ae33426ae0a -->|calls| 0aa64a1c_efd8_a69d_48ed_649b7a86c854
  47ef7bd7_b959_59c4_dbd1_328f35d7cd89["decl()"]
  81a639ec_bc2c_cf5e_48e3_3ae33426ae0a -->|calls| 47ef7bd7_b959_59c4_dbd1_328f35d7cd89
  f4f92a3d_c13e_a751_8402_451ffa4c772f["rule()"]
  81a639ec_bc2c_cf5e_48e3_3ae33426ae0a -->|calls| f4f92a3d_c13e_a751_8402_451ffa4c772f
  c35acfc6_964d_737e_6ecc_275e6f10293a["atRule()"]
  81a639ec_bc2c_cf5e_48e3_3ae33426ae0a -->|calls| c35acfc6_964d_737e_6ecc_275e6f10293a
  f4c1885c_39a0_4343_f87c_7a8b1145a3bc["comment()"]
  81a639ec_bc2c_cf5e_48e3_3ae33426ae0a -->|calls| f4c1885c_39a0_4343_f87c_7a8b1145a3bc
  style 81a639ec_bc2c_cf5e_48e3_3ae33426ae0a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/@tailwindcss-postcss/src/ast.ts lines 9–124

export function cssAstToPostCssAst(
  postcss: postcss.Postcss,
  ast: AstNode[],
  source?: postcss.Source,
): postcss.Root {
  let inputMap = new DefaultMap<Source, postcss.Input>((src) => {
    return new postcss.Input(src.code, {
      map: source?.input.map,
      from: src.file ?? undefined,
    })
  })

  let lineTables = new DefaultMap<Source, LineTable>((src) => createLineTable(src.code))

  let root = postcss.root()
  root.source = source

  function toSource(loc: SourceLocation | undefined): postcss.Source | undefined {
    // Use the fallback if this node has no location info in the AST
    if (!loc) return
    if (!loc[0]) return

    let table = lineTables.get(loc[0])
    let start = table.find(loc[1])
    let end = table.find(loc[2])

    return {
      input: inputMap.get(loc[0]),
      start: {
        line: start.line,
        column: start.column + 1,
        offset: loc[1],
      },
      end: {
        line: end.line,
        column: end.column + 1,
        offset: loc[2],
      },
    }
  }

  function updateSource(astNode: postcss.ChildNode, loc: SourceLocation | undefined) {
    let source = toSource(loc)

    // The `source` property on PostCSS nodes must be defined if present because
    // `toJSON()` reads each property and tries to read from source.input if it
    // sees a `source` property. This means for a missing or otherwise absent
    // source it must be *missing* from the object rather than just `undefined`
    if (source) {
      astNode.source = source
    } else {
      delete astNode.source
    }
  }

  function transform(node: AstNode, parent: postcss.Container) {
    // Declaration
    if (node.kind === 'declaration') {
      let astNode = postcss.decl({
        prop: node.property,
        value: node.value ?? '',
        important: node.important,
      })
      updateSource(astNode, node.src)
      parent.append(astNode)
    }

    // Rule
    else if (node.kind === 'rule') {
      let astNode = postcss.rule({ selector: node.selector })
      updateSource(astNode, node.src)
      astNode.raws.semicolon = true
      parent.append(astNode)
      for (let child of node.nodes) {
        transform(child, astNode)
      }
    }

    // AtRule
    else if (node.kind === 'at-rule') {
      let astNode = postcss.atRule({ name: node.name.slice(1), params: node.params })
      updateSource(astNode, node.src)
      astNode.raws.semicolon = true
      parent.append(astNode)
      for (let child of node.nodes) {
        transform(child, astNode)
      }
    }

    // Comment
    else if (node.kind === 'comment') {
      let astNode = postcss.comment({ text: node.value })
      // Spaces are encoded in our node.value already, no need to add additional
      // spaces.
      astNode.raws.left = ''
      astNode.raws.right = ''
      updateSource(astNode, node.src)
      parent.append(astNode)
    }

    // AtRoot & Context should not happen
    else if (node.kind === 'at-root' || node.kind === 'context') {
    }

    // Unknown
    else {
      node satisfies never
    }
  }

  for (let node of ast) {
    transform(node, root)
  }

  return root
}

Domain

Subdomains

Called By

Frequently Asked Questions

What does cssAstToPostCssAst() do?
cssAstToPostCssAst() is a function in the tailwindcss codebase.
What does cssAstToPostCssAst() call?
cssAstToPostCssAst() calls 6 function(s): atRule, comment, createLineTable, decl, get, rule.
What calls cssAstToPostCssAst()?
cssAstToPostCssAst() is called by 1 function(s): tailwindcss.

Analyze Your Own Codebase

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

Try Supermodel Free