问题描述:
一、解决打包后vendor.js 很大,导致上线后页面首次加载速度异常慢:
1、修改index.html页面,在head中引入cdn上的资源。
<script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script> <script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script> <script src="https://cdn.bootcss.com/element-ui/2.4.0/index.js"></script> <script src="https://cdn.bootcss.com/echarts/3.7.0/echarts.min.js"></script>
<!-- 引入 cdn 资源 --> <script src="https://cdn.bootcss.com/vue/2.5.16/vue.min.js"></script>
<script> //<![CDATA[ if (typeof Vue == 'undefined') { document.write(unescape("%3Cscript src='https://cdn.staticfile.org/vue/2.6.10/vue.min.js' %3E%3C/script%3E")); } //]]> </script> <script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script> <script> //<![CDATA[ if (typeof VueRouter == 'undefined') { document.write(unescape("%3Cscript src='https://cdn.staticfile.org/vue-router/3.0.7/vue-router.min.js' %3E%3C/script%3E")); } //]]> </script> <script src="https://cdn.bootcss.com/element-ui/2.4.0/index.js"></script> <script> //<![CDATA[ if (typeof ELEMENT == 'undefined') { document.write(unescape("%3Cscript src='https://cdn.staticfile.org/element-ui/2.10.1/index.js' %3E%3C/script%3E")); } //]]> </script> <script src="https://cdn.bootcss.com/echarts/3.7.0/echarts.min.js"></script> <script> //<![CDATA[ if (typeof echarts == 'undefined') { document.write(unescape("%3Cscript src='https://cdn.staticfile.org/echarts/4.2.1-rc1/echarts-en.min.js' %3E%3C/script%3E")); } //]]> </script>
2、修改webpack.base.conf.js文件.添加externals配置
externals: {
'vue': 'Vue',
'vue-router': 'VueRouter',
'element-ui': 'ELEMENT',
"echarts": "echarts",
},
3. 删除main.js中的相应import from。因为如果不删除,打包的时候还会把这两个文件打进去。
下面再补充一些其它优化:
http://www.bejson.com/ui/compress_img/