compression-webpack-plugin项目加载优化

1.开发环境 react18+webpack4
2.电脑系统 windows11专业版
3.在项目打包的过程中,我们发现打出来的包比较大,而且项目加载比较慢[比如首页],解决方法是使用compression-webpack-plugin进行打包压缩gzip。
4.所有现代浏览器都支持gzip压缩,启用gzip压缩可扩展和扩展传输资源大小,从而延长资源下载时间,减少首次白屏时间,提升用户体验。gzip对基于文本格式文件的压缩效果最好(如:CSS,JavaScript和HTML),在压缩压缩文件时往往可实现高达70-90%的压缩率,对已经压缩过的资源(如:图片)进行gzip压缩处理,效果很不好。

npm i compression-webpack-plugin@5 --save
注意,这里不能直接下载,需要下载低版本的。
直接下载就是最新版的了,项目可能暂时不支持最新版的,
所以就会报错:TypeError: Cannot read property 'tapPromise' of undefined。
const CompressionPlugin = require('compression-webpack-plugin');
plugins: [
 new CompressionPlugin({
 // gzip压缩配置
 algorithm: 'gzip',
 test: /\.(js|css)$/, // 匹配文件名
 threshold: 10240, // 对超过10kb的数据进行压缩
 deleteOriginalAssets: true, // 是否删除原文件
 }),
 ]
这里配置完以后,暂时还不能使用,还需要后端做一下配置,这里后端以nginx为例

5.主要配置如下图:


#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
 worker_connections 1024;
}
http {
 include mime.types;
 default_type application/octet-stream;
 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
 # '$status $body_bytes_sent "$http_referer" '
 # '"$http_user_agent" "$http_x_forwarded_for"';
 #access_log logs/access.log main;
 sendfile on;
 #tcp_nopush on;
 #keepalive_timeout 0;
 keepalive_timeout 65;
 #gzip on;
 server {
 listen 9000;
 server_name localhost;
 #charset koi8-r;
 #access_log logs/host.access.log main;
 location / {
 root D:/dist;
 index index.html index.htm;
 }
 location /api {
 proxy_pass http://localhost:3000;
 }
 # 主要是下方的gizp配置哦,直接复制粘贴就可以使用啦,亲测有效哦
 gzip on; # 开启gzip压缩
 gzip_min_length 4k; # 小于4k的文件不会被压缩,大于4k的文件才会去压缩
 gzip_buffers 16 8k; # 处理请求压缩的缓冲区数量和大小,比如8k为单位申请16倍内存空间;使用默认即可,不用修改
 gzip_http_version 1.1; # 早期版本http不支持,指定默认兼容,不用修改
 gzip_comp_level 2; # gzip 压缩级别,1-9,理论上数字越大压缩的越好,也越占用CPU时间。实际上超过2的再压缩,只能压缩一点点了,但是cpu确是有点浪费。因为2就够用了
 # 压缩的文件类型 MIME类型,百度一下,一大把 # css # xml # 识别php # 图片
 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 application/vnd.ms-fontobject font/x-woff font/ttf;
 # text # 早期js # js # js的另一种写法 # .eot字体 # woff字体 # ttf字体
 gzip_vary on; # 是否在http header中添加Vary: Accept-Encoding,一般情况下建议开启
 gzip_static on; 
 #error_page 404 /404.html;
 # redirect server error pages to the static page /50x.html
 #
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root html;
 }
 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 #
 #location ~ \.php$ {
 # proxy_pass http://127.0.0.1;
 #}
 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 #
 #location ~ \.php$ {
 # root html;
 # fastcgi_pass 127.0.0.1:9000;
 # fastcgi_index index.php;
 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
 # include fastcgi_params;
 #}
 # deny access to .htaccess files, if Apache's document root
 # concurs with nginx's one
 #
 #location ~ /\.ht {
 # deny all;
 #}
 }
 # another virtual host using mix of IP-, name-, and port-based configuration
 #
 #server {
 # listen 8000;
 # listen somename:8080;
 # server_name somename alias another.alias;
 # location / {
 # root html;
 # index index.html index.htm;
 # }
 #}
 # HTTPS server
 #
 #server {
 # listen 443 ssl;
 # server_name localhost;
 # ssl_certificate cert.pem;
 # ssl_certificate_key cert.key;
 # ssl_session_cache shared:SSL:1m;
 # ssl_session_timeout 5m;
 # ssl_ciphers HIGH:!aNULL:!MD5;
 # ssl_prefer_server_ciphers on;
 # location / {
 # root html;
 # index index.html index.htm;
 # }
 #}
}
// 关键 配置
# 主要是下方的gizp配置哦,直接复制粘贴就可以使用啦,亲测有效哦
 gzip on; # 开启gzip压缩
 gzip_min_length 4k; # 小于4k的文件不会被压缩,大于4k的文件才会去压缩
 gzip_buffers 16 8k; # 处理请求压缩的缓冲区数量和大小,比如8k为单位申请16倍内存空间;使用默认即可,不用修改
 gzip_http_version 1.1; # 早期版本http不支持,指定默认兼容,不用修改
 gzip_comp_level 2; # gzip 压缩级别,1-9,理论上数字越大压缩的越好,也越占用CPU时间。实际上超过2的再压缩,只能压缩一点点了,但是cpu确是有点浪费。因为2就够用了
 # 压缩的文件类型 MIME类型,百度一下,一大把 # css # xml # 识别php # 图片
 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 application/vnd.ms-fontobject font/x-woff font/ttf;
 # text # 早期js # js # js的另一种写法 # .eot字体 # woff字体 # ttf字体
 gzip_vary on; # 是否在http header中添加Vary: Accept-Encoding,一般情况下建议开启
 gzip_static on;

6.本期的分享到了这里就结束啦,希望对你有所帮助,让我们一起努力走向巅峰。

作者:灰太狼的情与殇原文地址:https://segmentfault.com/a/1190000043544672

%s 个评论

要回复文章请先登录注册