保姆级教学——React+Ts +Vite标准化框架搭建
保姆级教学——React+Ts +Vite标准化框架搭建
关联问题:如何优化框架性能Vite有何优势能用于生产环境吗
「这是我参与2022首次更文挑战的第3天,活动详情查看:2022首次更文挑战」。
前言
大家好今天由我来给大家搭建一个React+Ts +Vite的标准化框架。在日常开发中我们大多数时间都在维护老项目,但偶尔也会需要自己搭建框架。当我们需要为团队搭建一个React项目时,我觉得参考这篇文章就够了。
项目初始化
创建vite(构建工具)项目
兼容性注意 Vite 需要 Node.js 版本 >= 12.0.0 Node.js 最新版本
使用 Yarn
代码解读
复制代码
yarn create @vitejs/app
使用 NPM
代码解读
复制代码
npm init @vitejs/app
执行完命令后我们需要选择模板,这边我们选择React
是否是TS我们选择是
查看目录
使用VSCode打开项目我们可以看到基本的构成
启动项目
代码解读
复制代码
cd [你的项目名称路径]
yarn 或 npm install
yarn dev
可以看到启动的特别快
访问项目
浏览器访问http://localhost:3000/便可到达我们项目的主页。
配置Typescript
Typescript的文件位置
tsconfig.json
代码解读
复制代码
{
"compilerOptions": {
"target": "es5", // 指定 ECMAScript 版本
"lib": [
"dom",
"dom.iterable",
"esnext"
], // 要包含在编译中的依赖库文件列表
"allowJs": true, // 允许编译 JavaScript 文件
"skipLibCheck": true, // 跳过所有声明文件的类型检查
"esModuleInterop": true, // 禁用命名空间引用 (import * as fs from "fs") 启用 CJS/AMD/UMD 风格引用 (import fs from "fs")
"allowSyntheticDefaultImports": true, // 允许从没有默认导出的模块进行默认导入
"strict": true, // 启用所有严格类型检查选项
"forceConsistentCasingInFileNames": true, // 不允许对同一个文件使用不一致格式的引用
"module": "esnext", // 指定模块代码生成
"moduleResolution": "node", // 使用 Node.js 风格解析模块
"resolveJsonModule": true, // 允许使用 .json 扩展名导入的模块
"noEmit": true, // 不输出(意思是不编译代码,只执行类型检查
"jsx": "react", // 在.tsx文件中支持JSX
"sourceMap": true, // 生成相应的.map文件
"declaration": true, // 生成相应的.d.ts文件
"noUnusedLocals": true, // 报告未使用的本地变量的错误
"noUnusedParameters": true, // 报告未使用参数的错误
"experimentalDecorators": true, // 启用对ES装饰器的实验性支持
"incremental": true, // 通过从以前的编译中读取/写入信息到磁盘上的文件来启用增量编译
"noFallthroughCasesInSwitch": true
},
"include": [
"src/**/*" // *** TypeScript文件应该进行类型检查 ***
],
"exclude": ["node_modules", "build"] // *** 不进行类型检查的文件 ***}
大家可以去除注释后放到文件中或者参考自己公司规范。(不懂Typescript?可以参考我这篇文章《一篇文章带你了解Typescript那些事儿》)
项目配置
我们找到 vite.config.ts
vite.config.ts
代码解读
复制代码
import { defineConfig } from "vite";
import reactRefresh from "@vitejs/plugin-react-refresh";
import * as path from "path";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [reactRefresh()],
// 配置路径别名
resolve: {
alias: {
"@": path.resolve(__dirname, "src"),
},
},
server: {
port: 3000,
proxy: {
"/api": {
target: "https://yourBaseUrl",
changeOrigin: true,
cookieDomainRewrite: "",
secure: false,
},
},
},
});
我们这边配置路径别名后接口代理
Eslint 配置
ESLint 是一个开源的JavaScript验证工具,可以让我们在编译时就发现我们代码的错误。
安装eslint
代码解读
复制代码
npm install eslint -g
安装eslint-plugin-react(不用加-g)
代码解读
复制代码
npm install eslint-plugin-react
安装babel-eslint
如果用到了es6的新语法, 需要安装babel-eslint,不然会把箭头函数识别成错误
代码解读
复制代码
npm install babel-eslint
.eslintrc.json配置
在项目的根目录创建配置文件.eslintrc.json
eslint会根据.eslintrc.json定义的规则进行代码检测(在mac中的.开头的文件为隐藏文件);
eslint官方给出的一些有关react配置的文档
.eslintrc.json
代码解读
复制代码
{
//文件名 .eslintrc.json
"env": {
"browser": true,
"es6": true,
"node": true,
"commonjs": true
},
"extends": "eslint:recommended",
"installedESLint": true,
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
"jsx": true,
"arrowFunctions": true,
"classes": true,
"modules": true,
"defaultParams": true
},
"sourceType": "module"
},
"parser": "babel-eslint",
"plugins": ["react"],
"rules": {
"linebreak-style": ["error", "unix"],
//"semi": ["error", "always"],
"no-empty": 0,
"comma-dangle": 0,
"no-unused-vars": 0,
"no-console": 0,
"no-const-assign": 2,
"no-dupe-class-members": 2,
"no-duplicate-case": 2,
"no-extra-parens": [2, "functions"],
"no-self-compare": 2,
"accessor-pairs": 2,
"comma-spacing": [
2,
{
"before": false,
"after": true
}
],
"constructor-super": 2,
"new-cap": [
2,
{
"newIsCap": true,
"capIsNew": false
}
],
"new-parens": 2,
"no-array-constructor": 2