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.
5 Answers
Angular-cli team has removed the support for generating compress files (.gz). Github discussion url.
We can use a gulp task for it. Install following npm modules:
$npm install --save-dev gulp
$npm install --save-dev gulp-gzip
Create gulpfile.js
. The file name has to be gulpfile.js
not any other names.
var gulp = require('gulp');
var gzip = require('gulp-gzip');
gulp.task('compress', function() {
return gulp.src(['./dist/**/*.*'])
.pipe(gzip())
.pipe(gulp.dest('./dist'));
});
Since .gz
support can be configure in web servers but server has to do the zipping it self and expense some cpu cycles for it. If we pre build it then it helps server to save some cpu cycles. :)
We can add it in package.json
as script to run after each build
of application.
"scripts": {
...
"postbuild": "gulp compress"
...
}
-
3Where to add this gulpfile.js ? – Suresh Kumar Feb 06 '19 at 07:47
-
3Hi @SureshKumar, You can keep the file at root of project or at same level of `package.json` – Amitesh Mar 03 '19 at 18:50
-
Is it possible to gzip the files created by `ng serve`? – Matt DeKok Jul 03 '19 at 21:14
-
@Amitesh for me it is increasing the dist size, any idea why? – Rahul Purohit Sep 05 '19 at 11:05
-
To prevent "The following tasks did not complete: compress. Did you forget to signal async completion?", add a return to the gulp task function, e.g. `return gulp.src(...);`. – Pieter Jan 06 '20 at 09:09
-
Thanks @Pieter. It was very old snippet. Updated with your suggestion. – Amitesh Jan 25 '20 at 07:26
-
@Amitesh which job i then need to do on server? – van9petryk Mar 29 '21 at 16:43
-
@van9petryk you can use any CI job to run project `build` command and post build command it will run. – Amitesh Apr 07 '21 at 15:33
-
1how to test this on local machine. when i do ng serve? I follow the above steps but still file are not compressed. @Amitesh – Okasha Rafique May 12 '21 at 10:31
-
@OkashaRafique this script runs after the `npm run build` command. – Amitesh May 25 '21 at 03:39
-
1@OkashaRafique this script runs after the `npm run build` command. – Amitesh May 25 '21 at 03:39
-
1@Amitesh thanks. I published it on IIS and also added URL review package in IIS then i was able to test it. – Okasha Rafique May 25 '21 at 12:36
-
I can measure the bundle size after using gulfile sript? – Rebai Ahmed Oct 27 '22 at 13:31
-
You can append another shell command after this `gulp compress && measure.sh`. measure.sh will be like - https://www.geeksforgeeks.org/shell-script-to-measure-size-of-a-file/ @RebaiAhmed – Amitesh Nov 29 '22 at 12:00
-
Ok so this gulp script creates .gz files for alle scripts and css files. However the index.html.gz file still links to .js files in stead of .js.gz files. How to fix this? – Lapidus Mar 13 '23 at 17:30
-
Hi @Lapidus, Browser always requests the files without the `.gz` extension for any resource. The browser sends a header to the server to send the compressed version of this resource and the browser can accept it. In return, Webserver read the header, and if it has `.gz` extension files then the server sends the compressed file with a header where it tells the compression type (gzip) to inflate it on the browser. https://blog.hubspot.com/website/gzip-compression Hence we don't need to add`.gz` in html file. – Amitesh Apr 07 '23 at 12:51
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/*",
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.
EDIT: 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).

- 1,069
- 8
- 26
-
2
-
somehow it is failing `tar -zcvf archive.tar.gz dist/prod/*"` command, even I have `tar` package already installed – Pankaj Parkar Jan 26 '18 at 15:08
-
This server compression is the magic. But why angular does not provide any compression itself like this? – muasif80 Jul 23 '19 at 17:53
-
My understanding is that the cli team removed the .gz compression option because most servers will compress files automatically before serving them to the browser (as long as the browser supports compressed responses - which most do).

- 20,076
- 6
- 59
- 90
-
2What is an alternative to this? I want compression in my Angular7, Ionic 4 project? – Mohammad Ayoub Khan Jul 14 '19 at 06:34
I have found more simple solution to solve this issue. Try this:
npm i --save-dev gzipper
and in your angular-cli.json just add this gzipper --verbose ./dist
to your build command like:
ng build && gzipper --verbose ./dist

- 739
- 12
- 23
-
1@iliya.rudberg the command `ng build --prod && gzipper --verbose ./dist` creates the compressed files but once deployed, still refers the js files. any idea why that is? – Uthpala Pathirana Jul 31 '19 at 07:19
-
@UthpalaPathirana you means that browser didn't getting compressed files? Which server do you use to host your files ? If nginx, check this link - https://stackoverflow.com/questions/12640014/enable-gzip-for-css-and-js-files-on-nginx-server-for-magento – iliya.rudberg Aug 13 '19 at 14:49
-
I figured that SSL needs to be enabled to get the pre-compressed files, even when the server is configured to get those with URL-rewrite in IIS. – Uthpala Pathirana Aug 15 '19 at 07:05
-
3this is my preferred solution. Hint: In the new version of gzipper you have to write ng build && gzipper compress ./dist --verbose – b0lle Oct 06 '20 at 18:26
-
After run "ng build --prod && gzipper ./dist" i upload all files to Azure Static Web Storage, but when visite web site still using uncompressed version... can somebody point me on rigth direction? – Pablo Sanchez Manzano Jun 29 '21 at 20:55
-
After this I've got duplicated files ( the compressed and uncompressed ). May I delete the uncompressed ones? And what about the index.html references that points to the uncompressed files? This command made a really mess into dist folder. – Magno C Oct 11 '22 at 15:35
-
@iliya.rudberg I have the same question. how do I change the index.html to reference the .gz files? – Jonathas Sucupira Aug 02 '23 at 16:22
I think you need to enable gzip encoding on your api. This way, the client will ask for compressed resources from the server.

- 582
- 4
- 10