Home / Function/ analyze() — tailwindcss Function Reference

analyze() — tailwindcss Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  6d7d3a7a_08dc_88e4_0b4a_9f563d66e582["analyze()"]
  c30c28f2_b6df_e90d_67d1_db074bef35a5["analyze.ts"]
  6d7d3a7a_08dc_88e4_0b4a_9f563d66e582 -->|defined in| c30c28f2_b6df_e90d_67d1_db074bef35a5
  ad6791c1_558e_31b6_3be7_e2ace1f1c4c9["loadSync()"]
  6d7d3a7a_08dc_88e4_0b4a_9f563d66e582 -->|calls| ad6791c1_558e_31b6_3be7_e2ace1f1c4c9
  8d04f499_5623_03cc_19ff_b22a41168fc3["resolveCssId()"]
  6d7d3a7a_08dc_88e4_0b4a_9f563d66e582 -->|calls| 8d04f499_5623_03cc_19ff_b22a41168fc3
  6c5b3bb2_4069_e0af_8887_216e1c995446["error()"]
  6d7d3a7a_08dc_88e4_0b4a_9f563d66e582 -->|calls| 6c5b3bb2_4069_e0af_8887_216e1c995446
  d3ccc5ce_e370_63df_a97d_a2f004fd83ca["highlight()"]
  6d7d3a7a_08dc_88e4_0b4a_9f563d66e582 -->|calls| d3ccc5ce_e370_63df_a97d_a2f004fd83ca
  1c806fef_a933_285c_65d9_c10998b12c7d["relative()"]
  6d7d3a7a_08dc_88e4_0b4a_9f563d66e582 -->|calls| 1c806fef_a933_285c_65d9_c10998b12c7d
  2820372c_b982_9e06_fc23_f8f4ac308d00["get()"]
  6d7d3a7a_08dc_88e4_0b4a_9f563d66e582 -->|calls| 2820372c_b982_9e06_fc23_f8f4ac308d00
  c58cbb33_f3cc_0b4f_844a_15bf66a1dc61["segment()"]
  6d7d3a7a_08dc_88e4_0b4a_9f563d66e582 -->|calls| c58cbb33_f3cc_0b4f_844a_15bf66a1dc61
  65bf5d7b_4133_6a0b_291b_ac92d67c696b["analyzeImportPaths()"]
  6d7d3a7a_08dc_88e4_0b4a_9f563d66e582 -->|calls| 65bf5d7b_4133_6a0b_291b_ac92d67c696b
  9f5a8c9f_6828_fe75_f38e_72ee4cf3c428["ancestors()"]
  6d7d3a7a_08dc_88e4_0b4a_9f563d66e582 -->|calls| 9f5a8c9f_6828_fe75_f38e_72ee4cf3c428
  style 6d7d3a7a_08dc_88e4_0b4a_9f563d66e582 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/@tailwindcss-upgrade/src/codemods/css/analyze.ts lines 10–300

export async function analyze(stylesheets: Stylesheet[]) {
  let isIgnored = await isGitIgnored()
  let processingQueue: (() => Promise<Result>)[] = []
  let stylesheetsByFile = new DefaultMap<string, Stylesheet | null>((file) => {
    // We don't want to process ignored files (like node_modules)
    try {
      if (isIgnored(file)) {
        return null
      }
    } catch {
      // If the file is not part of the current working directory (which can
      // happen if you import `tailwindcss` and it's loading a shared file from
      // pnpm) then this will throw.
      return null
    }

    try {
      let sheet = Stylesheet.loadSync(file)

      // Mutate incoming stylesheets to include the newly discovered sheet
      stylesheets.push(sheet)

      // Queue up the processing of this stylesheet
      processingQueue.push(() => processor.process(sheet.root, { from: sheet.file! }))

      return sheet
    } catch {
      return null
    }
  })

  // Step 1: Record which `@import` rules point to which stylesheets
  // and which stylesheets are parents/children of each other
  let processor = postcss([
    {
      postcssPlugin: 'mark-import-nodes',
      AtRule: {
        import(node) {
          // Find what the import points to
          let id = node.params.match(/['"](.*?)['"]/)?.[1]
          if (!id) return

          let basePath = node.source?.input.file
            ? path.dirname(node.source.input.file)
            : process.cwd()

          // Resolve the import to a file path
          let resolvedPath: string | false = false
          try {
            // We first try to resolve the file as relative to the current file
            // to mimic the behavior of `postcss-import` since that's what was
            // used to resolve imports in Tailwind CSS v3.
            if (id[0] !== '.') {
              try {
                resolvedPath = resolveCssId(`./${id}`, basePath)
              } catch {}
            }

            if (!resolvedPath) {
              resolvedPath = resolveCssId(id, basePath)
            }
          } catch (err) {
            // Import is a URL, we don't want to process these, but also don't
            // want to show an error message for them.
            if (id.startsWith('http://') || id.startsWith('https://') || id.startsWith('//')) {
              return
            }

            // Something went wrong, we can't resolve the import.
            error(
              `Failed to resolve import: ${highlight(id)} in ${highlight(relative(node.source?.input.file!, basePath))}. Skipping.`,
              { prefix: '↳ ' },
            )
            return
          }

          if (!resolvedPath) return

          // Find the stylesheet pointing to the resolved path
          let stylesheet = stylesheetsByFile.get(resolvedPath)

Subdomains

Frequently Asked Questions

What does analyze() do?
analyze() is a function in the tailwindcss codebase, defined in packages/@tailwindcss-upgrade/src/codemods/css/analyze.ts.
Where is analyze() defined?
analyze() is defined in packages/@tailwindcss-upgrade/src/codemods/css/analyze.ts at line 10.
What does analyze() call?
analyze() calls 9 function(s): analyzeImportPaths, ancestors, error, get, highlight, loadSync, relative, resolveCssId, and 1 more.

Analyze Your Own Codebase

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

Try Supermodel Free