vue 前端性能监控及优化方案
/index.html
中引入CDN
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>vue-manage-system</title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
<script ></script>
<script ></script>
<script ></script>
<link rel="stylesheet" href="https://cdn.bootcss.com/element-ui/2.4.0/theme-chalk/index.css">
<script src="https://cdn.bootcss.com/element-ui/2.4.0/index.js"></script>
</head>
<body>
<div id="app"></div>
</body>
</html>
module.exports = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js'
},
externals: {
'vue': 'Vue',
'vue-router': 'VueRouter',
'ElementUI': 'ELEMENT',
'axios': 'axios',
}
}
如果不删除原先的import,项目还是会从node_modules中引入资源。也就是说不删的话,npm run build时候仍会将引用的资源一起打包,生成文件会大不少。所以我认为还是删了好。
如图:
// 安装 compression-webpack-plugin
npm install --save-dev compression-webpack-plugin@1.1.12
// 导入compression-webpack-plugin
const CompressionWebpackPlugin = require('compression-webpack-plugin')
// 定义压缩文件类型
const productionGzipExtensions = ['js', 'css']
module.exports = {
// 配置反向代理
devServer: {
proxy: {
'/api': {
target: 'http://172.31.120.61:8080/',
// target: 'http://172.31.120.158:8080/',
ws: true,
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
},
configureWebpack: {
plugins: [
new CompressionWebpackPlugin({
filename: '[path].gz[query]',
algorithm: 'gzip',
test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
threshold: 10240,
minRatio: 0.8
})
]
}
}
3、nginx 配制Gzip
# 开启gzip
gzip on;
# 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
# 设置压缩所需要的缓冲区大小
gzip_buffers 16 64k;
# 设置gzip压缩针对的HTTP协议版本
gzip_http_version 1.1;
# gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
gzip_comp_level 3;
gzip_types text/plain application/x-javascript application/javascript text/javascript text/css application/xml application/x-httpd-php image/jpeg image/gif image/png;
# 是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
nginx -s reload
5、解决 配置后 npm run build 报错
报错信息
npm run build
[email protected] build /home/lvph/work/XXXX
vue-cli-service build
sh: XXXX/node_modules/.bin/vue-cli-service: Permission denied
npm ERR! code ELIFECYCLE
npm ERR! errno 126
npm ERR! [email protected] build: `vue-cli-service build`
npm ERR! Exit status 126
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/lvph/.npm/_logs/2020-01-19T01_33_20_803Z-debug.log
解决办法
到项目路径下
rm -rf node_modules
rm package-lock.json
npm cache clear --force
在执行
npm install
https://www.cnblogs.com/Renyi-Fan/p/11047490.html
https://www.jianshu.com/p/1c5f80f45767
https://www.yuque.com/wangjingfan/uca8pl/mwohyt#8jEvf //特别详细