110k

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 初始化命令来设置你的项目:

pnpm dlx shadcn@latest init

完成

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

pnpm dlx shadcn@latest add button

上面的命令会向你的项目添加 Button 组件。然后你可以这样导入它:

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