隆重推出 Magic UI Pro - 50+ 区块和模板,助您在几分钟内构建精美着陆页。

命令面板

搜索要运行的命令...

文档
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"),
      },
    },
  });
};

运行命令行工具

运行 shadcn-ui 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

大功告成

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

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

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