最近接手了一個後臺管理系統,技術棧主要是vue全家桶+elementui,老大打開測試環境頁面的時候,說看到首頁需要6秒鐘,那如何進行優化呢?
首先我們需要安裝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
我們可以看到,vendor中的elementui佔了500k,怎麼優化呢?
在webpack配置文件中增加,接下來就是見證奇蹟的時刻。
externals: {
'vue': 'Vue',
'element-ui': 'ELEMENT',
'axios': 'axios'
},
再看一下我們的vendor體積
vendor一共才195k
那缺少的elementui文件去哪裏找呢?答案是cdn引用。
之前項目裏還有引用moment,但是這個庫實在是太大了,在github上我找到一個跟momentapi完全一樣的庫,但是文件大小隻要2kb。
其他優化方法還有ssr,這個最好用nuxtjs來做,自己配置ssr實在太麻煩了。
如果解決了你的問題,幫我點個贊吧