如何在React App中使用gzip压缩文件 [英] How to compress the files using gzip in react app

查看:352
本文介绍了如何在React App中使用gzip压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用create-react-app创建了一个react应用。我想在构建过程中对文件进行gzip压缩。

I have created a react app using create-react-app. I want to gzip the files during the build.

我希望对文件进行gzip压缩

I expect the files to be gziped

推荐答案

最简单的方法可能是修改package.json。

Easiest way is probably to modify your package.json.

在脚本中添加一个 postbuild部分。例如,如下所示:

Add a "postbuild" section to the scripts. For example, something like this:

"scripts": {
"build": "YOUR BUILD COMMAND"
"postbuild": "tar -cvzf your_react_app_name.tar.gz /path/to/your/build/artifacts"
}

构建运行后,构建后应自动运行。
如果您没有tar:

Post build should run automatically after build runs. If you don't have tar:

"scripts": {
"build": "YOUR BUILD COMMAND"
"postbuild": "cd /path/to/your/build && gzip *.js && gzip *.css"
}

或使用zip:

"scripts": {
"build": "YOUR BUILD COMMAND"
"postbuild": "zip -r your_app.zip /path/to/your/build"
}

Windows powershell:

Windows powershell:

"scripts": {
"build": "YOUR BUILD COMMAND"
"postbuild": "Compress-Archive -Path C:\path\to\your\build\* -CompressionLevel Optimal -DestinationPath C:\export\path\your_app_name.zip"
}

这篇关于如何在React App中使用gzip压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆