Skip to content

Instantly share code, notes, and snippets.

@dashed
Created January 7, 2014 12:38
Show Gist options
  • Save dashed/8298724 to your computer and use it in GitHub Desktop.
Save dashed/8298724 to your computer and use it in GitHub Desktop.
Interesting and useful gulp snippets.
/*
From https://github.com/gulpjs/gulp/issues/101. Organize gulpfile.js using concept of environment variables.
1. Set up sub-tasks to behave in a certain according to env var; may be env agnostic.
2. Set up high-level tasks to run a group of sub-tasks to orchestrate a behaviour (testing, staging, prod, etc)
*/
var R = 0;
var ENV_SWITCH = void 0;
// define env
var DEV_ENV = R++;
var PROD_ENV = R++;
// Transformation function on gulp.src depending on env
var getGlob = function(env, glob_target) {
var src = gulp.src(glob_target);
switch(env) {
case DEV_ENV:
return src.pipe(watch());
case PROD_ENV:
return src;
default:
throw new Error('Invalid Env');
}
}
/*
Tasks that describe build subroutines. Task names can be thought of as categories.
Set up subroutines to behave depending on ENV_SWITCH.
You do this so you don't need to repeat code at a higher task-level.
*/
gulp.task('vendor-scripts', function() {
gulp.src('client/js/vendor/**')
.pipe(gulp.dest('build/js/vendor'));
});
gulp.task('project-scripts', function() {
getGlob(ENV_SWITCH, ['client/js/**/*.js', '!client/js/vendor/**'])
.pipe(uglify())
.pipe(gulp.dest('build/js'));
});
gulp.task('assets', function() {
getGlob(ENV_SWITCH, 'client/img/**')
.pipe(gulp.dest('build/img'));
getGlob(ENV_SWITCH, 'client/css/**')
.pipe(gulp.dest('build/css'));
getGlob(ENV_SWITCH, 'client/*.html')
.pipe(gulp.dest('build'));
});
// High-level tasks that compose build subroutines to orchestrate a behaviour.
gulp.task('prod', function() {
ENV_SWITCH = PROD_ENV;
// Run categorical sub-tasks
gulp.run('vendor-scripts' 'project-scripts', 'assets');
});
gulp.task('dev', function() {
ENV_SWITCH = DEV_ENV;
gulp.run('project-scripts', 'assets');
});
gulp.task('test', function() {
ENV_SWITCH = TEST_ENV;
// To be set up...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment