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
  112073d3_66c7_8610_9209_6e6a56fb828b["cssAstToPostCssAst()"]
  f3a29437_b991_0a27_2561_a76b3f340642["tailwindcss()"]
  f3a29437_b991_0a27_2561_a76b3f340642 -->|calls| 112073d3_66c7_8610_9209_6e6a56fb828b
  2c7b6e31_c902_cd2d_f5ec_bf42f894f3a3["createLineTable()"]
  112073d3_66c7_8610_9209_6e6a56fb828b -->|calls| 2c7b6e31_c902_cd2d_f5ec_bf42f894f3a3
  4cd99e59_ac1e_2a1f_0946_33cc1afd2532["get()"]
  112073d3_66c7_8610_9209_6e6a56fb828b -->|calls| 4cd99e59_ac1e_2a1f_0946_33cc1afd2532
  85b46de2_edfa_9371_e2c6_e60f3f5346a2["decl()"]
  112073d3_66c7_8610_9209_6e6a56fb828b -->|calls| 85b46de2_edfa_9371_e2c6_e60f3f5346a2
  08f33202_11d1_569a_e8df_de23eb987e2f["rule()"]
  112073d3_66c7_8610_9209_6e6a56fb828b -->|calls| 08f33202_11d1_569a_e8df_de23eb987e2f
  a9af385a_fd12_f1d8_7cf0_ccb9b281ca18["atRule()"]
  112073d3_66c7_8610_9209_6e6a56fb828b -->|calls| a9af385a_fd12_f1d8_7cf0_ccb9b281ca18
  dbaac8a3_b27b_2942_9a4d_7e9063a9649d["comment()"]
  112073d3_66c7_8610_9209_6e6a56fb828b -->|calls| dbaac8a3_b27b_2942_9a4d_7e9063a9649d
  style 112073d3_66c7_8610_9209_6e6a56fb828b 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
}

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