111k

Vite

为 Vite 安装和配置 shadcn/ui。

选择适合你起点的方案。

使用 shadcn/create

构建你的预设

打开 shadcn/create 并以可视化方式构建你的预设。选择样式、颜色、字体、图标等。

打开 shadcn/create

创建项目

点击 Create Project,选择你的包管理器,然后复制生成的命令。

生成的命令看起来大致如下:

pnpm dlx shadcn@latest init --preset [CODE] --template vite

最终命令会包含你选择的选项,例如 --base--monorepo--rtl

添加组件

向你的项目添加 Card 组件:

pnpm dlx shadcn@latest add card

如果你创建了 monorepo,请在 apps/web 目录中运行命令,或者从仓库根目录指定 workspace:

pnpm dlx shadcn@latest add card -c apps/web

上面的命令会把 Card 组件添加到你的项目中。之后你可以像这样导入它:

src/App.tsx
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from "@/components/ui/card"
 
function App() {
  return (
    <Card className="max-w-sm">
      <CardHeader>
        <CardTitle>项目概览</CardTitle>
        <CardDescription>
          跟踪你的 Vite 应用的进展和最近活动。
        </CardDescription>
      </CardHeader>
      <CardContent>
        你的设计系统已经就绪。开始构建下一个组件吧。
      </CardContent>
    </Card>
  )
}
 
export default App

如果你创建了 monorepo,请改为更新 apps/web/src/App.tsx,并从 @workspace/ui/components/card 导入。

使用 CLI

创建项目

运行 init 命令以为新的 Vite 项目创建脚手架。按照提示配置项目:base、preset、monorepo 等。

pnpm dlx shadcn@latest init -t vite

对于 monorepo 项目,请使用 --monorepo 标志:

pnpm dlx shadcn@latest init -t vite --monorepo

添加组件

向你的项目添加 Card 组件:

pnpm dlx shadcn@latest add card

如果你创建了 monorepo,请在 apps/web 目录中运行命令,或者从仓库根目录指定 workspace:

pnpm dlx shadcn@latest add card -c apps/web

上面的命令会把 Card 组件添加到你的项目中。之后你可以像这样导入它:

src/App.tsx
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from "@/components/ui/card"
 
function App() {
  return (
    <Card className="max-w-sm">
      <CardHeader>
        <CardTitle>项目概览</CardTitle>
        <CardDescription>
          跟踪你的 Vite 应用的进展和最近活动。
        </CardDescription>
      </CardHeader>
      <CardContent>
        你的设计系统已经就绪。开始构建下一个组件吧。
      </CardContent>
    </Card>
  )
}
 
export default App

如果你创建了 monorepo,请改为更新 apps/web/src/App.tsx,并从 @workspace/ui/components/card 导入。

现有项目

创建项目

如果你需要一个新的 Vite 项目,请先创建一个,并选择 React + TypeScript 模板。否则,跳过此步骤。

pnpm create vite@latest

添加 Tailwind CSS

如果你的项目已经配置了 Tailwind CSS,可以跳过此步骤。

pnpm add tailwindcss @tailwindcss/vite

src/index.css 中的所有内容替换为以下内容:

src/index.css
@import "tailwindcss";

编辑 tsconfig.json 文件

如果你的项目已经配置了 @/* 别名,可以跳过此步骤。

Vite 会把 TypeScript 配置拆分到多个文件中。请在 tsconfig.jsontsconfig.app.jsoncompilerOptions 部分添加 baseUrlpaths 属性:

tsconfig.json
{
  "files": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.node.json"
    }
  ],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}

编辑 tsconfig.app.json 文件

tsconfig.app.json 中添加相同的别名,这样编辑器就能解析导入:

tsconfig.app.json
{
  "compilerOptions": {
    // ...
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    }
    // ...
  }
}

更新 vite.config.ts

安装 @types/node,并更新 vite.config.ts,让 Vite 能解析 @ 别名:

pnpm add -D @types/node
vite.config.ts
import path from "path"
import tailwindcss from "@tailwindcss/vite"
import react from "@vitejs/plugin-react"
import { defineConfig } from "vite"
 
// https://vite.dev/config/
export default defineConfig({
  plugins: [react(), tailwindcss()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
})

运行 CLI

运行 shadcn init 命令,为你的项目设置 shadcn/ui:

pnpm dlx shadcn@latest init

添加组件

现在你可以开始向项目中添加组件了。

pnpm dlx shadcn@latest add button

上面的命令会把 Button 组件添加到你的项目中。之后你可以像这样导入它:

src/App.tsx
import { Button } from "@/components/ui/button"
 
function App() {
  return (
    <div className="flex min-h-svh flex-col items-center justify-center">
      <Button>点击我</Button>
    </div>
  )
}
 
export default App