首頁 > web前端 > js教程 > 主體

shadcn-ui/ui 程式碼庫分析:shadcn-ui CLI 是如何運作的? — 第 3 部分

王林
發布: 2024-07-19 09:58:03
原創
546 人瀏覽過

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中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!