Vite + React 19 + TypeScript 前端项目
| 项目 | 说明 |
|---|---|
| 框架 | React 19.2 |
| 构建工具 | Vite 8.2 |
| 语言 | TypeScript 6.0 |
| 包管理器 | yarn(镜像源:npmmirror) |
| 默认端口 | 5173 |
# 在项目根目录下执行
yarn dev启动成功后访问:http://localhost:5173
yarn build构建产物输出到 dist/ 目录。
yarn previewyarn dev --port 3000yarn dev --hostyarn install# 示例:添加 axios
yarn add axios# 示例:添加 tailwindcss
yarn add -D tailwindcss# 示例:移除 axios
yarn remove axiosyarn upgradeyarn cache clean
rm -r node_modules
yarn installyarn lintnpx tsc --noEmit- 在代码中添加
debugger;语句或使用console.log()输出日志 - 打开浏览器开发者工具(F12)
- 切换到 Sources 面板,在断点处调试
- 安装 React Developer Tools 浏览器扩展
- 打开开发者工具中的 Components 和 Profiler 面板
- 可查看组件树、Props、State 及渲染性能
# 查看 Vite 详细日志
yarn dev --debug
# 强制重新构建依赖预构建缓存
yarn dev --force| 问题 | 排查方法 |
|---|---|
| 端口被占用 | 使用 yarn dev --port 3000 指定其他端口 |
| 依赖安装失败 | 检查 yarn 镜像源:yarn config get registry,应为 https://registry.npmmirror.com |
| HMR 不生效 | 清除缓存:yarn dev --force |
| 类型报错 | 执行 npx tsc --noEmit 检查 TypeScript 类型 |
| 构建失败 | 执行 yarn build 查看完整错误日志 |
my-react-app/
├── src/
│ ├── App.tsx # 根组件
│ ├── App.css # 根组件样式
│ ├── main.tsx # 入口文件
│ └── index.css # 全局样式
├── public/ # 静态资源目录
├── index.html # HTML 模板
├── vite.config.ts # Vite 配置
├── tsconfig.json # TypeScript 配置
└── package.json # 项目配置
If you are developing a production application, we recommend enabling type-aware lint rules by installing oxlint-tsgolint and editing .oxlintrc.json:
{
"$schema": "./node_modules/oxlint/configuration_schema.json",
"plugins": ["react", "typescript", "oxc"],
"options": {
"typeAware": true
},
"rules": {
"react/rules-of-hooks": "error",
"react/only-export-components": ["warn", { "allowConstantExport": true }]
}
}See the Oxlint rules documentation for the full list of rules and categories.