ホームページ ウェブフロントエンド jsチュートリアル shadcn-ui/ui コードベース分析: shadcn-ui CLI はどのように機能しますか? — パート 3

shadcn-ui/ui コードベース分析: shadcn-ui CLI はどのように機能しますか? — パート 3

Jul 19, 2024 am 09:58 AM

I wanted to find out how shadcn-ui CLI works. In this article, I discuss the code used to build the shadcn-ui/ui CLI.

In part 2.11, we looked at runInit function and how shadcn-ui/ui ensures directories provided in resolvedPaths in config exist.

The following operations are performed in runInit function:

  1. Ensure all resolved paths directories exist.
  2. Write tailwind config.
  3. Write css file.
  4. Write cn file.
  5. Install dependencies.

1 and 2 from the above are covered in part 2.12, let’s find out how “Write css file” is done.

Write css file

The below code snippet is picked from packages/cli/src/commands/init.

// Write css file.
const baseColor = await getRegistryBaseColor(config.tailwind.baseColor)
if (baseColor) {
  await fs.writeFile(
    config.resolvedPaths.tailwindCss,
    config.tailwind.cssVariables
      ? config.tailwind.prefix
        ? applyPrefixesCss(baseColor.cssVarsTemplate, config.tailwind.prefix)
        : baseColor.cssVarsTemplate
      : baseColor.inlineColorsTemplate,
    "utf8"
  )
}
ログイン後にコピー

baseColor is returned from getRegistryBaseColor and then using fs.writeFile, css code is written to the file located at path provided by config.resolvedPaths.tailwindCss.

getRegistryBaseColor

getRegistryBaseColor function is picked from src/utils/registry/index.ts

export async function getRegistryBaseColor(baseColor: string) {
  try {
    const \[result\] = await fetchRegistry(\[\`colors/${baseColor}.json\`\])

    return registryBaseColorSchema.parse(result)
  } catch (error) {
    throw new Error(\`Failed to fetch base color from registry.\`)
  }
}
ログイン後にコピー

An example baseColor json looks like below:

shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? — Part 3

Result fetched is validated using registryBaseColorSchema.

applyPrefixesCss

shadcn-ui docs provides an option to provide tailwind prefix in components.json. You can read more about tailwind prefix from the tailwind docs.

export function applyPrefix(input: string, prefix: string = "") {
  const classNames = input.split(" ")
  const prefixed: string\[\] = \[\]
  for (let className of classNames) {
    const \[variant, value, modifier\] = splitClassName(className)
    if (variant) {
      modifier
        ? prefixed.push(\`${variant}:${prefix}${value}/${modifier}\`)
        : prefixed.push(\`${variant}:${prefix}${value}\`)
    } else {
      modifier
        ? prefixed.push(\`${prefix}${value}/${modifier}\`)
        : prefixed.push(\`${prefix}${value}\`)
    }
  }
  return prefixed.join(" ")
}


export function applyPrefixesCss(css: string, prefix: string) {
  const lines = css.split("\\n")
  for (let line of lines) {
    if (line.includes("@apply")) {
      const originalTWCls = line.replace("@apply", "").trim()
      const prefixedTwCls = applyPrefix(originalTWCls, prefix)
      css = css.replace(originalTWCls, prefixedTwCls)
    }
  }
  return css
}
ログイン後にコピー

Below is a cssVarsTemplate example picked from https://ui.shadcn.com/registry/colors/slate.json:

"cssVarsTemplate": "@tailwind base;\\n  @tailwind components;\\n  @tailwind utilities;\\n\\n  @layer base {\\n    :root {\\n      --background: 0 0% 100%;\\n      --foreground: 222.2 84% 4.9%;\\n      --card: 0 0% 100%;\\n      --card-foreground: 222.2 84% 4.9%;\\n      --popover: 0 0% 100%;\\n      --popover-foreground: 222.2 84% 4.9%;\\n      --primary: 222.2 47.4% 11.2%;\\n      --primary-foreground: 210 40% 98%;\\n      --secondary: 210 40% 96.1%;\\n      --secondary-foreground: 222.2 47.4% 11.2%;\\n      --muted: 210 40% 96.1%;\\n      --muted-foreground: 215.4 16.3% 46.9%;\\n      --accent: 210 40% 96.1%;\\n      --accent-foreground: 222.2 47.4% 11.2%;\\n      --destructive: 0 84.2% 60.2%;\\n      --destructive-foreground: 210 40% 98%;\\n      --border: 214.3 31.8% 91.4%;\\n      --input: 214.3 31.8% 91.4%;\\n      --ring: 222.2 84% 4.9%;\\n      --radius: 0.5rem;\\n      --chart-1: 12 76% 61%;\\n      --chart-2: 173 58% 39%;\\n      --chart-3: 197 37% 24%;\\n      --chart-4: 43 74% 66%;\\n      --chart-5: 27 87% 67%;\\n    }\\n\\n    .dark {\\n      --background: 222.2 84% 4.9%;\\n      --foreground: 210 40% 98%;\\n      --card: 222.2 84% 4.9%;\\n      --card-foreground: 210 40% 98%;\\n      --popover: 222.2 84% 4.9%;\\n      --popover-foreground: 210 40% 98%;\\n      --primary: 210 40% 98%;\\n      --primary-foreground: 222.2 47.4% 11.2%;\\n      --secondary: 217.2 32.6% 17.5%;\\n      --secondary-foreground: 210 40% 98%;\\n      --muted: 217.2 32.6% 17.5%;\\n      --muted-foreground: 215 20.2% 65.1%;\\n      --accent: 217.2 32.6% 17.5%;\\n      --accent-foreground: 210 40% 98%;\\n      --destructive: 0 62.8% 30.6%;\\n      --destructive-foreground: 210 40% 98%;\\n      --border: 217.2 32.6% 17.5%;\\n      --input: 217.2 32.6% 17.5%;\\n      --ring: 212.7 26.8% 83.9%;\\n      --chart-1: 220 70% 50%;\\n      --chart-2: 160 60% 45%;\\n      --chart-3: 30 80% 55%;\\n      --chart-4: 280 65% 60%;\\n      --chart-5: 340 75% 55%;\\n    }\\n  }\\n\\n  @layer base {\\n    \* {\\n      @apply border-border;\\n    }\\n    body {\\n      @apply bg-background text-foreground;\\n    }\\n  }"
ログイン後にコピー

Conclusion:

baseColor is returned from getRegistryBaseColor and then using fs.writeFile, css code is written to the file located at path provided by config.resolvedPaths.tailwindCss.

getRegistryBaseColor uses fetchRegistry to fetch the base color. An example baseColor.json is available at https://ui.shadcn.com/registry/colors/slate.json

components.json provides an option to configure prefix for your tailwind classes.

Want to learn how to build shadcn-ui/ui from scratch? Check out build-from-scratch

About me:

Website: https://ramunarasinga.com/

Linkedin: https://www.linkedin.com/in/ramu-narasinga-189361128/

Github: https://github.com/Ramu-Narasinga

Email: ramu.narasinga@gmail.com

Build shadcn-ui/ui from scratch

References:

  1. https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/commands/init.ts#L331C3-L356C4
  2. https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/utils/registry/index.ts#L64
  3. https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/utils/registry/index.ts#L139
  4. https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/utils/registry/schema.ts#L33
  5. https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/utils/transformers/transform-tw-prefix.ts#L191

以上がshadcn-ui/ui コードベース分析: shadcn-ui CLI はどのように機能しますか? — パート 3の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

JavaScriptエンジン:実装の比較 JavaScriptエンジン:実装の比較 Apr 13, 2025 am 12:05 AM

さまざまなJavaScriptエンジンは、各エンジンの実装原則と最適化戦略が異なるため、JavaScriptコードを解析および実行するときに異なる効果をもたらします。 1。語彙分析:ソースコードを語彙ユニットに変換します。 2。文法分析:抽象的な構文ツリーを生成します。 3。最適化とコンパイル:JITコンパイラを介してマシンコードを生成します。 4。実行:マシンコードを実行します。 V8エンジンはインスタントコンピレーションと非表示クラスを通じて最適化され、Spidermonkeyはタイプ推論システムを使用して、同じコードで異なるパフォーマンスパフォーマンスをもたらします。

Python vs. JavaScript:学習曲線と使いやすさ Python vs. JavaScript:学習曲線と使いやすさ Apr 16, 2025 am 12:12 AM

Pythonは、スムーズな学習曲線と簡潔な構文を備えた初心者により適しています。 JavaScriptは、急な学習曲線と柔軟な構文を備えたフロントエンド開発に適しています。 1。Python構文は直感的で、データサイエンスやバックエンド開発に適しています。 2。JavaScriptは柔軟で、フロントエンドおよびサーバー側のプログラミングで広く使用されています。

C/CからJavaScriptへ:すべてがどのように機能するか C/CからJavaScriptへ:すべてがどのように機能するか Apr 14, 2025 am 12:05 AM

C/CからJavaScriptへのシフトには、動的なタイピング、ゴミ収集、非同期プログラミングへの適応が必要です。 1)C/Cは、手動メモリ管理を必要とする静的に型付けられた言語であり、JavaScriptは動的に型付けされ、ごみ収集が自動的に処理されます。 2)C/Cはマシンコードにコンパイルする必要がありますが、JavaScriptは解釈言語です。 3)JavaScriptは、閉鎖、プロトタイプチェーン、約束などの概念を導入します。これにより、柔軟性と非同期プログラミング機能が向上します。

JavaScriptとWeb:コア機能とユースケース JavaScriptとWeb:コア機能とユースケース Apr 18, 2025 am 12:19 AM

Web開発におけるJavaScriptの主な用途には、クライアントの相互作用、フォーム検証、非同期通信が含まれます。 1)DOM操作による動的なコンテンツの更新とユーザーインタラクション。 2)ユーザーエクスペリエンスを改善するためにデータを提出する前に、クライアントの検証が実行されます。 3)サーバーとのリフレッシュレス通信は、AJAXテクノロジーを通じて達成されます。

JavaScript in Action:実際の例とプロジェクト JavaScript in Action:実際の例とプロジェクト Apr 19, 2025 am 12:13 AM

現実世界でのJavaScriptのアプリケーションには、フロントエンドとバックエンドの開発が含まれます。 1)DOM操作とイベント処理を含むTODOリストアプリケーションを構築して、フロントエンドアプリケーションを表示します。 2)node.jsを介してRestfulapiを構築し、バックエンドアプリケーションをデモンストレーションします。

JavaScriptエンジンの理解:実装の詳細 JavaScriptエンジンの理解:実装の詳細 Apr 17, 2025 am 12:05 AM

JavaScriptエンジンが内部的にどのように機能するかを理解することは、開発者にとってより効率的なコードの作成とパフォーマンスのボトルネックと最適化戦略の理解に役立つためです。 1)エンジンのワークフローには、3つの段階が含まれます。解析、コンパイル、実行。 2)実行プロセス中、エンジンはインラインキャッシュや非表示クラスなどの動的最適化を実行します。 3)ベストプラクティスには、グローバル変数の避け、ループの最適化、constとletsの使用、閉鎖の過度の使用の回避が含まれます。

Python vs. JavaScript:コミュニティ、ライブラリ、リソース Python vs. JavaScript:コミュニティ、ライブラリ、リソース Apr 15, 2025 am 12:16 AM

PythonとJavaScriptには、コミュニティ、ライブラリ、リソースの観点から、独自の利点と短所があります。 1)Pythonコミュニティはフレンドリーで初心者に適していますが、フロントエンドの開発リソースはJavaScriptほど豊富ではありません。 2)Pythonはデータサイエンスおよび機械学習ライブラリで強力ですが、JavaScriptはフロントエンド開発ライブラリとフレームワークで優れています。 3)どちらも豊富な学習リソースを持っていますが、Pythonは公式文書から始めるのに適していますが、JavaScriptはMDNWebDocsにより優れています。選択は、プロジェクトのニーズと個人的な関心に基づいている必要があります。

Python vs. JavaScript:開発環境とツール Python vs. JavaScript:開発環境とツール Apr 26, 2025 am 12:09 AM

開発環境におけるPythonとJavaScriptの両方の選択が重要です。 1)Pythonの開発環境には、Pycharm、Jupyternotebook、Anacondaが含まれます。これらは、データサイエンスと迅速なプロトタイピングに適しています。 2)JavaScriptの開発環境には、フロントエンドおよびバックエンド開発に適したnode.js、vscode、およびwebpackが含まれます。プロジェクトのニーズに応じて適切なツールを選択すると、開発効率とプロジェクトの成功率が向上する可能性があります。

See all articles