Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to enable gzip compression in angular cli for production build

Tags:

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.

like image 340
Cling Avatar asked Jun 26 '17 11:06

Cling


People also ask

How do I enable gzip compression?

Gzip on Windows Servers (IIS Manager)Open up IIS Manager. Click on the site you want to enable compression for. Click on Compression (under IIS) Now Enable static compression and you are done!

How do you check gzip compression is enabled or not?

Double click on the file and select headers. Under 'Response headers' you are looking for the 'Connection-Encoding' field, it will say gzip if it is enabled.

Is Brotli better than gzip?

Brotli has a better compression ratio (i.e. it produces smaller compressed files) across every level of compression. While GZIP does beat Brotli on speed most of the time, the level you compress at factors into the results you'll see.

How do I enable gzip in Apache?

To turn on Gzip compression, simply add on to the gzip directive in the main Nginx configuration file. $ sudo nano /etc/nginx/nginx. conf gzip on; Add file types to compress.


1 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

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"
       ...
   }
like image 106
Amitesh Avatar answered Sep 29 '22 18:09

Amitesh