Home / Class/ Root Class — tailwindcss Architecture

Root Class — tailwindcss Architecture

Architecture documentation for the Root class in index.ts from the tailwindcss codebase.

Entity Profile

Dependency Diagram

graph TD
  26893bc3_0033_d6d7_a323_c3fa650b4ad4["Root"]
  2edc6732_180e_8223_de2c_856c87ce7657["index.ts"]
  26893bc3_0033_d6d7_a323_c3fa650b4ad4 -->|defined in| 2edc6732_180e_8223_de2c_856c87ce7657
  23164aa1_271e_6d63_661c_c5ef37d42f0e["constructor()"]
  26893bc3_0033_d6d7_a323_c3fa650b4ad4 -->|method| 23164aa1_271e_6d63_661c_c5ef37d42f0e
  68ac0b75_b7fb_24c9_5ba7_a0d956bcaba5["generate()"]
  26893bc3_0033_d6d7_a323_c3fa650b4ad4 -->|method| 68ac0b75_b7fb_24c9_5ba7_a0d956bcaba5
  5162df04_9826_6007_035c_c26fd3cadeca["addBuildDependency()"]
  26893bc3_0033_d6d7_a323_c3fa650b4ad4 -->|method| 5162df04_9826_6007_035c_c26fd3cadeca
  6f92adb6_955a_b157_bc28_eccea87e921f["requiresBuild()"]
  26893bc3_0033_d6d7_a323_c3fa650b4ad4 -->|method| 6f92adb6_955a_b157_bc28_eccea87e921f

Relationship Graph

Source Code

packages/@tailwindcss-vite/src/index.ts lines 247–454

class Root {
  // The lazily-initialized Tailwind compiler components. These are persisted
  // throughout rebuilds but will be re-initialized if the rebuild strategy is
  // set to `full`.
  private compiler?: Awaited<ReturnType<typeof compile>>

  // The lazily-initialized Tailwind scanner.
  private scanner?: Scanner

  // List of all candidates that were being returned by the root scanner during
  // the lifetime of the root.
  private candidates: Set<string> = new Set<string>()

  // List of all build dependencies (e.g. imported  stylesheets or plugins) and
  // their last modification timestamp. If no mtime can be found, we need to
  // assume the file has always changed.
  private buildDependencies = new Map<string, number | null>()

  constructor(
    private id: string,
    private base: string,

    private enableSourceMaps: boolean,
    private customCssResolver: (id: string, base: string) => Promise<string | false | undefined>,
    private customJsResolver: (id: string, base: string) => Promise<string | false | undefined>,
  ) {}

  // Generate the CSS for the root file. This can return false if the file is
  // not considered a Tailwind root. When this happened, the root can be GCed.
  public async generate(
    content: string,
    _addWatchFile: (file: string) => void,
    I: Instrumentation,
  ): Promise<
    | {
        code: string
        map: string | undefined
      }
    | false
  > {
    let inputPath = idToPath(this.id)

    function addWatchFile(file: string) {
      // Don't watch the input file since it's already a dependency and causes
      // issues with some setups (e.g. Qwik).
      if (file === inputPath) {
        return
      }

      // Scanning `.svg` file containing a `#` or `?` in the path will
      // crash Vite. We work around this for now by ignoring updates to them.
      //
      // https://github.com/tailwindlabs/tailwindcss/issues/16877
      if (/[\#\?].*\.svg$/.test(file)) {
        return
      }
      _addWatchFile(file)
    }

    let requiresBuildPromise = this.requiresBuild()
    let inputBase = path.dirname(path.resolve(inputPath))

    if (!this.compiler || !this.scanner || (await requiresBuildPromise)) {
      clearRequireCache(Array.from(this.buildDependencies.keys()))
      this.buildDependencies.clear()

      this.addBuildDependency(idToPath(inputPath))

      DEBUG && I.start('Setup compiler')
      let addBuildDependenciesPromises: Promise<void>[] = []
      this.compiler = await compile(content, {
        from: this.enableSourceMaps ? this.id : undefined,
        base: inputBase,
        shouldRewriteUrls: true,
        onDependency: (path) => {
          addWatchFile(path)
          addBuildDependenciesPromises.push(this.addBuildDependency(path))
        },

        customCssResolver: this.customCssResolver,
        customJsResolver: this.customJsResolver,

Frequently Asked Questions

What is the Root class?
Root is a class in the tailwindcss codebase, defined in packages/@tailwindcss-vite/src/index.ts.
Where is Root defined?
Root is defined in packages/@tailwindcss-vite/src/index.ts at line 247.

Analyze Your Own Codebase

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

Try Supermodel Free