快速开始

  • 全局安装 easywebpack-cli 插件
  1. npm i easywebpack-cli -g
  • 请通过 easy init 命令初始化骨架项目, 根据提示选择 React 对应的项目类型即可.
  • 开发: npm run dev
  • 构建: npm run build

从零开始

安装 easywebpack-cli 工具

  1. npm install easywebpack-cli -g

初始化项目

  • 通过 git init 初始化项目
  • 安装项目依赖
  1. npm install react react-dom --save
  2. npm install easywebpack-cli easywebpack-react
  • 编写 src/index.jsx 组件
  1. import React, { Component } from 'react';
  2. export default class Index extends Component {
  3. render() {
  4. return <h1>React Client Render Example</h1>;
  5. }
  6. }
  • 添加 src/app.js 入口文件
  1. import React, { Component } from 'react'
  2. import ReactDOM from 'react-dom'
  3. import Index from 'index.jsx'
  4. ReactDOM.render(<Entry />, document.getElementById('app'));
  • 添加 HTMLWebpackPlugin 模板配置 src/view/layout.html

默认 template 路径文件为 src/view/layout.html 直接存在该文件即可,无需 Webpack 配置。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title></title>
  5. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  6. <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
  7. <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
  8. </head>
  9. <body>
  10. <div id="app"></div>
  11. </body>
  12. </html>

Webpack 构建

基于 webpack-cli 模式

  1. // webpack.config.js
  2. const easywebpack = require('@easy-team/easywebpack-react');
  3. const { webpack, merge } = easywebpack.webpack;
  4. const env = process.env.BUILD_ENV;
  5. const baseWebpackConfig = easywebpack.getWebpackConfig({
  6. env, // 根据环境变量生成对应配置,可以在 npm script 里面配置,支持 dev, test, prod 模式
  7. target : 'web', // target: web 表示只获取前端构建 Webpack 配置
  8. entry:{
  9. index: 'src/index.js'
  10. }
  11. });
  12. // 拿到基础配置, 可以进行二次加工
  13. const webpackConfig = merge(baseWebpackConfig, {
  14. // 自定义配置
  15. })
  16. 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 文件, 添加如下配置

  1. const path = require('path');
  2. module.exports = {
  3. target:'web',
  4. framework: 'react', // 指定用 easywebpack-react 解决方案, 请在项目中安装该依赖
  5. entry: {
  6. index: 'src/index.js'
  7. }
  8. };

编译文件

  1. easy build dev
  2. easy build test
  3. easy build prod

直接运行
  1. easy dev
  2. # 构建文件并启动本地静态 HTTP Server
  3. easy build --server

运行完成自动打开编译结果页面 : http://127.0.0.1:8888/debug

特性支持

Sass/Less/Stylus 配置

Css Style

热更新实现

热更新

Babel 配置

具体根据实际情况添加相关 babel 插件配置,以下仅仅是举例. 详细配置见:https://www.yuque.com/easy-team/easywebpack/babel

Babel 7 配置

${app_root}/babel.config.js
  1. {
  2. "presets": [
  3. "@babel/preset-react",
  4. [
  5. "@babel/preset-env",
  6. {
  7. "modules": false
  8. }
  9. ]
  10. ],
  11. "plugins": [
  12. "@babel/plugin-syntax-dynamic-import",
  13. "@babel/plugin-proposal-object-rest-spread",
  14. ]
  15. }

Babel 6 配置

${app_root}/.babelrc
  1. {
  2. "presets": ['react', ["env",{ "modules": false }]],
  3. "plugins": [
  4. "transform-object-rest-spread",
  5. "syntax-dynamic-import"
  6. ],
  7. "comments": false
  8. }

Autoprefixer

${app_root}/postcss.config.js 具体根据实际情况添加 postcss 配置,以下仅仅是举例:

  1. 'use strict';
  2. module.exports = {
  3. plugins: [
  4. require('autoprefixer')
  5. ]
  6. };

自定义 Babel 配置文件

默认 easywebpack 已内置基础配置,如有需要,请添加babel 配置覆盖(完全覆盖而非合并)。具体根据实际情况添加相关 babel 插件配置,以下仅仅是举例. 详细配置见:https://www.yuque.com/easy-team/easywebpack/babel 。

  • babel 7 配置
  1. {
  2. "presets": [
  3. "@babel/preset-react",
  4. [
  5. "@babel/preset-env",
  6. {
  7. "modules": false
  8. }
  9. ]
  10. ],
  11. "plugins": [
  12. "@babel/plugin-syntax-dynamic-import",
  13. "@babel/plugin-proposal-object-rest-spread",
  14. ]
  15. }
  • babel 6 配置
  1. {
  2. "presets": ['react',["env",{ "modules": false }]],
  3. "plugins": [
  4. "transform-object-rest-spread",
  5. "syntax-dynamic-import"
  6. ],
  7. "comments": false
  8. }

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'
          }
        }
      }
    }
  }
 ]
};