Docs
Gatsby

Gatsby

安装并配置 Gatsby。

创建项目

¥Create project

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

¥Start by creating a new Gatsby project using create-gatsby:

npm init gatsby

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

¥Configure your Gatsby project to use TypeScript and Tailwind CSS

你将被问到几个问题来配置你的项目:

¥You will be asked a few questions to configure your project:

✔ 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 文件

¥Edit tsconfig.json file

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

¥Add the following code to the tsconfig.json file to resolve paths:

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

创建 gatsby-node.ts 文件

¥Create gatsby-node.ts file

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

¥Create a gatsby-node.ts file at the root of your project if it doesn’t already exist, and add the code below to the gatsby-node file so your app can resolve paths:

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

¥Run the CLI

运行 shadcn-ui init 命令来设置你的项目:

¥Run the shadcn-ui init command to setup your project:

pnpm dlx shadcn@latest init

配置 components.json

¥Configure components.json

你将被问到几个问题来配置 components.json

¥You will be asked a few questions to configure 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

就是这样

¥That's it

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

¥You can now start adding components to your project.

pnpm dlx shadcn@latest add button

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

¥The command above will add the Button component to your project. You can then import it like this:

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