網站首頁 編程語言 正文
使用 create-react-app 初始化
# 使用了 create-react-app 的 typescript 模板
yarn create react-app my-big-screen --template typescript
# 初始化之后進入 my-big-screen 文件目錄下執行
yarn start
# localhost:3000 就能看到 react 的 hello 頁面了
引入 antd UI庫
# 在 根目錄 下
yarn add antd
使用 craco 插件來自定義配置
安裝 craco
yarn add @craco/craco
修改 package.json 文件如下
# 原本 "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" } # 修改之后 "scripts": { "start": "craco start", "build": "craco build", "test": "craco test", "eject": "craco eject" }
在根目錄添加 craco.config.js 文件
module.exports = {}
自定義 antd 主題,使用 less 作為 css 預處理器
安裝 craco-antd
yarn add craco-antd
修改 craco.config.js 文件
const CracoAntDesignPlugin = require('craco-antd');
module.exports = {
plugins: [
{
plugin: CracoAntDesignPlugin,
options: {
customizeTheme: {
'@primary-color': '#1DA57A',
},
},
},
],
};
craco-less .module.less 模擬vue組件中style的scope 功能
craco-less 依賴 已經內置在 @craco/craco 依賴中,無需主動安裝
配置 craco.config.js 文件
const CracoAntDesignPlugin = require('craco-antd')
const CracoLessPlugin = require('craco-less')
const { loaderByName } = require('@craco/craco')
module.exports = {
plugins: [
{
plugin: CracoAntDesignPlugin,
options: {
customizeTheme: {
'@primary-color': '#1DA57A'
}
}
},
{
plugin: CracoLessPlugin,
options: {
modifyLessRule(lessRule, context) {
// You have to exclude these file suffixes first,
// if you want to modify the less module's suffix
lessRule.exclude = /\.m\.less$/
return lessRule
},
modifyLessModuleRule(lessModuleRule, context) {
// Configure the file suffix
lessModuleRule.test = /\.m\.less$/
// Configure the generated local ident name.
const cssLoader = lessModuleRule.use.find(loaderByName('css-loader'))
cssLoader.options.modules = {
localIdentName: '[local]_[hash:base64:5]'
}
return lessModuleRule
}
}
}
]
}
配置 * ./src/react-app-env.d.ts * 文件
/// <reference types="react-scripts" />
// 定義在 全局的 變量 然后就可以 window.* 進行訪問了
interface Window {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any
AMap: any
}
declare module "*.module.less" {
const classes: { readonly [key: string]: string };
export default classes;
}
使用示例(類似vue scope 的效果)
# index.module.less 文件命名
app {
font-size: 40px;
color: #f00;
}
# 在組件中引入
import styles from './index.module.less'
<div className={styles['app']}>hello</div>
使用 ESLint Prettier 兩個插件來做代碼格式校驗和自動格式化
以上操作版本記錄
{
"name": "xxx",
"version": "0.1.0",
"private": true,
"dependencies": {
"@craco/craco": "^6.2.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/react-router-dom": "^5.1.8",
"@typescript-eslint/eslint-plugin": "^4.30.0",
"@typescript-eslint/parser": "^4.30.0",
"antd": "^4.16.13",
"craco-antd": "^1.19.0",
"eslint": "^7.32.0",
"eslint-config-alloy": "^4.3.0",
"eslint-loader": "^4.0.2",
"eslint-plugin-react": "^7.25.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.4",
"react-router-dom": "^5.2.1",
"react-scripts": "4.0.3",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "set PORT=3090 && craco start",
"build": "craco build",
"test": "craco test",
"eject": "craco eject",
"lint": "eslint '{src}/**/*.{ts,tsx,js,jsx}'",
"lint_fix": "eslint {src} --ext .ts,.tsx,.js,jsx --fix"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
原文鏈接:https://www.jianshu.com/p/071ab1721a8b
相關推薦
- 2022-06-28 python神經網絡學習利用PyTorch進行回歸運算_python
- 2022-07-12 git如何上傳本地項目
- 2022-06-18 Elasticsearch之文檔批量操作示例_其它綜合
- 2022-07-22 如何測試webservice接口
- 2023-10-16 基于lodop實現web端打印分頁樣式自定義配置需求
- 2022-04-15 python3?cmp實現方式_python
- 2023-01-13 Go?cobra庫使用教程_Golang
- 2022-04-30 詳解Matlab如何繪制小提琴圖_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支