参考:
https://www.cnblogs.com/xidian-Jingbin/p/10643391.html
https://blog.csdn.net/weixin_43993065/article/details/105374325
https://blog.csdn.net/qq_39953537/article/details/82188438
删除打包后的map文件
config/index.js 文件中将productionSourceMap 的值设置为false.
vue-router懒加载
{
path: '/404',
component: (resolve) => require(['@/views/features/404'], resolve),//懒加载
hidden: true
}
使用gzip压缩
1、npm install --save-dev compression-webpack-plugin
2、vue.config.js中修改
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css']
configureWebpack: config => {
// 生产环境下使用gzip
if (process.env.NODE_ENV === 'production') {
// 增加浏览器CPU(需要解压缩), 减少网络传输量和带宽消耗 (需要衡量,一般小文件不需要压缩的)
// 图片和PDF文件不应该被压缩,因为他们已经是压缩的了,试着压缩他们会浪费CPU资源而且可能潜在增加文件大小。
config.plugins.push(
new CompressionWebpackPlugin({
filename: '[path].gz[query]', // asset -> filename
algorithm: 'gzip',
test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
threshold: 10240, // 达到10kb的静态文件进行压缩 按字节计算
minRatio: 0.8, // 只有压缩率比这个值小的资源才会被处理
deleteOriginalAssets: false // 是否删除压缩的源文件
})
)
}
/*****------别名配置等,可用return返回配置---------*****/
return {
name: name,
resolve: {
alias: {
'@': resolve('src'),
'@crud': resolve('src/components/Crud')
}
}
}
}
3、再去nginx的/etc/nginx/nginx.conf里配置
http:{
gzip on;
gzip_static on;
gzip_buffers 4 16k;
gzip_comp_level 5;
gzip_types text/plain application/javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
}
使用CDN引入资源,并更换为min版本
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="icon" href="./favicon.ico">
<link href="https://cdn.bootcss.com/element-ui/2.13.0/theme-chalk/index.css" rel="stylesheet">
<title><%= webpackConfig.name %></title>
</head>
<body>
<noscript>
<strong>We're sorry but <%= webpackConfig.name %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.min.js"></script>
<script src="https://cdn.bootcss.com/element-ui/2.13.0/index.js"></script>
<script src="https://cdn.bootcss.com/vuex/3.1.1/vuex.min.js"></script>
<script src="https://cdn.bootcss.com/axios/0.19.0/axios.min.js"></script>
<script src="https://cdn.bootcss.com/vue-router/3.1.2/vue-router.min.js"></script>
</body>
</html>
tips:安装 webpack-bundle-analyzer 插件,可以在打包时看到打包文件大小