🌐 Nodejs.cn

在 monorepo 中使用 shadcn/ui 组件和 CLI。

直到现在,在 monorepo 中使用 shadcn/ui 都有点麻烦。你可以使用 CLI 添加组件,但你必须管理组件的安装位置并手动修复导入路径。

🌐 Until now, using shadcn/ui in a monorepo was a bit of a pain. You could add components using the CLI, but you had to manage where the components were installed and manually fix import paths.

借助 CLI 中的新 monorepo 支持,我们使在 monorepo 中使用 shadcn/ui 变得容易得多。

🌐 With the new monorepo support in the CLI, we've made it a lot easier to use shadcn/ui in a monorepo.

CLI现在能够理解单仓库结构,并会将组件、依赖和注册表依赖安装到正确的路径,并为你处理导入。

🌐 The CLI now understands the monorepo structure and will install the components, dependencies and registry dependencies to the correct paths and handle imports for you.

入门

🌐 Getting started

创建一个新的 monorepo 项目

🌐 Create a new monorepo project

要创建一个新的 monorepo 项目,请使用 --monorepo 标志运行 init 命令。

🌐 To create a new monorepo project, run the init command with the --monorepo flag.

pnpm dlx shadcn@latest init --monorepo

然后选择你想要使用的模板。

🌐 Then select the template you want to use.

? Select a template ›
   Next.js
    Vite
    TanStack Start
    React Router
    Astro

这将创建一个包含两个工作区的新单体仓库项目:webui,并使用 Turborepo 作为构建系统。

🌐 This will create a new monorepo project with two workspaces: web and ui, and Turborepo as the build system.

一切都为你设置好了,因此你可以开始向项目添加组件。

🌐 Everything is set up for you, so you can start adding components to your project.

将组件添加到你的项目

🌐 Add components to your project

要向你的项目添加组件,请在 应用路径中 运行 add 命令。

🌐 To add components to your project, run the add command in the path of your app.

cd apps/web
pnpm dlx shadcn@latest add [COMPONENT]

CLI 会确定你正在添加的组件类型,并将正确的文件安装到正确的路径。

🌐 The CLI will figure out what type of component you are adding and install the correct files to the correct path.

例如,如果你运行 npx shadcn@latest add button,CLI 将在 packages/ui 下安装按钮组件,并更新 apps/web 中组件的导入路径。

🌐 For example, if you run npx shadcn@latest add button, the CLI will install the button component under packages/ui and update the import path for components in apps/web.

如果你运行 npx shadcn@latest add login-01,CLI 将在 packages/ui 下安装 buttonlabelinputcard 组件,并在 apps/web/components 下安装 login-form 组件。

🌐 If you run npx shadcn@latest add login-01, the CLI will install the button, label, input and card components under packages/ui and the login-form component under apps/web/components.

导入组件

🌐 Importing components

你可以按如下方式从 @workspace/ui 包中导入组件:

🌐 You can import components from the @workspace/ui package as follows:

import { Button } from "@workspace/ui/components/button"

你也可以从 @workspace/ui 包中导入 hooks 和工具。

🌐 You can also import hooks and utilities from the @workspace/ui package.

import { useTheme } from "@workspace/ui/hooks/use-theme"
import { cn } from "@workspace/ui/lib/utils"

文件结构

🌐 File Structure

当你创建一个新的 monorepo 项目时,CLI 将创建以下文件结构:

🌐 When you create a new monorepo project, the CLI will create the following file structure:

apps
└── web         # Your app goes here.
    ├── app
    │   └── page.tsx
    ├── components
    │   └── login-form.tsx
    ├── components.json
    └── package.json
packages
└── ui          # Your components and dependencies are installed here.
    ├── src
    │   ├── components
    │   │   └── button.tsx
    │   ├── hooks
    │   ├── lib
    │   │   └── utils.ts
    │   └── styles
    │       └── globals.css
    ├── components.json
    └── package.json
package.json
turbo.json

要求

🌐 Requirements

  1. 每个工作区必须有一个 components.json 文件。package.json 文件告诉 npm 如何安装依赖。components.json 文件告诉 CLI 如何以及在何处安装组件。
  2. components.json 文件必须正确定义工作区的别名。这告诉 CLI 如何导入组件、钩子、工具等。
apps/web/components.json
{
  "$schema": "https://shadcn.nodejs.cn/schema.json",
  "style": "radix-nova",
  "rsc": true,
  "tsx": true,
  "tailwind": {
    "config": "",
    "css": "../../packages/ui/src/styles/globals.css",
    "baseColor": "neutral",
    "cssVariables": true
  },
  "iconLibrary": "lucide",
  "aliases": {
    "components": "@/components",
    "hooks": "@/hooks",
    "lib": "@/lib",
    "utils": "@workspace/ui/lib/utils",
    "ui": "@workspace/ui/components"
  }
}
packages/ui/components.json
{
  "$schema": "https://shadcn.nodejs.cn/schema.json",
  "style": "radix-nova",
  "rsc": true,
  "tsx": true,
  "tailwind": {
    "config": "",
    "css": "src/styles/globals.css",
    "baseColor": "neutral",
    "cssVariables": true
  },
  "iconLibrary": "lucide",
  "aliases": {
    "components": "@workspace/ui/components",
    "utils": "@workspace/ui/lib/utils",
    "hooks": "@workspace/ui/hooks",
    "lib": "@workspace/ui/lib",
    "ui": "@workspace/ui/components"
  }
}
  1. 确保你在两个 components.json 文件中具有相同的 styleiconLibrarybaseColor
  2. 对于 Tailwind CSS v4,在 components.json 文件中将 tailwind 配置留空。

通过遵循这些要求,CLI 将能够将 ui 组件、块、库和钩子安装到正确的路径并为你处理导入。

🌐 By following these requirements, the CLI will be able to install ui components, blocks, libs and hooks to the correct paths and handle imports for you.