S
S
Sergey Saburkin2021-08-13 12:58:44
gulp.js
Sergey Saburkin, 2021-08-13 12:58:44

What is the difference in gulp, return gulp.src(...) and cb()?

1- way via cb

const html = (cb) => {
   gulp.src(config.src.html)
      .pipe(plumber())
      .pipe(include())
      .pipe(gulpif(config.isProd, htmlmin({
         collapseWhitespace: true,
         removeComments: true,
      })))
      .pipe(gulp.dest(config.build.html))
   cb();
}


2- way via return

const html = () => {
   return gulp.src(config.src.html)
      .pipe(plumber())
      .pipe(include())
      .pipe(gulpif(config.isProd, htmlmin({
         collapseWhitespace: true,
         removeComments: true,
      })))
      .pipe(gulp.dest(config.build.html))
}

The only difference I see is in the speed of the build, if you use the cb () method.
Which of these methods is better or when is it better to use one or the other and why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2021-08-13
@sabyrkin

In the first case, your callback fires immediately, and not after the task is completed.
It is correct to write like this:

const html = (cb) => {
   gulp.src(config.src.html)
      .on('end', cb) // Вызвать коллбэк после завершения таска.
      .pipe(plumber())
      .pipe(include())
      .pipe(gulpif(config.isProd, htmlmin({
         collapseWhitespace: true,
         removeComments: true,
      })))
      .pipe(gulp.dest(config.build.html))
}

And when it is written like that, there is no difference which of the options to use.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question