question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

gulp.series() could be friendlier to run inside a function

See original GitHub issue

There are a few scenarios where I’ve been wanting to write a task similar to:

gulp.task('prod', () => {
    process.env.NODE_ENV = 'production';

    return gulp.series([
        'build'
        'serviceWorker',
    ]);
});

This doesn’t work because gulp.series() returns a function, and the execution of a task is expected to return a promise or a stream.

So executing gulp.series, like so:

gulp.task('prod', () => {
    process.env.NODE_ENV = 'production';

    return gulp.series([
        'build'
        'serviceWorker',
    ])();
});

Running the prod task, it’ll correctly run the series tasks, but at the end it’ll print:

[11:10:12] The following tasks did not complete: prod [11:10:12] Did you forget to signal async completion?

In the end I need to write:

gulp.task('prod', (done) => {
    process.env.NODE_ENV = 'production';

    return gulp.series([
        'build'
        'serviceWorker',
    ])(done);
});

This works, but it would have been amazing if:

  1. Returning a function as a result of a gulp task would cause the function to be run. This means developers could do:
    gulp.task('example', () => {
        // TODO: Do stuff here
        return gulp.series([....]);  
    });
    
  2. The return type of gulp.series()() was suitable for gulp.task to be happy once it’s completed (i.e. return a promise or stream or gulp.task expect the result).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:20 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
yocontracommented, May 16, 2018

@nevercast the “gulp way” is to go plain js wherever possible, I would write your stuff as simply:

const deploy = async () => {
  log(`Starting deploy to ${configuration.serverName}`)
  const deployer = createDeployerService()
  await validateServer(configuration.serverName)
  await deleteFilesFromServer(configuration.serverName)
  await uploadDistToServer(configuration.serverName)
}

const deployTask = gulp.series(
  loadConfiguration,
  validateConfiguration,
  buildWebpack,
  jest,
  karma,
  deploy
)

export default deployTask

You only need gulp.series/gulp.parallel when you start needing nesting, stream completion, callbacks, etc. - if you are using simple promises you can just use async/await normal JS stuff.

2reactions
phatedcommented, Feb 4, 2018

@gauntface hmmm. The general idea of these APIs is to compose functions at the top level. The pattern you showed is something that can be done due to the implementation but it’s not a recommended pattern (thus looks bad). If we added your suggestion, I feel like many more people would be adding extra function wrappers into their programs (like gulp.task('example', () => gulp.series(...));) which we really don’t want.

I think most of these scenarios go away when people utilize flags and task metadata). (And named functions in the cases where they are dodging the forward reference changes)

if (process.argv.includes('--prod')) {
  process.env.NODE_ENV = 'production'
}

// "prod" no longer makes sense
export const generate = gulp.series(build, serviceWorker);
generate.flags = {
  '--prod': "Set NODE_ENV to 'production'"
};

I understand you provided a pretty basic example and I’d like to see other samples to see how they could be converted to not add another function wrapper around gulp.series/gulp.parallel

Read more comments on GitHub >

github_iconTop Results From Across the Web

GulpJS 4.0.2 series inside a function called by a series does ...
I am using gulp 4.0.2. In reality I have the second function registered as a task in a different file and that is...
Read more >
series() - gulp.js
series() #. Combines task functions and/or composed operations into larger operations that will be executed one after another, in sequential order.
Read more >
An Introduction to Gulp.js - Section 1 - SitePoint
The exported html task uses gulp.series() to combine tasks that are executed one after the other. In this case, the images() function is...
Read more >
Building a Gulp API Compatible Task Automation Tool - Toptal
In version 4.0, Gulp introduced two new functions: gulp.series and gulp.parallel. These APIs allow tasks to be run in series or in parallel....
Read more >
Gulp 4 tutorial for beginners (Sass and JS workflow)
Gulp is a tool that will run various tasks for you in your web ... notice that we are putting all those tasks...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found