Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在弹出/不弹出create react应用程序的情况下压缩生成。还包括压缩文件到脚本标记到index.html_Reactjs_Create React App - Fatal编程技术网

Reactjs 如何在弹出/不弹出create react应用程序的情况下压缩生成。还包括压缩文件到脚本标记到index.html

Reactjs 如何在弹出/不弹出create react应用程序的情况下压缩生成。还包括压缩文件到脚本标记到index.html,reactjs,create-react-app,Reactjs,Create React App,我正在使用react scripts build制作生产准备版本,并将其部署到AWS服务器上。我正在使用生成\u SOURCEMAP=false-warn build和&AWS s3同步版本/s3://********* 是否有任何方法可以应用gzip压缩来构建文件,并将gzip文件包含到index.html中,并将其部署到aws上,以便在浏览器中提供gzip文件。更改cra构建过程并不容易,您可以将其弹出,但之后必须创建自己的文件。但是,在您的package.json中,您可以定义要在构建

我正在使用
react scripts build
制作生产准备版本,并将其部署到AWS服务器上。我正在使用

生成\u SOURCEMAP=false-warn build和&AWS s3同步版本/s3://*********



是否有任何方法可以应用gzip压缩来构建文件,并将gzip文件包含到index.html中,并将其部署到aws上,以便在浏览器中提供gzip文件。

更改cra构建过程并不容易,您可以将其弹出,但之后必须创建自己的文件。但是,在您的package.json中,您可以定义要在构建之后启动的任务:

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "postbuild": "node yourNodeGzipScript.js"   }

希望能有所帮助。

您也可以在package.json中添加postbuild脚本,其中包含:

"postbuild": "find ./build -type f -name '*.css' -o -name '*.js' -exec gzip -k '{}' \\;"
安装gzip程序包()。 像这样修改build命令

"build": "react-scripts build && gzipper --verbose ./build"
并构建您的项目,您将获得gzip和常规文件。由您决定让服务器服务于gzip而不是普通文件。如果您使用的是nginx,则必须在nginx.conf文件中设置服务器,如下所示

server {
        # Gzip Settings
        gzip on;
        gzip_static on; # allows pre-serving of .gz file if it exists 
        gzip_disable "msie6"; # Disable for user-agent Internet explorer 6. Not supported.
        gzip_proxied any; # enable gzip for all proxied requests
        gzip_buffers 16 8k; # number and size of buffers to compress a response
        gzip_http_version 1.1;
        gzip_min_length 256; # Only gzip files of size in bytes
        gzip_types text/plain text/css text/html application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
        gunzip on; # Uncompress on the fly
        listen       4000;
        server_name  localhost;



        location / {
            root   html/react-app;
            index  index.html index.htm;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


    }

我使用这个命令使gzip文件保持相同的名称

- yarn global add gzipper
- gzipper compress ./build ./gzip_build --output-file-format [filename].[ext] --verbose

仅供参考:gzipper--verbose给了我一个错误。缺少“compress”。

我可以压缩文件,但无法更改注入的文件名,因为.gz必须包含在最后一个文件名中,例如从**.chunks.js更改为**chunks.js.gz您是否尝试过使用“fs”包的“重命名”或“复制”方法?是的,我这样做了,我制作了一个.js文件并使用“fs”更新脚本和css标记。而且成功了。。谢谢,伙计。通常我会通过CloudFront压缩文件。请看:@CharlieHileman我是在CloudFront的帮助下完成的,它工作得很好。谢谢你的主意。这有如何做的细节。我已经从云端解决了这个问题。但谢谢你的解决方案。:)这应该是公认的答案。最详细、最有助于确保gzip_静态开启;效验如神