web优化 之 模块化引入

本文Demo的完整工程代码, 参考import-as-required-antd / import-as-required-lodash / import-as-required-date-fns

目录

  • antd

    • 开始

    • 全部引入

    • 模块化引入

    • 优化

  • lodash

    • 开始

    • 全部引入

    • 模块化引入

    • 优化

  • date-fns

    • 开始

    • 全部引入

    • 模块化引入

    • 优化

antd

开始

dva new import-as-required-antd
npm run build
File sizes after gzip:

  88.61 KB  dist/index.js
  316 B     dist/index.css

全部引入

cnpm i --save antd
vim src/routes/IndexPage.js
import React from 'react';
import { connect } from 'dva';
import { Button } from 'antd';
import styles from './IndexPage.css';

function IndexPage() {
  return (
    
); } IndexPage.propTypes = { }; export default connect()(IndexPage);
npm run build
File sizes after gzip:

  397.95 KB  dist/index.js
  316 B      dist/index.css

模块化引入

vim src/routes/IndexPage.js
import React from 'react';
import { connect } from 'dva';
import Button from 'antd/lib/button';
import 'antd/lib/button/style';
import styles from './IndexPage.css';

function IndexPage() {
  return (
    
); } IndexPage.propTypes = { }; export default connect()(IndexPage);
npm run build
File sizes after gzip:

  90.51 KB  dist/index.js
  10.1 KB   dist/index.css

优化

npm i babel-plugin-import --save-dev
vim .roadhogrc
{
  "entry": "src/index.js",
  "env": {
    "development": {
      "extraBabelPlugins": [
        "dva-hmr",
        "transform-runtime",
        ["import", { "libraryName": "antd", "style": "css" }]
      ]
    },
    "production": {
      "extraBabelPlugins": [
        "transform-runtime",
        ["import", { "libraryName": "antd", "style": "css" }]
      ]
    }
  }
}
vim src/routes/IndexPage.js
import React from 'react';
import { connect } from 'dva';
import { Button } from 'antd';
import styles from './IndexPage.css';

function IndexPage() {
  return (
    
); } IndexPage.propTypes = { }; export default connect()(IndexPage);
npm run build
File sizes after gzip:

  90.51 KB  dist/index.js
  10.1 KB   dist/index.css

关于babel-plugin-import详细介绍 参考ant-design/babel-plugin-import

lodash

开始

dva new import-as-required-lodash
npm run build
File sizes after gzip:

  88.61 KB  dist/index.js
  316 B     dist/index.css

全部引入

cnpm i --save lodash
vim src/routes/IndexPage.js
import React from 'react';
import { connect } from 'dva';
import _ from 'lodash';
import styles from './IndexPage.css';

function IndexPage() {
  const r = _.indexOf(['hello', 'world'], hello);
  console.log('lodash: ', r);

  return (
    

Yay! Welcome to dva!

); } IndexPage.propTypes = { }; export default connect()(IndexPage);
npm run build
File sizes after gzip:

  112.62 KB  dist/index.js
  316 B      dist/index.css

模块化引入

vim src/routes/IndexPage.js
import React from 'react';
import { connect } from 'dva';
import array from 'lodash/array';
import styles from './IndexPage.css';

function IndexPage() {
  const r = array.indexOf(['hello', 'world'], 'hello');
  console.log('lodash: ', r);

  return (
    

Yay! Welcome to dva!

); } IndexPage.propTypes = { }; export default connect()(IndexPage);
npm run build
File sizes after gzip:

  99.57 KB  dist/index.js
  316 B     dist/index.css

优化

cnpm i --save-dev babel-plugin-lodash
vim .roadhogrc
{
  "entry": "src/index.js",
  "env": {
    "development": {
      "extraBabelPlugins": [
        "dva-hmr",
        "transform-runtime",
        "lodash"
      ]
    },
    "production": {
      "extraBabelPlugins": [
        "transform-runtime",
        "lodash"
      ]
    }
  }
}
npm run build
File sizes after gzip:

  89.41 KB  dist/index.js
  316 B     dist/index.css

date-fns

开始

dva new import-as-required-date-fns
npm run build
File sizes after gzip:

  88.61 KB  dist/index.js
  316 B     dist/index.css

全部引入

cnpm i --save date-fns
vim src/routes/IndexPage.js
import React from 'react';
import { connect } from 'dva';
import { format } from 'date-fns';
import styles from './IndexPage.css';

function IndexPage() {
  return (
    
{format(new Date(2017, 6, 6), 'MM/DD/YYYY')}

Yay! Welcome to dva!

); } IndexPage.propTypes = { }; export default connect()(IndexPage);
npm run build
File sizes after gzip:

  96.04 KB  dist/index.js
  316 B     dist/index.css

模块化引入

vim src/routes/IndexPage.js
import React from 'react';
import { connect } from 'dva';
import format from 'date-fns/format';
import styles from './IndexPage.css';

function IndexPage() {
  return (
    
{format(new Date(2017, 6, 6), 'MM/DD/YYYY')}

Yay! Welcome to dva!

); } IndexPage.propTypes = { }; export default connect()(IndexPage);
npm run build
File sizes after gzip:

  91.46 KB  dist/index.js
  316 B     dist/index.css

优化

cnpm i --save-dev babel-plugin-date-fns
vim .roadhogrc
{
  "entry": "src/index.js",
  "env": {
    "development": {
      "extraBabelPlugins": [
        "dva-hmr",
        "transform-runtime",
        "date-fns"
      ]
    },
    "production": {
      "extraBabelPlugins": [
        "transform-runtime",
        "date-fns"
      ]
    }
  }
}
npm run build
File sizes after gzip:

  91.46 KB  dist/index.js
  316 B     dist/index.css

参考

  • Tree Shaking

  • Webpack 打包优化之体积篇

  • 快速上手

  • ant-design/babel-plugin-import

  • Webpack按需打包Lodash的几种方式

  • date-fns 轻量级的 JavaScript 日期库

更多文章, 请支持我的个人博客

你可能感兴趣的:(web优化 之 模块化引入)