Home / File/ get-module-dependencies.ts — tailwindcss Source File

get-module-dependencies.ts — tailwindcss Source File

Architecture documentation for get-module-dependencies.ts, a typescript file in the tailwindcss codebase. 2 imports, 1 dependents.

File typescript NodeServer Resolver 2 imports 1 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  d5a4928f_80ca_0a99_96fb_178e14fe7a6a["get-module-dependencies.ts"]
  b75e8457_6610_e7ce_eeaf_9a1dd10fc510["promises"]
  d5a4928f_80ca_0a99_96fb_178e14fe7a6a --> b75e8457_6610_e7ce_eeaf_9a1dd10fc510
  89aef3dd_1eed_c141_d425_b8949215a653["node:path"]
  d5a4928f_80ca_0a99_96fb_178e14fe7a6a --> 89aef3dd_1eed_c141_d425_b8949215a653
  86cbca6b_bfa0_804c_ee2e_2cc7a7aef7fe["compile.ts"]
  86cbca6b_bfa0_804c_ee2e_2cc7a7aef7fe --> d5a4928f_80ca_0a99_96fb_178e14fe7a6a
  style d5a4928f_80ca_0a99_96fb_178e14fe7a6a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import fs from 'node:fs/promises'
import path from 'node:path'

// Patterns we use to match dependencies in a file whether in CJS, ESM, or TypeScript
const DEPENDENCY_PATTERNS = [
  /import[\s\S]*?['"](.{3,}?)['"]/gi,
  /import[\s\S]*from[\s\S]*?['"](.{3,}?)['"]/gi,
  /export[\s\S]*from[\s\S]*?['"](.{3,}?)['"]/gi,
  /require\(['"`](.+)['"`]\)/gi,
]

// Given the current file `a.ts`, we want to make sure that when importing `b` that we resolve
// `b.ts` before `b.js`
//
// E.g.:
//
// a.ts
//   b // .ts
//   c // .ts
// a.js
//   b // .js or .ts
const JS_EXTENSIONS = ['.js', '.cjs', '.mjs']
const JS_RESOLUTION_ORDER = ['', '.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', '.jsx', '.tsx']
const TS_RESOLUTION_ORDER = ['', '.ts', '.cts', '.mts', '.tsx', '.js', '.cjs', '.mjs', '.jsx']

async function resolveWithExtension(file: string, extensions: string[]) {
  // Try to find `./a.ts`, `./a.cts`, ... from `./a`
  for (let ext of extensions) {
    let full = `${file}${ext}`

    let stats = await fs.stat(full).catch(() => null)
    if (stats?.isFile()) return full
  }

  // Try to find `./a/index.js` from `./a`
  for (let ext of extensions) {
    let full = `${file}/index${ext}`

    let exists = await fs.access(full).then(
      () => true,
      () => false,
    )
    if (exists) {
      return full
    }
  }

  return null
}

async function traceDependencies(
  seen: Set<string>,
  filename: string,
  base: string,
  ext: string,
): Promise<void> {
  // Try to find the file
  let extensions = JS_EXTENSIONS.includes(ext) ? JS_RESOLUTION_ORDER : TS_RESOLUTION_ORDER
  let absoluteFile = await resolveWithExtension(path.resolve(base, filename), extensions)
  if (absoluteFile === null) return // File doesn't exist

  // Prevent infinite loops when there are circular dependencies
  if (seen.has(absoluteFile)) return // Already seen

  // Mark the file as a dependency
  seen.add(absoluteFile)

  // Resolve new base for new imports/requires
  base = path.dirname(absoluteFile)
  ext = path.extname(absoluteFile)

  let contents = await fs.readFile(absoluteFile, 'utf-8')

  // Recursively trace dependencies in parallel
  let promises = []

  for (let pattern of DEPENDENCY_PATTERNS) {
    for (let match of contents.matchAll(pattern)) {
      // Bail out if it's not a relative file
      if (!match[1].startsWith('.')) continue

      promises.push(traceDependencies(seen, match[1], base, ext))
    }
  }

  await Promise.all(promises)
}

/**
 * Trace all dependencies of a module recursively
 *
 * The result is an unordered set of absolute file paths. Meaning that the order
 * is not guaranteed to be equal to source order or across runs.
 **/
export async function getModuleDependencies(absoluteFilePath: string) {
  let seen = new Set<string>()

  await traceDependencies(
    seen,
    absoluteFilePath,
    path.dirname(absoluteFilePath),
    path.extname(absoluteFilePath),
  )

  return Array.from(seen)
}

Domain

Subdomains

Dependencies

  • node:path
  • promises

Frequently Asked Questions

What does get-module-dependencies.ts do?
get-module-dependencies.ts is a source file in the tailwindcss codebase, written in typescript. It belongs to the NodeServer domain, Resolver subdomain.
What functions are defined in get-module-dependencies.ts?
get-module-dependencies.ts defines 3 function(s): getModuleDependencies, resolveWithExtension, traceDependencies.
What does get-module-dependencies.ts depend on?
get-module-dependencies.ts imports 2 module(s): node:path, promises.
What files import get-module-dependencies.ts?
get-module-dependencies.ts is imported by 1 file(s): compile.ts.
Where is get-module-dependencies.ts in the architecture?
get-module-dependencies.ts is located at packages/@tailwindcss-node/src/get-module-dependencies.ts (domain: NodeServer, subdomain: Resolver, directory: packages/@tailwindcss-node/src).

Analyze Your Own Codebase

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

Try Supermodel Free