首页 web前端 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中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1666
14
CakePHP 教程
1425
52
Laravel 教程
1328
25
PHP教程
1273
29
C# 教程
1253
24
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引入闭包、原型链和Promise等概念,增强了灵活性和异步编程能力。

JavaScript和Web:核心功能和用例 JavaScript和Web:核心功能和用例 Apr 18, 2025 am 12:19 AM

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

JavaScript在行动中:现实世界中的示例和项目 JavaScript在行动中:现实世界中的示例和项目 Apr 19, 2025 am 12:13 AM

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

了解JavaScript引擎:实施详细信息 了解JavaScript引擎:实施详细信息 Apr 17, 2025 am 12:05 AM

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

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