ReactJS Vite TailwindCSS 样板项目教程

ReactJS Vite TailwindCSS 样板项目教程

reactjs-vite-tailwindcss-boilerplateThis is a boilerplate build with Vite, React 18, TypeScript, Vitest, Testing Library, TailwindCSS 3, Eslint and Prettier.项目地址:https://gitcode.com/gh_mirrors/re/reactjs-vite-tailwindcss-boilerplate

1. 项目的目录结构及介绍

reactjs-vite-tailwindcss-boilerplate/
├── public/
│   └── index.html
├── src/
│   ├── assets/
│   ├── components/
│   ├── App.tsx
│   ├── index.tsx
│   └── ...
├── .eslintrc.js
├── .prettierrc
├── tsconfig.json
├── vite.config.ts
└── package.json

目录结构介绍

  • public/: 存放公共资源,如 index.html
  • src/: 源代码目录,包含应用的主要代码。
    • assets/: 存放静态资源,如图片、字体等。
    • components/: 存放React组件。
    • App.tsx: 主应用组件。
    • index.tsx: 入口文件。
  • .eslintrc.js: ESLint配置文件。
  • .prettierrc: Prettier配置文件。
  • tsconfig.json: TypeScript配置文件。
  • vite.config.ts: Vite配置文件。
  • package.json: 项目依赖和脚本配置。

2. 项目的启动文件介绍

入口文件

src/index.tsx 是项目的入口文件,负责渲染React应用到DOM中。

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

主应用组件

src/App.tsx 是主应用组件,通常包含应用的路由和全局状态管理。

import React from 'react';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <p>
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

3. 项目的配置文件介绍

ESLint配置

.eslintrc.js 文件用于配置ESLint,确保代码风格一致。

module.exports = {
  extends: [
    'plugin:react/recommended',
    'plugin:react/jsx-runtime',
    'prettier',
  ],
  parserOptions: {
    ecmaVersion: 2021,
    sourceType: 'module',
    ecmaFeatures: {
      jsx: true,
    },
  },
  settings: {
    react: {
      version: 'detect',
    },
  },
  rules: {
    // 自定义规则
  },
};

Prettier配置

.prettierrc 文件用于配置Prettier,格式化代码。

{
  "singleQuote": true,
  "trailingComma": "all",
  "printWidth": 80
}

TypeScript配置

tsconfig.json 文件用于配置TypeScript编译选项。

{
  "compilerOptions": {
    "target": "esnext",
    "lib": ["dom", "esnext"],
    "allowJs": true,
    "checkJs": true,
    "jsx": "react-jsx",
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true
  },
  "include": ["src"]
}

Vite配置

`vite.config

reactjs-vite-tailwindcss-boilerplateThis is a boilerplate build with Vite, React 18, TypeScript, Vitest, Testing Library, TailwindCSS 3, Eslint and Prettier.项目地址:https://gitcode.com/gh_mirrors/re/reactjs-vite-tailwindcss-boilerplate

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用Tailwind CSS可以快速构建定制化的UI界面,以下是在Vite项目中使用Tailwind CSS的详细步骤和过程: 1. 创建新的Vite项目。 ```bash npm init vite@latest my-project ``` 2. 安装Tailwind CSS和Autoprefixer依赖包。 ```bash npm install tailwindcss autoprefixer -D ``` 3. 创建Tailwind CSS配置文件。 ```bash npx tailwindcss init ``` 运行该命令后,会在项目根目录下生成一个`tailwind.config.js`文件,用于配置Tailwind CSS的各种选项。 4. 在CSS文件中引入Tailwind CSS。 在你的CSS文件中,使用`@tailwind`关键字来引入Tailwind CSS。例如: ```css @tailwind base; @tailwind components; @tailwind utilities; ``` 5. 在Vite配置文件中配置PostCSS。 在`vite.config.js`文件中配置PostCSS,启用Tailwind CSS和Autoprefixer插件。例如: ```js import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; import autoprefixer from 'autoprefixer'; import tailwindcss from 'tailwindcss'; export default defineConfig({ plugins: [vue()], css: { postcss: { plugins: [ tailwindcss, autoprefixer, ], }, }, }); ``` 6. 使用`@apply`指令来自定义样式。 在`tailwind.config.js`中,你可以定义自己的颜色、字体、间距等等。例如: ```js module.exports = { theme: { extend: { colors: { 'my-color': '#123456', }, fontFamily: { 'my-font': ['Arial', 'sans-serif'], }, spacing: { 'my-spacing': '24px', }, }, }, variants: {}, plugins: [], }; ``` 在CSS文件中,你可以使用`@apply`指令来引用定义好的自定义样式。例如: ```css .my-text { @apply text-2xl font-my-font text-my-color; } ``` 这里的`.my-text`样式会继承Tailwind CSS中定义好的`text-2xl`、`font-my-font`和`text-my-color`样式。 至此,你就可以在Vite项目中使用Tailwind CSS来构建UI界面了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伍冠跃Barbara

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值