Skip to content

Instantly share code, notes, and snippets.

@digitalhitler
Created December 6, 2015 20:10
Show Gist options
  • Save digitalhitler/ce64dada1f7bfedd34d6 to your computer and use it in GitHub Desktop.
Save digitalhitler/ce64dada1f7bfedd34d6 to your computer and use it in GitHub Desktop.
GulpBoss: gulpfile.js with extendable plugins, less, css, js, browserify, minify-uglify-anythingyouwantfy
/**
* gulpfile.js
*
* Gulp BOSS 8-)
*
* Удобный (для меня) способ компилировать всё в Gulp-е.
* Всякие ништяки included.
*
* Made by http://github.com/digitalhitler
*
*
*
* ________________________
* _____¶¶______¶¶_¶¶____¶¶_¶¶¶¶¶¶¶¶_______¶¶¶
* _____¶¶______¶¶_¶¶__¶¶¶__¶¶____________¶¶¶¶
* _____¶¶______¶¶_¶¶_¶¶____¶¶____________¶¶_¶¶
* _____¶¶______¶¶_¶¶¶¶¶____¶¶¶¶¶¶¶¶_____¶¶___¶¶
* _____¶¶______¶¶_¶¶¶_¶¶___¶¶___________¶¶¶¶¶¶¶
* _____¶¶______¶¶_¶¶___¶¶__¶¶__________¶¶____¶¶¶
* _____¶¶¶¶¶¶¶_¶¶_¶¶____¶¶_¶¶¶¶¶¶¶¶¶__¶¶¶_____¶¶¶
* __________________________________________
* _______¶¶¶¶¶¶¶_____¶¶¶¶¶____¶¶¶¶¶____¶¶¶¶¶
* _______¶¶____¶¶__¶¶¶__¶¶¶__¶¶___¶¶¶_¶¶___¶¶¶
* _______¶¶____¶¶_¶¶¶_____¶¶_¶¶_______¶¶
* _______¶¶¶¶¶¶¶__¶¶______¶¶__¶¶¶¶¶____¶¶¶¶¶
* _______¶¶____¶¶_¶¶______¶¶¶_____¶¶_______¶¶
* _______¶¶_____¶¶_¶¶____¶¶¶_¶¶____¶¶_¶¶____¶¶
* _______¶¶¶¶¶¶¶¶___¶¶¶¶¶¶¶__¶¶¶¶¶¶¶__¶¶¶¶¶¶¶
*
*
*/
var gulp = require('gulp');
// error handler:
var plumber = require('gulp-plumber');
// file renamer
var rename = require('gulp-rename');
// file + file
var concat = require('gulp-concat');
// minificator for CSS
var minifyCSS = require('gulp-minify-css');
// minificator for JS
var uglify = require('gulp-uglify');
// less compiler
var less = require('gulp-less');
// browserify compiler:
var browserify = require('browserify');
// stream for browserify:
var source = require('vinyl-source-stream');
// gulp debugger
var debug = require('gulp-debug');
// eyecandy:
var colors = require('colors');
// filesystem
var fs = require('fs');
global.GulpBoss = {
config: {
compiledDestination: './public/build/',
extensionToQueue: {
js: 'scripts',
css: 'styles'
}
},
// == watching ==
browserify: {
entryPoint: './app/brow/index.js',
destinationFilename: 'app.js',
watchPattern: 'app/brow/**/*.js'
},
less: {
src: [
'app/less/**/*.less'
],
autoPrefixer: 'last 2 versions'
},
// == not watching (used by plugins and compiles once) ==
styles: {
src: [
],
concatFilename: 'env.css'
},
scripts: {
src: [
'app/raw/_autoload/*.js'
],
concatFilename: 'env.js'
},
addPlugin: function(name, files) {
var _this = this;
var results = {};
if (typeof name !== undefined) {
var fullPath = './app/raw/plugins/' + name;
var dirInfo;
try {
var dirInfo = fs.statSync(fullPath);
} catch (e) {
this.reportError('Plugin directory ' + fullPath + ' does not exists.');
}
console.log('Checking plugin ' + colors.bold(name) + '...');
if (typeof dirInfo === 'object') {
for (file in files) {
var currentFile = files[file];
var fileInfo = false;
var fileExt = false;
var fileQueue = false;
if (typeof currentFile === 'string') {
var fullFile = fullPath + '/' + currentFile;
try {
var fileInfo = fs.statSync(fullFile);
if (typeof fileInfo === 'object') {
var fileExt = fullFile.split('.').pop();
if (typeof fileExt === 'string' && fileExt.length > 0) {
if (typeof _this.config.extensionToQueue[fileExt] === 'string') {
fileQueue = _this.config.extensionToQueue[fileExt];
_this[fileQueue].src.push(fullFile);
if (!results[fileQueue]) {
results[fileQueue] = 1;
} else {
results[fileQueue] += 1;
}
}
} else {
_this.reportError('Unknown type of file ' + colors.bold(currentFile) + ' in ' + colors.bold(name) + ' plugin.');
}
}
} catch (e) {
_this.reportError('File ' + colors.bold(currentFile) + ' of plugin ' + colors.bold(name) + ' does not exists.');
}
} else {
_this.reportError(file + ' is not a string with filename, dude.');
}
}
if (results) {
var report = 'Queued: ';
for (result in results) {
report += colors.bold(results[result]) + ' of ' + result + '; ';
}
}
}
}
},
reportError(desc, terminate) {
console.log(colors.white('Gulp Toolbox reports you that'));
console.log(colors.red.bold('SHIT HAPPENS!'));
console.log(colors.yellow('* * *'));
console.log(desc);
if (!terminate || terminate === true) {
this.terminate(1);
}
},
terminate: function(exitCode) {
var exitCode = exitCode || 1;
console.log(colors.inverse('* Process terminated with code ' + exitCode));
process.exit(exitCode);
},
_runtime: {
includedPlugins: [],
}
};
GulpBoss.addPlugin(
'jquery-ui',
[
'jquery-ui.js',
'jquery-ui.css'
]
);
gulp.task('less', function() {
return gulp
.src(GulpBoss.less.src)
.pipe(plumber({
errorHandler: function(error) {
GulpBoss.reportError(error.message, false);
this.emit('end');
}
}))
.pipe(less())
.pipe(minifyCSS())
.pipe(gulp.dest(GulpBoss.config.compiledDestination));
});
gulp.task('scripts', function() {
// rawbundle means "no-browserify supported smthng"
return gulp
.src(GulpBoss.scripts.src)
.pipe(debug())
.pipe(plumber({
errorHandler: function(error) {
GulpBoss.reportError(error.message, false);
this.emit('end');
}
}))
.pipe(concat(GulpBoss.scripts.concatFilename))
.pipe(gulp.dest(GulpBoss.config.compiledDestination))
.pipe(rename({suffix: '.min'}))
.pipe(uglify())
.pipe(gulp.dest(GulpBoss.config.compiledDestination));
});
gulp.task('styles', function() {
return gulp
.src(GulpBoss.styles.src)
.pipe(debug())
.pipe(minifyCSS())
.pipe(concat(GulpBoss.styles.concatFilename))
.pipe(gulp.dest(GulpBoss.config.compiledDestination));
});
gulp.task('browserify', function() {
return browserify(GulpBoss.browserify.entryPoint)
.bundle()
.pipe(source(GulpBoss.browserify.destinationFilename))
.pipe(gulp.dest(GulpBoss.config.compiledDestination));
});
gulp.task('compile', ['styles', 'scripts']);
gulp.task('default', function() {
console.log(colors.yellow(colors.bold.underline('GulpBoss') + ' are here.'));
console.log(colors.grey('Made by DigitalHitler <[email protected]>\n'));
console.log(colors.bold('Hey-ho!\n'));
console.log('There is no default task, make the right decision:\n');
console.log(colors.white.bold(' less') + ' compile LESS styles into CSS\n');
console.log(colors.white.bold(' browserify') + ' compile browserify-powered application\n');
console.log(colors.white.bold(' styles') + ' compile non-dynamic style files\n');
console.log(colors.white.bold(' scripts') + ' compile non-dynamic script files\n');
console.log(colors.white.bold(' compile') + ' alias for running ' + colors.bold('styles') + ' and then ' + colors.bold('scripts') + ' tasks\n');
console.log(colors.white.bold(' watch') + ' starts watching for changes in ' + colors.bold('less') + ' and ' + colors.bold('browserify') + ' sources\n');
console.log('By the way, check out the ' + colors.underline.bold('GulpBoss') + ' object in gulpfile.js for configuration, plugins etc.\n');
});
gulp.task('watch', function() {
gulp.watch(GulpBoss.less.src, ['less']);
gulp.watch(GulpBoss.browserify.watchPattern, ['browserify']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment