Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

前端优化 #2

Open
carlleton opened this issue Apr 8, 2019 · 0 comments
Open

前端优化 #2

carlleton opened this issue Apr 8, 2019 · 0 comments

Comments

@carlleton
Copy link
Owner

carlleton commented Apr 8, 2019

记录一下当前vue工程下使用的优化(vue-cli3):

1. moment.js

方案一:替换为date-fns或dayjs,参考You-Dont-Need-Momentjs (翻译版)

方案二:使用ignorePlugin删除它国际化的部分,具体操作:
在vue.config.js中加入如下配置:

const webpack = require('webpack');

module.exports = {
  chainWebpack: config => {
    config
      .plugin('ignorePlugin')
      .use(webpack.IgnorePlugin, [{
        resourceRegExp: /^\.\/locale$/,
        contextRegExp: /moment$/,
      }]);
  },
}

使用方式:

const moment = require('moment');
// 引入zh-cn locale文件
require('moment/locale/zh-cn');
moment.locale('zh-cn');

缩减后,大致会缩减至原来的四分之一

2.lodash.js

方法一:单独引入

import {chain, cloneDeep} from 'lodash';
// 可以改写为
import chain from 'lodash/chain';
import cloneDeep from 'lodash/cloneDeep';

方法二:引入lodash-webpack-pluginbabel-plugin-lodash
在vue.config.js中chainWebpack加入

config.plugin("loadshReplace")
        	.use(new LodashModuleReplacementPlugin());//优化lodash

方法三:使用 lodash-es
利用tree-shaking

import {isEmpty, isObject, cloneDeep} from 'lodash-es';

参考:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant