Docs
Gatsby

Gatsby

安装和配置 Gatsby。

创建项目

首先使用 create-gatsby 创建一个新的 Gatsby 项目:

npm init gatsby

配置你的 Gatsby 项目以使用 TypeScript 和 Tailwind CSS

系统会询问您一些问题以配置您的项目:

✔ 您想给您的网站取什么名字?
· your-app-name
✔ 您想给您的网站创建的文件夹命名为?
· your-app-name
✔ 您将使用 JavaScript 还是 TypeScript?
· TypeScript
✔ 您会使用 CMS 吗?
· 选择您想要的
✔ 您想安装一个样式系统吗?
· Tailwind CSS
✔ 您想通过其他插件安装额外功能吗?
· 选择您想要的
✔ 我们可以这样做吗? (Y/n) · 是

编辑 tsconfig.json 文件

tsconfig.json 文件中添加以下代码以解析路径:

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

创建 gatsby-node.ts 文件

如果项目根目录下不存在 gatsby-node.ts 文件,请创建一个,并在该文件中添加以下代码,以便您的应用可以解析路径:

import * as path from "path"
 
export const onCreateWebpackConfig = ({ actions }) => {
  actions.setWebpackConfig({
    resolve: {
      alias: {
        "@/components": path.resolve(__dirname, "src/components"),
        "@/lib/utils": path.resolve(__dirname, "src/lib/utils"),
      },
    },
  })
}

运行 CLI

运行 shadcn-ui 初始化命令来设置您的项目:

pnpm dlx shadcn@latest init

配置 components.json

系统会询问您一些问题以配置 components.json

您想使用 TypeScript(推荐)吗? no / yes
您想使用哪种样式? › 默认
您想使用哪种颜色作为基础颜色? › Slate
您的全球 CSS 文件在哪里? › › ./src/styles/globals.css
您想为颜色使用 CSS 变量吗? › no / yes
您的 tailwind.config.js 位于哪里? › tailwind.config.js
配置组件的导入别名: › @/components
配置工具的导入别名: › @/lib/utils
您正在使用 React Server 组件吗? › no

就这样

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

pnpm dlx shadcn@latest add button

上述命令将 Button 组件添加到您的项目中。您可以像这样导入它:

import { Button } from "@/components/ui/button"
 
export default function Home() {
  return (
    <div>
      <Button>点击我</Button>
    </div>
  )
}