javascript - creating tasks using a loop [gulp] -


i'm trying dynamically create tasks (minify , concat) based on jsfiles object. key give destination file name , array contains src files. when run gulp see tasks names being ran writes last key group2.js in case. what's wrong here?

// imports here  var jsfiles =  {     group1:[file1.js,file2.js],     group2:[file2.js,file3.js] };  (var key in jsfiles) {     gulp.task(key, function() {         return gulp.src(jsfiles[key])         .pipe(jshint())         .pipe(uglify())         .pipe(concat(key + '.js'))  // <- here         .pipe(gulp.dest('public/js'));     }); }  var defaulttasks = []; (var key in jsfiles) {     defaulttasks.push(key); } gulp.task('default', defaulttasks); 

another option use functional array looping functions combined object.keys, so:

var defaulttasks = object.keys(jsfiles);  defaulttasks.foreach(function(taskname) {    gulp.task(taskname, function() {        return gulp.src(jsfiles[taskname])           .pipe(jshint())           .pipe(uglify())           .pipe(concat(key + '.js'))           .pipe(gulp.dest('public/js'));    }); }); 

i feel little cleaner, because have loop , function in same place, it's easier maintain.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -