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.本期的分享到了这里就完结啦,心愿对你有所帮忙,让咱们一起致力走向巅峰。