0 votes
in JAVA by

How does gulp use streams and what are the benefits of using it?

1 Answer

0 votes
by
As mentioned earlier, that Gulp is a streaming build system, uses node’s streams. So it reads the files and keeps them in memory, perform all the operation in memory, and finally write the file to disk. Here take a look at sample gulp file.

var gulp = require('gulp');

var concat = require('gulp-concat');

var uglify = require('gulp-uglify');

var jshint = require('gulp-jshint');

gulp.task('scripts', function() {

    return gulp.src('js/*.js')

        .pipe(jshint())

        .pipe(jshint.reporter('default'))

        .pipe(uglify())

        .pipe(concat('app.js'))

        .pipe(gulp.dest('build'));});

// Default Task

gulp.task('default', ['scripts']);

Here a single task named “script” does magic for 3 gulp plugins jshint, uglify and concat. It first reads all the js files and pipe it to jshint (which checks for errors) and which passes it on to uglify and uglify will do the its job and pass the updated source to contact which contacts the files in “app.js” file and finally writes to the disk.

So here are advantages of using streams in gulp,

There are less number of file I/O operations, in fact only 2. Just think how grunt will do the same job. Grunt will take the file -> runs the task -> write the file. And repeat the same process for the task. So there are more file I/O operations. Though this feature will be coming soon in grunt as well. But for now, gulp is the winner.

It makes gulp faster, since grunt doesn’t use streams.

In gulp, you can chain multiple tasks but that’s not possible with grunt.

Related questions

0 votes
+1 vote
asked Feb 15, 2020 in JAVA by rahuljain1
0 votes
asked Feb 15, 2020 in JAVA by rahuljain1
...