从度娘到处搜刮的一些前端优化的手段,主要是一些对首屏加载的优化已经减小打包文件大小的措施。
component: (resolve) => require(['./XXX.vue'], resolve)
module: { rules: [ { test: /\.(js|jsx)$/,, use: [ 'babel-loader', 'lazyload-loader' //注意位置 ] }] } //...... // 使用lazy! 前缀 代表需要懒加载的Router import Shop from 'lazy!./src/view/Shop'; // Router 正常使用 <Route path="/shop" component={Shop} /> 复制代码
npm install font-spider -g
font-spider ./demo/*.html
使用命令解析这个html文件 就会生成新的字体包productionSourceMap: false
externals: { 'vue': 'Vue', 'vue-router': 'VueRouter', 'element-ui': 'ELEMENT', 'echarts': 'echarts', 'vuex': 'Vuex' }, 复制代码
new UglifyJsPlugin({ uglifyOptions: { comments: false, show_copyright: false, compress: { warnings: false, drop_debugger: true, drop_console: true } }, sourceMap: false, parallel: true }), 复制代码
productionGzip: true
npm install --save-dev compression-webpack-plugin@1.1.11
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh-cn/),
大约会减少200kb// 简易版moment代替moment.js class Moment { date constructor(arg = new Date().getTime()) { this.date = new Date(arg); } padStart(num) { num = String(num); if (num.length < 2) { return '0' + num; } else { return num; } } unix() { return Math.round(this.date.getTime() / 1000); } static unix(timestamp) { return new Moment(timestamp * 1000); } format(formatStr) { const date = this.date; const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); const week = date.getDay(); const hour = date.getHours(); const minute = date.getMinutes(); const second = date.getSeconds(); const weeks = ['一', '二', '三', '四', '五', '六', '日']; return formatStr.replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|h{1,2}|m{1,2}|s{1,2}/g, (match) => { switch (match) { case 'YY': return String(year).slice(-2); case 'YYY': case 'YYYY': return String(year); case 'M': return String(month); case 'MM': return this.padStart(month); case 'D': return String(day); case 'DD': return this.padStart(day); case 'd': return String(week); case 'dd': return weeks[week]; case 'ddd': return '周' + weeks[week]; case 'dddd': return '星期' + weeks[week]; case 'h': return String(hour); case 'hh': return this.padStart(hour); case 'm': return String(minute); case 'mm': return this.padStart(minute); case 's': return String(second); case 'ss': return this.padStart(second); default: return match; } }); } } export const moment = (arg) => { return new Moment(arg); }; 复制代码
import { is } from 'immutable'; shouldComponentUpdate: (nextProps = {}, nextState = {}) => { const thisProps = this.props || {}, thisState = this.state || {}; if (Object.keys(thisProps).length !== Object.keys(nextProps).length || Object.keys(thisState).length !== Object.keys(nextState).length) { return true; } for (const key in nextProps) { if (!is(thisProps[key], nextProps[key])) { return true; } } for (const key in nextState) { if (thisState[key] !== nextState[key] && !is(thisState[key], nextState[key])) { return true; } } return false; } 复制代码
document.getElementsByTagName('*').length
可获取页面元素数量