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:
- Ensure all resolved paths directories exist.
- Write tailwind config.
- Write css file.
- Write cn file.
- 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:
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:
- https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/commands/init.ts#L331C3-L356C4
- https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/utils/registry/index.ts#L64
- https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/utils/registry/index.ts#L139
- https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/utils/registry/schema.ts#L33
- https://github.com/shadcn-ui/ui/blob/main/packages/cli/src/utils/transformers/transform-tw-prefix.ts#L191
The above is the detailed content of shadcn-ui/ui codebase analysis: How does shadcn-ui CLI work? — Part 3. For more information, please follow other related articles on the PHP Chinese website!

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.

How to send task notifications in Quartz In advance When using the Quartz timer to schedule a task, the execution time of the task is set by the cron expression. Now...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor