最近接手了一个后台管理系统,技术栈主要是vue全家桶+elementui,老大打开测试环境页面的时候,说看到首页须要6秒钟,那如何进行优化呢?vue
首先咱们须要安装webpack-bundle-analyzer
webpack
// 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
ios
咱们能够看到,vendor中的elementui占了500k,怎么优化呢?git
在webpack配置文件中增长,接下来就是见证奇迹的时刻。github
externals: { 'vue': 'Vue', 'element-ui': 'ELEMENT', 'axios': 'axios' },
再看一下咱们的vendor体积web
vendor一共才195knpm
那缺乏的elementui文件去哪里找呢?答案是cdn引用。element-ui
以前项目里还有引用moment,可是这个库实在是太大了,在github上我找到一个跟momentapi彻底同样的库,可是文件大小只要2kb。axios
其余优化方法还有ssr,这个最好用nuxtjs来作,本身配置ssr实在太麻烦了。api
若是解决了你的问题,帮我点个赞吧