Skip to content

Instantly share code, notes, and snippets.

@toh82
Created June 19, 2015 12:47
Show Gist options
  • Save toh82/a788b269c063a32a9bb0 to your computer and use it in GitHub Desktop.
Save toh82/a788b269c063a32a9bb0 to your computer and use it in GitHub Desktop.
Gulp Configurable Tasks
{
"theme": "default",
"minifyCss": true,
"uglifyJS": true,
"bless" : {
"useImport": false
},
"bootstrap": {
"js": [
"bower_components/bootstrap/js/transition.js",
"bower_components/bootstrap/js/collapse.js",
"bower_components/bootstrap/js/carousel.js",
"bower_components/bootstrap/js/dropdown.js",
"bower_components/bootstrap/js/modal.js",
"bower_components/bootstrap/js/tab.js"
]
},
"modernizr": {
"detectors": [
"bower_components/modernizr/feature-detects/css-filters.js",
"bower_components/modernizr/feature-detects/forms-formattribute.js"
]
},
"customJsPaths": [],
"htmlSrc": ["src/**"]
}
var config = require('./gulp-config.json');
var gulp = require('gulp'),
less = require('gulp-less'),
minifycss = require('gulp-minify-css'),
bless = require('gulp-bless'),
jshint = require('gulp-jshint'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
util = require('gulp-util'),
notify = require('gulp-notify'),
rimraf = require('gulp-rimraf'),
fileinclude = require('gulp-file-include');
var oPath = {
basePath: "../",
defaultTheme: "default/",
parts: {
less: "less/style.less",
js: "js/*.js",
dist: "compiled/"
}
};
/**
* custom reporter for notify
*/
var customNotify = notify.withReporter(function (options, callback) {
callback();
});
/**
* method to deliver correct file paths to tasks
* according to path object oPath
*
* @param {string} part - choose between less, js and dist
* @return {array}
*/
var getPath = function (part) {
var theme = (config.theme !== '') ? config.theme : oPath.defaultTheme;
// for part js we have to return custom theme and default theme folder
if(config.theme !== '' && part === 'js') {
var aPaths = [
oPath.basePath + theme + '/' + oPath.parts[part],
oPath.basePath + oPath.defaultTheme + oPath.parts[part]
];
config.customJsPaths.forEach(function(sPath) {
aPaths.push(sPath);
});
return aPaths;
}
return [oPath.basePath + theme + '/' + oPath.parts[part]];
};
// LINT
gulp.task('lint', ['clean'], function() {
return gulp.src(getPath('js'))
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
// LESS
gulp.task('less', ['clean'], function() {
var stream = gulp.src(getPath('less'))
.pipe(less().on('error', customNotify.onError(function (error) {
return 'Error compiling LESS: ' + error.message;
})))
.pipe(gulp.dest(getPath('dist') + 'css'));
// this if statement is not nice yet and should be improved
// if we do bless in the return part it proccs and error
if (config.minifyCss === true) {
stream
.pipe(minifycss())
.pipe(bless({
imports: config.bless.useImport
}))
.pipe(gulp.dest(getPath('dist') + 'css'));
} else {
stream
.pipe(bless({
imports: config.bless.useImport
}))
.pipe(gulp.dest(getPath('dist') + 'css'));
}
return stream
.pipe(gulp.dest(getPath('dist') + 'css'))
.pipe(customNotify({
message: 'Successfully compiled LESS'
}));
});
// JavaScript
gulp.task('js', ['clean'], function() {
var stream = gulp.src(getPath('js'))
.pipe(concat('script.js'))
.pipe(gulp.dest(getPath('dist') + 'js'));
if (config.uglifyJS === true) {
stream.pipe(uglify());
}
return stream
.pipe(gulp.dest(getPath('dist') + 'js'))
.pipe(customNotify({
message: 'Successfully compiled JavaScript'
}));
});
// Bootstrap JavaScript
gulp.task('bootstrapJs', ['clean'], function() {
return gulp.src(config.bootstrap.js)
.pipe(concat('bootstrap.min.js'))
.pipe(uglify())
.pipe(gulp.dest(getPath('dist') + 'js/bootstrap'))
.pipe(customNotify({
message: 'Successfully compiled Bootstrap JS'
}));
});
// modernizr
var modernizrModules = ['bower_components/modernizr/modernizr.js'];
gulp.task('modernizr', ['clean'], function () {
gulp.src(modernizrModules.concat(config.modernizr.detectors))
.pipe(concat('modernizr.min.js'))
.pipe(uglify())
.pipe(gulp.dest(getPath('dist') + 'js/modernizr'))
.pipe(customNotify({
message: 'Successfully compiled Modernizr JS'
}));
});
// Bootstrap Fonts
gulp.task('bootstrapFonts', ['clean'], function(){
gulp.src('bower_components/bootstrap/fonts/*')
.pipe(gulp.dest(getPath('dist') + 'fonts/'));
gulp.src('bower_components/font-awesome/fonts/*')
.pipe(gulp.dest(getPath('dist') + 'fonts/'));
});
// Fileinclude
gulp.task('fileinclude', function() {
gulp.src(config.htmlSrc)
.pipe(fileinclude({
prefix: '@@',
basepath: '@file'
}))
.pipe(gulp.dest('./'));
});
// Task section:
// --------------------------------------
// Clean always has to clean the "dist/" folder
gulp.task('clean', function() {
var theme = (config.theme !== '') ? config.theme : oPath.defaultTheme;
var cleanPath = [oPath.basePath + theme + '/' + 'dist/'];
if (typeof util.env.mode !== undefined && util.env.mode === 'dev') {
cleanPath = [
oPath.basePath + theme + '/' + 'dist/js/*.js',
oPath.basePath + theme + '/' + 'dist/css/'
];
}
return gulp.src(
cleanPath,
{read: false}
)
.pipe(rimraf({force: true}));
});
// Watch, use "--mode dev" to run in dev mode
gulp.task('watch', function() {
gulp.watch(['less/**/*.less', getPath('js')], ['default']);
});
// Default task, use "--mode dev" to run in dev mode
gulp.task('default', function () {
if (typeof util.env.mode !== undefined && util.env.mode === 'dev') {
oPath.parts.dist = 'dist/';
}
// run default tasks
gulp.start('bootstrapFonts', 'bootstrapJs', 'less', 'lint', 'js', 'modernizr');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment