最近接手了一個后臺管理系統(tǒng),技術棧主要是vue全家桶+elementui甩卓,老大打開測試環(huán)境頁面的時候鸠匀,說看到首頁需要6秒鐘,那如何進行優(yōu)化呢猛频?
首先我們需要安裝webpack-bundle-analyzer
// webpack.prod.conf.js
if (config.build.bundleAnalyzerReport) {
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
webpackConfig.plugins.push(new BundleAnalyzerPlugin())
}
// config/index.js
build: {
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
}
運行npm run build --report
image
我們可以看到狮崩,vendor中的elementui占了500k蛛勉,怎么優(yōu)化呢?
在webpack配置文件中增加睦柴,接下來就是見證奇跡的時刻诽凌。
externals: {
'vue': 'Vue',
'element-ui': 'ELEMENT',
'axios': 'axios'
},
再看一下我們的vendor體積
image
vendor一共才195k
那缺少的elementui文件去哪里找呢?答案是cdn引用坦敌。
image
之前項目里還有引用moment侣诵,但是這個庫實在是太大了,在github上我找到一個跟momentapi完全一樣的庫狱窘,但是文件大小只要2kb杜顺。
image
其他優(yōu)化方法還有ssr,這個最好用nuxtjs來做蘸炸,自己配置ssr實在太麻煩了躬络。
如果解決了你的問題,幫我點個贊吧