0

I have this code so far:

gulp.task('make_prod_aa', function () {
    makeAppHtml(config.srcAalHtml, function () {
        runSequence(
          'makeTemplate',
          'make_css_bundle',
          'rename_css_bundle',
          'make_js_bundle',
          'rename_js_bundle',
          function () {
              makeAppIndex('index-aa.html');
          });
    });
});

It allows me to pass parameters into two functions and runs the functions and the tasks all in order. Now I would like to pass parameters into a third task. I want to be able to pass the config.aaTemplates parameter to the task makeTemplate which is here:

gulp.task('makeTemplate', function () {
    return gulp.src(config.aaTemplates)
          .pipe(print(function (file) {
              return "Added file " + file + " to template";
          }))
       .pipe(minifyHTML({ collapseWhitespace: true }))
       .pipe(templateCache({ standalone: true, root: '/app' }))
       .pipe(gulp.dest(config.destPartials))
       .pipe(gzip(gzip_options))
       .pipe(gulp.dest(config.destPartials));
});

Would appreciate any suggestions on how I can do this.

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • Just apply the exact same refactoring that I described to you in [this answer](http://stackoverflow.com/questions/36644578#36651822) to your `makeTemplate` task. Of course, if you do that with every task in your `runSequence()` you will eventually just end up with lots of nested callbacks, aka [callback hell](http://stackoverflow.com/questions/25098066/). At that point its probably best to look into something like [`async.waterfall()`](https://github.com/caolan/async#waterfall). – Sven Schoenung Apr 16 '16 at 09:52

1 Answers1

2

I don't think there is another option than using JS closure to have all tasks which run in a sequence sharing the same config. Proof

For example:

var config = require('path/to/config');

function makAppAppHtml () {
        runSequence(
          'makeTemplate',
          'make_css_bundle',
          'rename_css_bundle',
          'make_js_bundle',
          'rename_js_bundle',
          function () {
              makeAppIndex('index-aa.html');
          });
}

function makeTemplate () {
    return gulp.src(config.aaTemplates)
            .pipe(print(function (file) {
                return "Added file " + file + " to template";
            }))
         .pipe(minifyHTML({ collapseWhitespace: true }))
         .pipe(templateCache({ standalone: true, root: '/app' }))
         .pipe(gulp.dest(config.destPartials))
         .pipe(gzip(gzip_options))
         .pipe(gulp.dest(config.destPartials));
  });
}

gulp.task('make_prod_aa', makAppAppHtml);
gulp.task('makeTemplate', makeTemplate);

Both makeAppHtml and makeTemplate have access to config object because it was defined in the outer scope, the same trick could be applied to the function scope, for more detailed example I recommend JavaScript Garden article.

Yevgen Safronov
  • 3,977
  • 1
  • 27
  • 38
  • can you explain about JS closure. I heard people talking about Closure but don't know anything about it. If you could give an example that would be really good. Thank you. – Alan2 Apr 16 '16 at 09:43