快速开始
- 全局安装
easywebpack-cli插件
npm i easywebpack-cli -g
- 请通过
easy init命令初始化骨架项目, 根据提示选择 React 对应的项目类型即可. - 开发:
npm run dev - 构建:
npm run build
从零开始
安装 easywebpack-cli 工具
npm install easywebpack-cli -g
初始化项目
- 通过
git init初始化项目 - 安装项目依赖
npm install react react-dom --savenpm install easywebpack-cli easywebpack-react
- 编写
src/index.jsx组件
import React, { Component } from 'react';export default class Index extends Component {render() {return <h1>React Client Render Example</h1>;}}
- 添加
src/app.js入口文件
import React, { Component } from 'react'import ReactDOM from 'react-dom'import Index from 'index.jsx'ReactDOM.render(<Entry />, document.getElementById('app'));
- 添加 HTMLWebpackPlugin 模板配置
src/view/layout.html
默认 template 路径文件为 src/view/layout.html 直接存在该文件即可,无需 Webpack 配置。
<!DOCTYPE html><html lang="en"><head><title></title><meta http-equiv="content-type" content="text/html;charset=utf-8"><meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui"><link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" /></head><body><div id="app"></div></body></html>
Webpack 构建
基于 webpack-cli 模式
// webpack.config.jsconst easywebpack = require('@easy-team/easywebpack-react');const { webpack, merge } = easywebpack.webpack;const env = process.env.BUILD_ENV;const baseWebpackConfig = easywebpack.getWebpackConfig({env, // 根据环境变量生成对应配置,可以在 npm script 里面配置,支持 dev, test, prod 模式target : 'web', // target: web 表示只获取前端构建 Webpack 配置entry:{index: 'src/index.js'}});// 拿到基础配置, 可以进行二次加工const webpackConfig = merge(baseWebpackConfig, {// 自定义配置})module.exports = webpackConfig;
本地开发
- 使用 webpack-dev-server :
webpack-dev-server --hot 使用 easywepback-cli:
easy dev --webpack打包编译
使用 webpack-cli :
webpack --mode production --config webpack.config.js- 使用 easywepback-cli:
easy build --webpack
基于 easywebpack-cli 构建模式
添加 webpack.config.js 配置
在项目根目录添加 webpack.config.js 文件, 添加如下配置
const path = require('path');module.exports = {target:'web',framework: 'react', // 指定用 easywebpack-react 解决方案, 请在项目中安装该依赖entry: {index: 'src/index.js'}};
编译文件
easy build deveasy build testeasy build prod
直接运行
easy dev# 构建文件并启动本地静态 HTTP Servereasy build --server
运行完成自动打开编译结果页面 : http://127.0.0.1:8888/debug
特性支持
Sass/Less/Stylus 配置
热更新实现
Babel 配置
具体根据实际情况添加相关 babel 插件配置,以下仅仅是举例. 详细配置见:https://www.yuque.com/easy-team/easywebpack/babel
Babel 7 配置
${app_root}/babel.config.js
{"presets": ["@babel/preset-react",["@babel/preset-env",{"modules": false}]],"plugins": ["@babel/plugin-syntax-dynamic-import","@babel/plugin-proposal-object-rest-spread",]}
Babel 6 配置
${app_root}/.babelrc
{"presets": ['react', ["env",{ "modules": false }]],"plugins": ["transform-object-rest-spread","syntax-dynamic-import"],"comments": false}
Autoprefixer
${app_root}/postcss.config.js 具体根据实际情况添加 postcss 配置,以下仅仅是举例:
'use strict';module.exports = {plugins: [require('autoprefixer')]};
自定义 Babel 配置文件
默认 easywebpack 已内置基础配置,如有需要,请添加babel 配置覆盖(完全覆盖而非合并)。具体根据实际情况添加相关 babel 插件配置,以下仅仅是举例. 详细配置见:https://www.yuque.com/easy-team/easywebpack/babel 。
- babel 7 配置
{"presets": ["@babel/preset-react",["@babel/preset-env",{"modules": false}]],"plugins": ["@babel/plugin-syntax-dynamic-import","@babel/plugin-proposal-object-rest-spread",]}
- babel 6 配置
{"presets": ['react',["env",{ "modules": false }]],"plugins": ["transform-object-rest-spread","syntax-dynamic-import"],"comments": false}
Autoprefixer 配置
具体根据实际情况添加 postcss 配置,以下仅仅是举例:
'use strict';
module.exports = {
plugins: [
require('autoprefixer')
]
};
HTML 页面 HTMLWebpackPlugin 模板配置
默认 template 路径文件为 src/view/layout.html 如果需要构建 HTML 文件,直接存在该文件即可,无需 Webpack 配置。
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
<div id="app"></div>
</body>
</html>
AntD 按需加载
依赖配置
// ${root}/package.json
{
"devDependencies": {
"babel-plugin-import": "^1.0.0"
}
}
代码编写
import { Button } from 'antd';
babel.cofnig.js 配置
module.exports = {
"presets": [
"@babel/preset-react",
[
"@babel/preset-env",
{
"modules": false
}
]
],
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-proposal-object-rest-spread",
["import", {
"libraryName": "antd",
"libraryDirectory": "lib",
"style": true
}]
],
"env": {
"development": {
"plugins": [
"react-hot-loader/babel"
]
}
},
}
AntD 主题定制
- AntD 主题定制需要开启 webpack less 编译
- Babel 和 Less 编译需要包含 node_modules 的 antd
less 构建支持
// ${root}/webpack.config.js
module.exports = {
module:{
rules:[
{ less: true }
]
}
}
依赖配置
// ${root}/package.json
{
"devDependencies": {
"less": "^2.7.2",
"less-loader": "^4.1.0"
}
}
构建配置
//${root}/webpack.config.js
const path = require('path');
const resolve = (filepath) => path.resolve(__dirname, filepath);
module.exports = {
module: rules: [
{
babel: {
include: [resolve('app/web'), resolve('node_modules')]
}
},
{
less: {
include: [resolve('app/web'), resolve('node_modules')],
options: {
javascriptEnabled: true,
modifyVars: {
'primary-color': 'red',
'link-color': '#1DA57A',
'border-radius-base': '2px'
}
}
}
}
}
]
};
