文档
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 命令来设置你的项目

npx 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

就是这样

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

npx shadcn@latest add button

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

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