WillSquire of Parent
7/23/2015 - 1:50 PM

Gulp - SCSS and Javascript transpiler. For Javascript (polyfill ECMA6 to ECMA5 code, minify and auto set UNIX file permissions). For SCSS (S

Gulp - SCSS and Javascript transpiler. For Javascript (polyfill ECMA6 to ECMA5 code, minify and auto set UNIX file permissions). For SCSS (SCSS to CSS, minify, auto adds style prefixes for different browsers, auto set UNIX file permissions).Note Gulp is Node.js based.

/*!
 * Gulp
 * ----
 *
 * Installation
 * ============
 *
 * In the commandline, cd to this folder. Note that sudo privileges may be needed to run the required commands.
 *
 * First install gulp globally (if this hasn't been done already, check with `gulp -v`):
 * `npm install gulp -g`
 *
 * Next setup the `package.json` file using npm initialisation:
 * `npm init`
 *
 * Install gulp to the current working directory with:
 * `npm install gulp --save-dev`
 *
 * Now retrieve the dependencies needed below using:
 * `npm install yargs gulp-autoprefixer babelify browserify vinyl-buffer gulp-chmod del gulp-if gulp-notify gulp-rename gulp-sass vinyl-source-stream gulp-sourcemaps gulp-uglify --save-dev`
 *
 *
 * Configuration
 * =============
 *
 * First configure this file's variables to suit the environment and needs. Here is a breakdown of what the configurable variable do:
 *
 * `scssSource` - is the folder container SCSS files to be transpiled into CSS.
 * `cssDestination` - is the folder for containing transpiled CSS files.
 * `javascriptAppSourceFile` - is the main javascript file that requires all other javascript modules (to transpile to one file).
 * `javascriptSource` - is the folder containing the originating Javascript files.
 * `javascriptDestination` - is the folder for containing the transpiled Javascript files.
 * `filePermissions` - is the file permissions that the transpiled files will have.
 *
 *
 * Usage
 * =====
 *
 * ### WARNING: This tool will remove anything within "stylesDestination" and "scriptsDestination" upon transpiling.
 * Please ensure these folders are empty to store the output from this only.
 *
 * In the commandline, cd to this folder and use these commands to execute the following tasks:
 *
 * `gulp` - to process default task.
 * `gulp styles` - to compile SCSS files to CSS.
 * `gulp scripts` - to transpile Javascript files to pollyfilled ES5 and minified Javascript.
 * `gulp watch` - to watch styling and scripting files for changes and process automatically. Cancel watch with `Ctrl C`.
 * `gulp watch:css` - to process styling files and watch these for changes to process automatically. Cancel watch with `Ctrl C`.
 * `gulp watch:js` - to process scripting files and watch these for changes to process automatically. Cancel watch with `Ctrl C`.
 *
 *  Whilst in development, use `--debug` at the end of these commands to generate source maps for the browser.
 *  Source maps map transpiled code back to the original source, showing the correct line the code was at in the browser.
 *  For example, use `gulp watch --debug` to auto transpile code with source maps.
 *
 */

'use strict';

// Configuration
var scssSource = 'assets/build/scss', // Folder for
    cssDestination = 'assets/distribution/css',
    javascriptMainAppSourceFile = 'app.js',
    javascriptSource = 'assets/build/javascript',
    javascriptDestination = 'assets/distribution/javascript',
    filePermissions = 444; // 6=RW 4=R

// Load plugins
var gulp = require('gulp'),
    argv = require('yargs').argv,
    autoprefixer = require('gulp-autoprefixer'),
    babelify = require('babelify'),
    browserify = require('browserify'),
    buffer = require('vinyl-buffer'),
    chmod = require('gulp-chmod'),
    del = require('del'),
    gulpif = require('gulp-if'),
    notify = require('gulp-notify'),
    rename = require('gulp-rename'),
    sass = require('gulp-sass'),
    source = require('vinyl-source-stream'),
    sourcemaps = require('gulp-sourcemaps'),
    uglify = require('gulp-uglify');

// CSS
gulp.task('styles', ['clean:css'], function() {
    return gulp.src(scssSource + '/**/*.scss')
        .pipe(gulpif(argv.debug, sourcemaps.init()))
        .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
        .pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
        .pipe(gulpif(argv.debug, sourcemaps.write()))
        .pipe(rename({ suffix: '.min' }))
        .pipe(chmod(filePermissions))
        .pipe(gulp.dest(cssDestination))
        .pipe(notify({ message: 'Styles task complete' }));
});

// Javascript
gulp.task('scripts', ['clean:js'], function() {
    //return gulp.src(scriptsSource + '/**/*.js')
    browserify({
        entries: javascriptSource + '/' + javascriptMainAppSourceFile,
        debug: true
    })
        .transform(babelify)
        .bundle()
        .pipe(source(javascriptMainAppSourceFile))
        .pipe(buffer())
        .pipe(gulpif(argv.debug, sourcemaps.init({ loadMaps: true })))
        .pipe(uglify())
        //.on('error', gutil.log)
        .pipe(gulpif(argv.debug, sourcemaps.write()))
        .pipe(rename({ suffix: '.min' }))
        .pipe(chmod(filePermissions))
        .pipe(gulp.dest(javascriptDestination))
        .pipe(notify({ message: 'Scripts task complete' }));
});

// Clean
gulp.task('clean', ['clean:css', 'clean:js']); // All
gulp.task('clean:css', function(cb) { // CSS
    del([cssDestination + '/**/*'], cb)
});
gulp.task('clean:js', function(cb) { // JS
    del([javascriptDestination + '/**/*'], cb)
});

// Watch
gulp.task('watch', ['styles', 'scripts'], function() {
    gulp.start('watch:css', 'watch:js');
});
gulp.task('watch:css', function() { // CSS
    // Watch .scss files
    gulp.watch(scssSource + '/**/*.scss', ['styles']);
});
gulp.task('watch:js', function() { // JS
    // Watch .js files
    gulp.watch(javascriptSource + '/**/*.js', ['scripts']);
});

// Default task
gulp.task('default', function() {
    gulp.start('styles', 'scripts');
});

// TODO:
// Add callbacks to functions to make sure they run in order.
// Support bundling multiple js files.
// Look into minifying HTML aswell