最近接手了一個后臺管理系統,技術棧主要是vue全家桶+elementui,老大打開測試環境頁面的時候,說看到首頁需要6秒鐘,那如何進行優化呢?
首先我們需要安裝webpack-bundle-analyzer
// webpack.prod.conf.jsif (config.build.bundleAnalyzerReport) { const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin webpackConfig.plugins.push(new BundleAnalyzerPlugin())}// config/index.jsbuild: { // 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實在太麻煩了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答