react 项目配置
自己搭了一套 react 的的后台,下面记录了我一步步搭建的过程,希望对大家有所帮助。
文章主要描述了下面几个部分
- 项目的结构
- 项目的搭建过程
- webpack 的多环境配置、loader配置、webserver配置等
- babel 的基本配置
- eslint 的基本配置
项目结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65|
|- config // webpack 相关配置
| |- webpack.common.js // 通用的配置
| |- webpack.dev.js // 发展的配置
| |- webpack.prod.js // 生产的配置
|
|- dist // 放置打包好的文件
|- public // 放置html模板以及图标等
|- src
| |- apis // apis 接口文件
| | |- brand.js
| | |- channel.js
| | |- ...
| |
| |- assets // 放置通用的资源
| | |- images
| | |- icons
| |
| |- components // 通用的组件
| | |- PageLoading
| |
| |- config
| | |- menu.js // 菜单配置
| | |- routes.js // 路由配置
| |
| |- layouts // 布局组件
| | |- BasicLayout // 基本布局
| | | |- components // 组成布局的组件
| | | |- index.jsx // 布局的主要结构文件
| | | |- index.module.scss // 布局对应的scss
| | |-...
| |
| |- pages // 页面文件
| | |- OrderManage
| | |- ...
| |
| |- stores
| | |- actions
| | | |- index.js // 触发 actions 的常量名列表
| | | |- menu.js // 对应的 actions
| | |
| | |- reducers
| | | |- index.js // 合并各个模块的 reducer,输入根的 reducer
| | | |- menu.js // 对应模块的 reducer
| | |
| | |- index.js // 输出根据 rootReducer 生成 store
| |
| |- utils // 各种工具
| | |- date.js // 日期格式化相关的工具
| | |- request.js // 封装好的 axios 组件
| |
| |- index.js // 入口文件
| |- router.jsx // 生成路由组件
|
|- test // 测试文件
| |- unit // 单元测试
|
|- .babelrc // babel 相关配置
|- .editourconfig // 编辑器的相关配置,编辑器会根据此格式化文件
|- .eslintrc.js // eslint 的配置文件
|- .gitignore // git 忽略追踪的配置文件
|- jsconfig.josn // js的配置文件,可以让别名 alias 得到识别
|- package.json // 配置文件
|- postcss.config.js // postcss 配置文件
|- README.md // Readme.md 文件
项目搭建
初始化项目
1 | npm init |
安装webpack
1 | npm install --save-dev webpack |
webpack 4+ 需要安装 CLI
1 | npm install --save-dev webpack-cli |
创建一个 config 文件夹,专门用于存放 webpack 的配置文件
1 | |- config |
这里使用到webpack-merge
合并通用配置
1 | npm i webpack-merge -D |
1 | // webpack.common.js |
在package.json
中调用对应的配置文件
1 | { |
HtmlWebpackPlugin
HtmlWebpackPlugin插件自动生成html文件,引入对应的包文件
安装
1 | npm install --save-dev html-webpack-plugin |
在common.js
中添加以下配置
1 | const HtmlWebpackPlugin = require('html-webpack-plugin'); |
webpack-dev-server
安装
1 | npm install --save-dev webpack-dev-server |
在dev.js
配置
1 | module.exports = { |
某些路由使用的history模式的路由,需要404时候,重定向回 index.html,则可以开启下面的选项
1 | historyApiFallback: true // 404的时候重定向回 index.html |
clean-webpack-plugin
安装
1 | npm install --save-dev clean-webpack-plugin |
在common中配置文件
1 | const { CleanWebpackPlugin } = require('clean-webpack-plugin'); |
图片的loader
安装url-loader, url-loader可以将小于一定大小的图片,转换成DataURL格式然后引入,对于大于等于指定大小的图片,将会默认使用 file-loader 将图片引入,所以需要安装file-loader
1 | npm i url-loader file-loader -D |
loader配置
1 | ... |
字体文件的loader
字体文件loader与图片文件的loader类似,同样需要url-loader以及file-loader
1 | npm i url-loader file-loader -D |
option配置
1 | ... |
css以及scss样式loader的配置
先安装css的相关loader
1 | npm i style-loader css-loader -D |
安装scss的相关loader
1 | npm i node-sass sass-loader --save-dev |
为了让自动加上css前缀,需要安装postcss
1 | npm install postcss-loader autoprefixer --save-dev |
在根目录创建一个 postcss.config.js 配置文件,
1 | module.exports = { |
安装postcss-preset-env
,默认包含了autoprefixer
1 | npm i -D postcss-preset-env |
对于module后缀的启动css模块化
1 | { |
其余的样式文件关闭css模块化,否则会造成引入的antd样式失效
1 | { |
对于生产环境,若想将css提取出来,需要安装mini-css-extract-plugin
1 | npm i mini-css-extract-plugin --save-dev |
更改webpack.prod.js
,将style-loader换成 css module 即可
1 | const MiniCssExtractPlugin = require('mini-css-extract-plugin'); |
babel配置
react中大多都是使用es6语法编写,为了让es6语法以及react的jsx语法能够正确在浏览器上运行,需要安装babel,
- @babel-core
- @babel/preset-env 将es6语法转换成es5语法
- @babel/preset-react 将jsx语法转换为Javascript
- @babel/polyfill: ES6 内置方法和函数转化垫片
- @babel/plugin-transform-runtime: 避免 polyfill 污染全局变量,减小打包体积在根目录创建
1
npm i @babel/core babel-loader @babel/preset-env @babel/preset-react --save-dev
.babelrc
文件若想转换es6新的语法,则需要安装1
2
3{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}@babel/polyfill
若想重复利用babel注入的工具代码,可以安装以下插件1
npm i @babel/polyfill @babel/runtime
若想要直接在 class 里面声明静态类变量以及直接声明箭头函数的实例方法1
@babel/plugin-transform-runtime
需要安装 @babel/plugin-proposal-class-properties 插件1
2
3
4
5
6
7
8
9class Dog {
// 声明静态变量
static type = 'animal';
// 箭头函数的实例方法
bark = () => {
console.log('Wang Wang ~')
}
}babel 整体配置1
npm install --save-dev @babel/plugin-proposal-class-properties
1
2
3
4
5
6
7
8
9
10
11
12{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
"react-hot-loader/babel",
"@babel/plugin-proposal-class-properties",
["import", {
"libraryName": "antd",
"libraryDirectory": "es",
"style": "css"
}]
]
}
eslint配置
1 | module.exports = { |
整体webpack的配置
项目 webpack 文件如下:
1 | // webpack.commmon.js |
1 | const merge = require("webpack-merge"); |
1 | const merge = require("webpack-merge"); |
在webpack.common.js
配置输入和输出
1 | const rootPath = path.resolve(_dirname, '../'); |