react-typescript-custom
2024年04月29日
项目架构
website # 模板目录结构
├── .husky
│ └── commit-msg
│ └── pre-commit
├── .vscode
│ └── settings.json
├── src
│ └── assets
│ └── pages
│ └── styles
│ └── App.tsx
│ └── index.tsx
│ └── type-env.d.ts
│ └── type-resource.d.ts
├── .editorconfig
├── .eslintignore
├── .eslintrc.js
├── .gitignore
├── .lintstagedrc.js
├── .prettierignore
├── .prettierrc.js
├── .stylelintignore
├── .stylelintrc.js
├── commitlint.config.js
├── index.html
├── package.json
├── tsconfig.json
├── vite.config.ts
├── ...
步骤一、Vite
1. 安装依赖
yarn add vite @vitejs/plugin-react -D
2. 构建入口文件、初始页面
根目录/index.html
: 入口文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite-React 模板</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="./src/index.tsx"></script>
</body>
</html>
根目录/src/index.ts
: react 入口文件
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './styles/index';
ReactDOM.createRoot(document.getElementById('app')!).render(<App />);
根目录/src/App.tsx
: react 初始页面
import React, {useState} from 'react';
import logo from '@/assets/svg/logo.svg';
function App() {
const [title, setTitle] = useState<string>('Vite-React 模板');
const name: string = 'st';
const age: number = 200;
return (
<div>
<h3>{title}</h3>
<img src={logo} alt="" />
</div>
);
}
export default App;
3. 支持 scss、less 等文件
Vite 天然支持 scss
、less
等文件,只需要安装响应依赖即可
yarn add scss less -D
步骤二、VsCode
1. 配置 .vscode
.vscode/settings.json
文件
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"source.fixAll.stylelint": true
},
"eslint.options": {
"extensions": [".ts", ".tsx"]
},
"eslint.validate": ["typescript", "typescriptreact"],
"stylelint.validate": ["css", "scss"]
}
2. 配置 .editorconfig 文件
[*.{ts,tsx}]
indent_style = space
indent_size = 4
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 200