Skip to content

Instantly share code, notes, and snippets.

@kbshl
Created September 16, 2014 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbshl/c7177f50caf8505f3439 to your computer and use it in GitHub Desktop.
Save kbshl/c7177f50caf8505f3439 to your computer and use it in GitHub Desktop.
Gulp Receipe: Generating a file per folder
/*
If you have a set of folders, and wish to perform a set of tasks on each, for instance...
/scripts
/scripts/jquery/*.js
/scripts/angularjs/*.js
...and want to end up with...
/scripts
/scripts/jquery.min.js
/scripts/angularjs.min.js
...you'll need to do something like the following...
*/
var fs = require('fs');
var path = require('path');
var merge = require('merge-stream');
var gulp = require('gulp');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var scriptsPath = 'src/scripts';
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
gulp.task('scripts', function() {
var folders = getFolders(scriptsPath);
var tasks = folders.map(function(folder) {
// concat into foldername.js
// write to output
// minify
// rename to folder.min.js
// write to output again
return gulp.src(path.join(scriptsPath, folder, '/*.js'))
.pipe(concat(folder + '.js'))
.pipe(gulp.dest(scriptsPath))
.pipe(uglify())
.pipe(rename(folder + '.min.js'))
.pipe(gulp.dest(scriptsPath));
});
return merge(tasks);
});
/*
A few notes:
folders.map - executes the function once per folder, and returns the async stream
es.concat - combines the streams and ends only when all streams emitted end
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment