Last active
December 29, 2016 13:23
-
-
Save diegoos/6174e243489268d293f4da0146c8d2f9 to your computer and use it in GitHub Desktop.
Gulp config with sass
This file contains 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
'use strict'; | |
// Gulp modules | |
const gulp = require('gulp'); | |
const gutil = require('gulp-util'); | |
const concat = require('gulp-concat'); | |
const sass = require('gulp-sass'); | |
const gInclude = require('gulp-include'); | |
const gulpif = require('gulp-if'); | |
const sprity = require('sprity'); | |
const cleanCSS = require('gulp-clean-css'); | |
const uglifyjs = require('gulp-uglify'); | |
const notify = require('gulp-notify'); | |
const babel = require('gulp-babel'); | |
/** | |
* Check if is productions environment | |
* @method isProduction | |
* @return {Boolean} [description] | |
*/ | |
const isProduction = function() { | |
if (gutil.env.env === 'prod') { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/** | |
* The erro message | |
* @type {Object} | |
*/ | |
const errorObj = { | |
title : "Assets", | |
subtitle : "Failure on compiling!", | |
message : "Error: <%= error.message %>", | |
sound : "Sosumi" | |
}; | |
/** | |
* The success message | |
* @type {Object} | |
*/ | |
const successObj = { | |
title : "Assets", | |
subtitle : "Compiled with success!", | |
message : "File generated!", | |
sound : "Beep" | |
}; | |
/** | |
* Show notification on error | |
* @param {[type]} err [description] | |
* @return {[type]} [description] | |
*/ | |
const onError = function(err) { | |
if(gutil.env.env != 'prod'){ | |
notify.onError(errorObj)(err); | |
this.emit('end'); | |
} | |
}; | |
/** | |
* Compile all scss and css that are included in this file | |
* @type {String} | |
*/ | |
const compileSass = "./assets/stylesheet/application.scss"; | |
const compileJs = "./assets/javascript/application.js"; | |
/** | |
* Path to assets | |
* @type {Object} | |
*/ | |
const assetPath = { | |
"js" : "./assets/javascript/**/*.js", | |
"sass" : "./assets/stylesheet/**/*.scss", | |
"css" : "./assets/stylesheet/**/*.css", | |
}; | |
/** | |
* Where the assets are compiled | |
* @type {Object} | |
*/ | |
const compileTo = { | |
'js' : { | |
'path' : './', | |
'file' : 'compiled.js', | |
}, | |
'css' : { | |
'path' : './', | |
'file' : 'compiled.css', | |
} | |
}; | |
/** | |
* Define what the gulp will run | |
*/ | |
gulp.task('default', [ 'css', 'js' ], function() { | |
gulp.watch([ assetPath.sass, assetPath.css, assetPath.js ], [ 'css', 'js' ]); | |
}); | |
gulp.task('build', [ 'css', 'js' ]); | |
/** | |
* Task to compile css | |
*/ | |
gulp.task('css', function() { | |
const sassOptions = { | |
outputStyle: isProduction === true ? "compressed" : "expanded", | |
errLogToConsole: true | |
} | |
gulp.src(compileSass) | |
.pipe(sass(sassOptions).on('error', onError)) | |
.pipe(gulpif(isProduction, cleanCSS({ compatibility: 'ie8' }).on('error', onError), gutil.noop())) | |
.pipe(concat(compileTo.css.file)) | |
.pipe(gulp.dest(compileTo.css.path)) | |
.pipe(gulpif(isProduction, gutil.noop(), notify(successObj))); | |
}); | |
/** | |
* Task to compile javascript | |
*/ | |
gulp.task('js', function() { | |
const uglifyOptions = { | |
preserveComments: 'license' | |
}; | |
gulp.src(compileJs) | |
.pipe(gInclude().on('error', onError).on('error', onError)) | |
.pipe(babel({presets: ['es2015']})) | |
.pipe(gulpif(isProduction, uglifyjs(uglifyOptions).on('error', onError), gutil.noop())) | |
.pipe(concat(compileTo.js.file).on('error', onError)) | |
.pipe(gulp.dest(compileTo.js.path)) | |
.pipe(gulpif(isProduction, gutil.noop(), notify(successObj))); | |
}); | |
/** | |
* generate sprite.png and _sprite.scss | |
*/ | |
gulp.task('sprites', function() { | |
sprity.src({ | |
src: './assets/images/sprites/*.{png,jpg}', | |
style: './sprites.scss', | |
orientation: 'binary-tree', | |
cssPath: './assets/images/' | |
}) | |
.pipe(gulpif('*.png', gulp.dest('./assets/images/'), gulp.dest('./assets/stylesheet/'))) | |
}); |
This file contains 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
{ | |
"name": "Project_Name", | |
"version": "1.0.0", | |
"description": "Project Description", | |
"author": "diegoos", | |
"devDependencies": { | |
"babel-preset-es2015": "^6.18.0", | |
"gulp": "^3.9.1", | |
"gulp-babel": "^6.1.2", | |
"gulp-clean-css": "^2.0.12", | |
"gulp-concat": "^2.6.0", | |
"gulp-if": "^2.0.2", | |
"gulp-include": "^2.3.1", | |
"gulp-notify": "^2.2.0", | |
"gulp-sass": "^2.3.2", | |
"gulp-uglify": "^2.0.0", | |
"gulp-util": "^3.0.7", | |
"sprity": "^1.0.8", | |
"sprity-sass": "^1.0.4" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment