最近接手了一個後臺管理系統,技術棧主要是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

在webpack配置檔案中增加,接下來就是見證奇蹟的時刻。
externals: {
'vue': 'Vue',
'element-ui': 'ELEMENT',
'axios': 'axios'
},
複製程式碼
再看一下我們的vendor體積

那缺少的elementui檔案去哪裡找呢?答案是cdn引用。

之前專案裡還有引用moment,但是這個庫實在是太大了,在github上我找到一個跟momentapi完全一樣的庫,但是檔案大小隻要2kb。

如果解決了你的問題,幫我點個贊吧