文档
Gatsby

Gatsby

安装和配置 Gatsby。

创建项目

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

npm init gatsby

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

系统会询问您几个问题来配置您的项目

✔ What would you like to call your site?
· your-app-name
✔ What would you like to name the folder where your site will be created?
· your-app-name
✔ Will you be using JavaScript or TypeScript?
· TypeScript
✔ Will you be using a CMS?
· Choose whatever you want
✔ Would you like to install a styling system?
· Tailwind CSS
✔ Would you like to install additional features with other plugins?
· Choose whatever you want
✔ Shall we do this? (Y/n) · Yes

编辑 tsconfig.json 文件

将以下代码添加到 tsconfig.json 文件以解析路径

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

创建 gatsby-node.ts 文件

在您的项目根目录创建一个 gatsby-node.ts 文件(如果它尚不存在),并将以下代码添加到 gatsby-node 文件中,以便您的应用程序可以解析路径

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 init 命令来设置您的项目

pnpm dlx shadcn@latest init

配置 components.json

系统会询问您几个问题来配置 components.json

Would you like to use TypeScript (recommended)? no / yes
Which style would you like to use? › Default
Which color would you like to use as base color? › Slate
Where is your global CSS file? › › ./src/styles/globals.css
Do you want to use CSS variables for colors? › no / yes
Where is your tailwind.config.js located? › tailwind.config.js
Configure the import alias for components: › @/components
Configure the import alias for utils: › @/lib/utils
Are you using React Server Components? › no

就是这样

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

pnpm dlx shadcn@latest add button

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

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