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
  60cad534_12fe_6e72_4bc7_443aad14536b["analyze()"]
  764d02dc_895f_8f85_d274_59af948c9ebb["analyze.ts"]
  60cad534_12fe_6e72_4bc7_443aad14536b -->|defined in| 764d02dc_895f_8f85_d274_59af948c9ebb
  7b77858e_ca4f_a550_ad82_d0815acce8e3["loadSync()"]
  60cad534_12fe_6e72_4bc7_443aad14536b -->|calls| 7b77858e_ca4f_a550_ad82_d0815acce8e3
  5a0b5868_f0d5_73f9_87bf_98f06eb00ddb["resolveCssId()"]
  60cad534_12fe_6e72_4bc7_443aad14536b -->|calls| 5a0b5868_f0d5_73f9_87bf_98f06eb00ddb
  f1ca9393_701c_e4a7_8303_ed03f028d19b["error()"]
  60cad534_12fe_6e72_4bc7_443aad14536b -->|calls| f1ca9393_701c_e4a7_8303_ed03f028d19b
  a81de696_6bb5_40db_26ca_1d707ccebed9["highlight()"]
  60cad534_12fe_6e72_4bc7_443aad14536b -->|calls| a81de696_6bb5_40db_26ca_1d707ccebed9
  efdaf4fe_1cde_66e0_60e0_5e155e297f6d["relative()"]
  60cad534_12fe_6e72_4bc7_443aad14536b -->|calls| efdaf4fe_1cde_66e0_60e0_5e155e297f6d
  5bcf4886_1230_a8ff_7302_a26cc5a9a525["get()"]
  60cad534_12fe_6e72_4bc7_443aad14536b -->|calls| 5bcf4886_1230_a8ff_7302_a26cc5a9a525
  f712ed47_45d4_4e5a_dd73_fdefa1da71da["segment()"]
  60cad534_12fe_6e72_4bc7_443aad14536b -->|calls| f712ed47_45d4_4e5a_dd73_fdefa1da71da
  097a5886_73fc_f3e3_8de2_3a76f018c380["analyzeImportPaths()"]
  60cad534_12fe_6e72_4bc7_443aad14536b -->|calls| 097a5886_73fc_f3e3_8de2_3a76f018c380
  4d021142_8ab8_64dd_9dd3_89c5a94806be["ancestors()"]
  60cad534_12fe_6e72_4bc7_443aad14536b -->|calls| 4d021142_8ab8_64dd_9dd3_89c5a94806be
  style 60cad534_12fe_6e72_4bc7_443aad14536b 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