Last active
November 11, 2015 17:35
-
-
Save luigimannoni/f98d0ab198fa00c5363d to your computer and use it in GitHub Desktop.
Gulp tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Gulp | |
| var gulp = require('gulp'); | |
| // Gulp plug-ins | |
| var watch = require('gulp-watch'), // Watcher, runs tasks on file change | |
| concat = require('gulp-concat'), // Concatenation of files | |
| jshint = require('gulp-jshint'), // JS Linting | |
| compass = require('gulp-compass'), // SASS compilator including Compass mixin libraries. | |
| jade = require('gulp-jade'), // Compiles .jade files into raw HTML | |
| minifyCSS = require('gulp-minify-css'), // Minify CSS | |
| uglify = require('gulp-uglify'), // Uglifies JS files | |
| stripDebug = require('gulp-strip-debug'), // Strips all console.xxx commands from JS files. | |
| rimraf = require('rimraf'), // Deletes files and folders | |
| plumber = require('gulp-plumber'), // Error pipe streaming | |
| util = require('gulp-util'), // Extends gulp utilities | |
| merge = require('gulp-merge'), // Merges multiple streams | |
| notify = require('gulp-notify'); // Toast notifications for MacOS and Windows | |
| var paths = { | |
| jade: [ | |
| './src/jade/**', | |
| ], | |
| sass: [ | |
| './src/sass/**', | |
| ], | |
| angularScripts: [ | |
| './src/js/**', | |
| ], | |
| }; | |
| // Error reporting function. | |
| var reportError = function (error) { | |
| // Log into console and beep. | |
| util.log(util.colors.red('Error'), error.message); | |
| util.beep(); | |
| // Toast message | |
| notify({ | |
| title: 'Task Failed [' + error.plugin + ']', | |
| message: error.message, | |
| }).write(error); | |
| // Prevent the 'watch' task from stopping | |
| this.emit('end'); | |
| }; | |
| // Error reporting for Rimraf. | |
| var rimrafCBHandler = function (cb) { | |
| if (cb == null) { | |
| util.log(util.colors.green('Folder deleted.')); | |
| } | |
| else { | |
| util.log(util.colors.red('Rimraf Error:'), cb); | |
| util.beep(); | |
| // Toast message | |
| notify({ | |
| title: 'RimRaf has thrown an exception', | |
| message: cb, | |
| }).write(cb); | |
| } | |
| }; | |
| /*** | |
| *** !!! THIS TASK DELETES FILES !!! | |
| *** | |
| *** BE EXTREMELY CAUTIOUS ON CHANGING ANYTHING ON THIS TASK AS | |
| *** IT CAN RESULT IN SOURCE FILES DELETED FUCKING UP THE PROJECT. | |
| ***/ | |
| gulp.task('clean', function() { | |
| rimraf('./.sass-cache', rimrafCBHandler); | |
| rimraf('./templates', rimrafCBHandler); | |
| rimraf('./assets', rimrafCBHandler); | |
| }); | |
| // Copy assets from SRC folder | |
| gulp.task('copyassets', function() { | |
| return gulp.src('./src/assets/**') | |
| .pipe(gulp.dest('./assets')); | |
| }); | |
| // Jade for DEV | |
| gulp.task('jade:dev', function() { | |
| // Compile everything apart from components folder. | |
| return gulp.src(['./src/jade/{*.jade,**/**.jade}', '!./src/jade/{components,components/**}']) | |
| .pipe(plumber({ errorHandler: reportError })) | |
| .pipe(jade({ | |
| pretty: '\t', // Tab indent | |
| doctype: 'html' | |
| })) | |
| .pipe(gulp.dest('./')); | |
| }); | |
| // Jade for Build (compresses HTML) | |
| gulp.task('jade:production', function() { | |
| // Compile everything apart from components folder. | |
| return gulp.src(['./src/jade/{*.jade,**/**.jade}', '!./src/jade/{components,components/**}']) | |
| .pipe(plumber({ errorHandler: reportError })) | |
| .pipe(jade({ | |
| pretty: false, // Whitespace compression | |
| doctype: 'html' | |
| })) | |
| .pipe(gulp.dest('./')); | |
| }); | |
| gulp.task('compass:dev', function() { | |
| return gulp.src('./src/*.scss') | |
| .pipe(plumber({ errorHandler: reportError })) | |
| .pipe(compass({ | |
| css: './assets/css', | |
| sass: './src/sass', | |
| image: './assets/images' | |
| })) | |
| .pipe(gulp.dest('./assets/css')); | |
| }); | |
| gulp.task('compass:production', function() { | |
| return gulp.src('./src/*.scss') | |
| .pipe(plumber({ errorHandler: reportError })) | |
| .pipe(compass({ | |
| css: './assets/css', | |
| sass: './src/sass', | |
| image: './assets/images', | |
| style: 'compressed', | |
| })) | |
| .pipe(gulp.dest('./assets/css')); | |
| }); | |
| // Compile JS for DEV | |
| gulp.task('scripts:dev', function() { | |
| // Pack all the Angular JS | |
| gulp.src(['./src/js/application/**/**.js']) | |
| .pipe(plumber({ errorHandler: reportError })) | |
| .pipe(jshint()) | |
| .pipe(concat('scripts.js')) | |
| .pipe(gulp.dest('./assets/js')); | |
| // Pack all vendor JS | |
| gulp.src('./src/js/vendor/*.js') | |
| .pipe(plumber({ errorHandler: reportError })) | |
| .pipe(jshint()) | |
| .pipe(concat('vendor.js')) | |
| .pipe(gulp.dest('./assets/js/vendor')); | |
| // return merge(appJS, vendorJS); | |
| }); | |
| // Compile JS for Build - Strips console.log, Compresses and uglifies JS. | |
| gulp.task('scripts:production', function() { | |
| // Pack all the Angular JS | |
| gulp.src(['./src/js/application/**/**.js']) | |
| .pipe(plumber({ errorHandler: reportError })) | |
| .pipe(jshint()) | |
| .pipe(concat('scripts.js')) // Concatenate | |
| .pipe(stripDebug()) // Strip all console.log | |
| .pipe(uglify({ | |
| mangle: false, // Skip name obfuscation, fucks up with Angular dependencies. | |
| })) // Minify | |
| .pipe(gulp.dest('./assets/js')); | |
| // Pack all vendor JS | |
| gulp.src('./src/js/vendor/*.js') | |
| .pipe(plumber({ errorHandler: reportError })) | |
| .pipe(jshint()) | |
| .pipe(concat('vendor.js')) // Concatenate | |
| .pipe(stripDebug()) // Strip all console.log | |
| .pipe(uglify({ | |
| mangle: false, // Skip name obfuscation, fucks up with Angular dependencies. | |
| })) // Minify | |
| .pipe(gulp.dest('./assets/js/vendor')); | |
| // return merge(appJS, vendorJS); | |
| }); | |
| // Rerun the task when a file changes | |
| gulp.task('watch', function() { | |
| gulp.watch(paths.angularScripts, ['scripts:dev']); | |
| gulp.watch(paths.jade, ['jade:dev']); | |
| gulp.watch(paths.sass, ['compass:dev']); | |
| }); | |
| // Task batches | |
| gulp.task('default', ['jade:dev', 'scripts:dev', 'compass:dev']); // Quick compile | |
| gulp.task('build:dev', ['copyassets', 'jade:dev', 'scripts:dev', 'compass:dev']); // Compile for dev | |
| gulp.task('build:production', ['copyassets', 'jade:production', 'scripts:production', 'compass:production']); // Compile minified files |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment