SelinaChan 2020-04-23
其中vendor.js文件高达1.16MB,平均白屏时间超过10秒。
(当然你也可以选择其他CDN网站,本例使用bootcdn.cn)
3. 修改 项目根目录下的 index.html
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" /><!--在meta这一行下面添加刚才在cdn网站找到的引用,注意版本号要和你的package.json里的一致 --><script src="https://cdn.bootcss.com/vue/2.5.2/vue.min.js"></script><script src="https://cdn.bootcss.com/element-ui/2.13.0/index.js"></script><script src="https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js"></script><link href="https://cdn.bootcss.com/quill/1.3.7/quill.snow.css" rel="stylesheet"><script src="https://cdn.bootcss.com/quill/1.3.7/quill.min.js"></script><script src="/static/js/vue-quill-editor.js"></script>
4.修改 /build/webpack.base.conf.js 文件
module.exports = { context: path.resolve(__dirname, ‘../‘), entry: { app: ‘./src/main.js‘ },// 在这个位置添加externals设置: externals: { ‘vue‘: ‘Vue‘, ‘vue-router‘: ‘VueRouter‘, ‘element-ui‘: ‘ELEMENT‘, //注意element-ui的引用时纯大写 ‘vue-quill-editor‘: ‘VueQuillEditor‘, }, output: { path: config.build.assetsRoot, filename: ‘[name].js‘, publicPath: process.env.NODE_ENV === ‘production‘ ? config.build.assetsPublicPath : config.dev.assetsPublicPath },
//import ElementUI from ‘element-ui‘//import Vue from ‘vue‘//import Router from ‘vue-router‘//Vue.use(ElementUI)//Vue.use(Router)//Vue.use(VueQuillEditor)
将这些代码通通注释掉,因为在externals里面已经做过声明了,如果在Vue文件里再声明一次,将会报重复引用的错误。
vendor.js从1.2M瞬间减肥减到了109K
productionGzip: true, productionGzipExtensions: [‘js‘, ‘css‘],
gzip on;gzip_types text/plain application/x-javascript application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
可以看到vendor.js文件被狠狠地压缩到只有34Kb的程度