如何在angular cli中启用gzip压缩以进行生产构建 [英] how to enable gzip compression in angular cli for production build

查看:656
本文介绍了如何在angular cli中启用gzip压缩以进行生产构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想压缩在构建angular项目时创建的捆绑文件.我现在使用ng build --environment=${environment}来构建应用程序,并且"@angular/compiler-cli": "^4.0.0"的版本不会将.gz文件生成到dist文件夹.生成.gz捆绑包文件(最好不触摸webpack.config.js文件)的最简单方法是什么? PS:我知道创建.gz文件的选项在某个时候被angular/cli团队删除了.但是我迫切需要它,因为我的捆绑文件很大.

I would want to compress the bundle files that are created when building the angular project. I use ng build --environment=${environment} to build the application currently and the version of "@angular/compiler-cli": "^4.0.0" do not generate the .gz files to the dist folder. What is the simplest way to generate .gz bundle files (preferable without touching webpack.config.js file)? PS: I knew the option for creating the .gz files was removed by the angular/cli team sometime back. But I need that desperately as my bundle files are huge.

推荐答案

您可以使用简单的bash脚本实现此目的,然后再将其传输到服务器,甚至可以将其作为命令添加到package.json中

You could achieve this with a simple bash script before transferring them to the server, or even adding it to package.json as a command

 "scripts": {
   "build.prod": "ng build --environment=prod && tar -zcvf archive.tar.gz dist/prod/*",

不确定您的文件夹结构是什么,但是您可以在终端中使用tar -zcvf archive.tar.gz dist/prod/*进行操作,直到找到适合您需求的路径.

Not sure what's your folder structure, but you can play around with tar -zcvf archive.tar.gz dist/prod/* in the terminal until you find paths that suite to your needs.

似乎我误解了这个问题,如果在向最终用户提供东西时是关于捆绑包的大小,那么您应该查看AOT +汇总以最小化捆绑包的大小.并在提供文件时在您的Web服务器上启用gzip压缩(可能大多数服务器已经启用了该功能).

Seems I misunderstood the question, if it is about bundle size when serving the stuff to the end user, you should take a look at AOT + Rollup to minimize your bundle sizes. And enable gzip compression on your webserver when serving files (probably most servers have it enabled already).

这篇关于如何在angular cli中启用gzip压缩以进行生产构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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