Last active
January 4, 2017 09:17
-
-
Save robshep/6f9307b5eb110cf4e039 to your computer and use it in GitHub Desktop.
gulpfile.js
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
/** | |
* | |
*/ | |
var version = '1.0.0'; | |
var bsThemeVariables = "bootstrap-variables-sarbase2"; | |
var PRODUCTION = (process.env.FE_PROD) || false; | |
console.log("PRODUCTION = " + PRODUCTION); | |
var s = { | |
dest: "src/main/resources/static/gen", | |
stage: "target/frontend", | |
nghtml: { | |
src: ["src/frontend/ng/**/*.html"], | |
dst: "/partials" | |
}, | |
misc: { | |
src: [ 'src/apple-touch-icon-precomposed.png', 'src/robots.txt', 'src/favicon.ico' ], | |
dst: "/" | |
}, | |
js: { | |
src: ['src/frontend/js/*.js', 'src/frontend/ng/**/*.js'], | |
dst: "/js/" | |
}, | |
css: { | |
src: ['src/frontend/css/*'], | |
dst: "/css/" | |
}, | |
less: { | |
src: ['src/frontend/less/*'], | |
dst: "/less-css/" | |
}, | |
fonts: { | |
src: ['bower_components/font-awesome/fonts/*'], | |
dst: "/fonts/" | |
}, | |
ng: { | |
src: 'src/ng/**/*', | |
dst: '//' | |
}, | |
bootstrap: { | |
dst: '/bs/' | |
}, | |
layouts:{ | |
src: ['src/frontend/layouts/*.html'], | |
dst: 'src/main/resources/templates/layouts/' | |
} | |
}; | |
/* prepend the common dist dir */ | |
for(var p in s){ | |
if(!!s[p].dst && s[p].dst.indexOf("/") == 0){ // only if the dst starts with "/" otherwise it is already configured | |
s[p].stage = s.stage + s[p].dst; | |
s[p].dst = s.dest + s[p].dst; | |
} | |
} | |
var css_sources = [s.less.stage + "/*"].concat(s.css.src); | |
var libs = { | |
css : [ | |
s.bootstrap.stage + "bootstrap.css", // generated | |
//pnotify | |
"bower_components/pnotify/pnotify.core.css", | |
"bower_components/pnotify/pnotify.buttons.css", | |
"bower_components/pnotify/pnotify.history.css", | |
//slidebars | |
"bower_components/Slidebars/dist/slidebars.css", | |
//font awesome | |
"bower_components/font-awesome/css/font-awesome.css", | |
// ui select | |
"bower_components/select2/select2.css", | |
"bower_components/select2-bootstrap3-css/select2-bootstrap.css", | |
"bower_components/angular-ui-select/dist/select.css", | |
// x-editable | |
"bower_components/angular-xeditable/dist/css/xeditable.css", | |
// tag def color picker | |
"bower_components/really-simple-colorpicker/css/colorPicker.css", | |
// angula grid | |
"bower_components/ag-grid/dist/angularGrid.css", | |
//"bower_components/ag-grid/dist/theme-fresh.css", | |
], | |
assets : [ | |
"bower_components/select2/select2.png", | |
"bower_components/select2/select2x2.png" | |
], | |
js : [ | |
"bower_components/jquery/dist/jquery.js", | |
"bower_components/bootstrap/dist/js/bootstrap.js", | |
// angular core libs | |
"bower_components/angularjs/angular.js", | |
"bower_components/angular-route/angular-route.js", | |
"bower_components/angular-sanitize/angular-sanitize.js", | |
//lodash | |
"bower_components/lodash/lodash.js", | |
// pnotify | |
"bower_components/pnotify/pnotify.core.js", | |
"bower_components/pnotify/pnotify.buttons.js", | |
"bower_components/pnotify/pnotify.callbacks.js", | |
"bower_components/pnotify/pnotify.confirm.js", | |
"bower_components/pnotify/pnotify.desktop.js", | |
"bower_components/pnotify/pnotify.history.js", | |
"bower_components/pnotify/pnotify.nonblock.js", | |
//slidebars | |
"bower_components/Slidebars/dist/slidebars.js", | |
// ui select2 | |
"bower_components/angular-ui-select/dist/select.js", | |
// x-editable | |
"bower_components/angular-xeditable/dist/js/xeditable.js", | |
// bootbox modals and dialogs | |
// unused, preferring angular-modal-service | |
// "bower_components/bootbox/bootbox.js", | |
"bower_components/angular-modal-service/dst/angular-modal-service.js", | |
// tag def colour picker | |
"bower_components/really-simple-colorpicker/js/jquery.colorPicker.js", | |
// this one must be last or it borks the concat :( | |
// angular grid: http://www.angulargrid.com/ | |
"bower_components/ag-grid/dist/angularGrid.js", | |
] | |
}; | |
// for expect-file plugin | |
var expectation = { errorOnFailure: true, checkRealFile: true } | |
// Include Gulp | |
var fs = require('fs'); | |
var gulp = require('gulp'); | |
var del = require('del'); | |
var merge = require('merge-stream'); | |
var browserSync = require('browser-sync'); | |
var lock = require("gulp-lock"); | |
var iff = require("gulp-if"); | |
// Include p | |
var p = require("gulp-load-plugins")({ | |
pattern: ['gulp-*', 'gulp.*', 'main-bower-files'], | |
replaceString: /\bgulp[\-.]/, | |
lazy:false | |
}); | |
console.log(p); | |
console.log(s); | |
var sourcemaps = p.sourcemaps; | |
var path = require('path'); | |
// Define default destination folder | |
var dest = 'dist/'; | |
///////////////////////////////////////////////// | |
// Tasks Below | |
///////////////////////////////////////////////// | |
/** | |
* Angular partials | |
*/ | |
gulp.task("nghtml", function(){ | |
return gulp.src(s.nghtml.src) | |
.pipe(p.htmlmin({collapseWhitespace: true})) | |
.pipe(gulp.dest(s.nghtml.dst)); | |
}); | |
/** | |
* font assets | |
*/ | |
gulp.task('fonts', ['fonts:clean'], function(){ | |
return gulp.src(s.fonts.src) | |
.pipe(gulp.dest(s.fonts.dst)); | |
}); | |
gulp.task('fonts:clean', [], function(cb){ | |
return del(s.fonts.dst, {force:true}, cb); | |
}); | |
/** | |
* | |
* Bootstrap customizer | |
* | |
* | |
*/ | |
gulp.task('custom-bootstrap:setup', ['custom-bootstrap:clean'], function(){ | |
return gulp.src("src/frontend/" + bsThemeVariables + ".less") | |
.pipe(p.rename("variables.less")) | |
.pipe(gulp.dest("bower_components/bootstrap/less/")); | |
}); | |
gulp.task('custom-bootstrap:clean', [], function(cb) { | |
return del(s.bootstrap.stage + "/*", {force:true}, cb); | |
}); | |
gulp.task('custom-bootstrap', ['custom-bootstrap:setup'], function(){ | |
return gulp.src('bower_components/bootstrap/less/bootstrap.less') | |
.pipe(p.less()) | |
.pipe(p.rename("bootstrap.css")) | |
.pipe(gulp.dest(s.bootstrap.stage)); | |
}); | |
/* | |
* | |
* LAYOUT template setup | |
* | |
*/ | |
gulp.task('layouts', ['layouts:setup'], function(){}); | |
gulp.task('layouts:setup', ['layouts:clean'], function(){ | |
return gulp.src(s.layouts.src) | |
.pipe(gulp.dest(s.layouts.dst)); | |
}); | |
gulp.task('layouts:clean', [], function(cb){ | |
return del(s.layouts.dst + "/*.html", {force:true}, cb); | |
}); | |
/** | |
* we need to use a lock for processing the layout file injector | |
* the injectorLock is used at task definition to ensure that | |
* only one injector is operating on the layout files at once. | |
* | |
*/ | |
var injectorLock = lock(); | |
var injectIntoLayouts = function(fileset, injectTag){ | |
return gulp.src(s.layouts.dst + "/*") | |
.pipe(p.filter('*.html')) | |
.pipe(p.inject(fileset, {name:injectTag, ignorePath:'/src/main/resources/static/', addRootSlash:true})) | |
.pipe(gulp.dest(s.layouts.dst)); | |
}; | |
var checkAllFilesExist = function(sources) | |
{ | |
if( ! Array.isArray(sources)){ | |
throw "Not an Array" + sources | |
} | |
for(var f=0;f<sources.length;f++){ | |
if(!fs.existsSync(sources[f])){ | |
throw "File not found: " + sources[f] | |
} | |
} | |
} | |
/** | |
* main flexible raw JS or CSS processor. | |
*/ | |
var procSources = function(type, sources, outfile, dest, injectTag){ | |
var src = gulp.src(sources) | |
if( injectTag == 'lib' ){ // if we're processing libs then check each file specified does actually exist | |
checkAllFilesExist(sources); | |
} | |
var out = p.concat(outfile); | |
var dst = gulp.dest(dest); | |
var minFn = null; | |
if(type === 'css'){ | |
minFn = p.cssmin; | |
} | |
else if(type === 'js'){ | |
minFn = p.uglify; | |
} | |
else if(typeof(type) != 'undefined'){ | |
throw "Type is not 'css' or 'js'"; | |
} | |
var output = null; | |
if(PRODUCTION) | |
{ | |
output = src.pipe(sourcemaps.init()) | |
.pipe(out) | |
.pipe( p.rev() ) | |
.pipe( iff( (!!minFn), minFn() ) ) | |
.pipe(sourcemaps.write("maps/")) | |
.pipe(dst); | |
} | |
else | |
{ | |
output = src.pipe(out) | |
.pipe(dst); | |
} | |
if( typeof injectTag === 'undefined'){ | |
return output; | |
} | |
else { | |
return injectIntoLayouts(output, injectTag); | |
} | |
}; | |
/* | |
* LESS | |
* | |
* | |
*/ | |
gulp.task('less', ['less:clean'], function() { | |
return gulp.src(s.less.src) | |
.pipe(p.filter('*.less')) | |
.pipe(p.less()) | |
.pipe(gulp.dest(s.less.stage)); | |
}); | |
gulp.task('less:clean', [], function(cb) { | |
return del(s.less.stage + "/*", {force:true}, cb); | |
}); | |
/* | |
* _________ _________ _________ | |
* \_ ___ \ / _____// _____/ | |
* / \ \/ \_____ \ \_____ \ | |
* \ \____/ \/ \ | |
* \______ /_______ /_______ / | |
* \/ \/ \/ | |
* | |
* CSS tasks | |
* | |
*/ | |
gulp.task('css', ['css:libs', 'css:app'], function() {}); | |
gulp.task('css:libs', ['css:clean','css:libs:assets','custom-bootstrap','layouts'], injectorLock.stream(function(){ | |
return procSources('css', libs.css,'libs.css',s.css.dst, 'lib'); | |
})); | |
gulp.task('css:app', ['css:clean','layouts', 'less'], injectorLock.stream(function(){ | |
return procSources('css', css_sources, 'app.css', s.css.dst, 'app' ); | |
})); | |
gulp.task('css:clean', [], function(cb){ | |
return del(s.css.dst + "/*", {force:true}, cb); | |
}); | |
gulp.task('css:libs:assets', ['css:clean', 'misc'], function(){ | |
checkAllFilesExist(libs.assets) | |
return gulp.src(libs.assets) | |
.pipe(gulp.dest(s.css.dst)); | |
}); | |
gulp.task("dev:update:css", injectorLock.stream(function(){ | |
return procSources('css', css_sources, 'app.css', s.css.dst ); | |
})); | |
/* | |
* __ .__ __ | |
* |__|____ ___ _______ ______ ___________|__|______/ |_ | |
* | \__ \\ \/ /\__ \ / ___// ___\_ __ \ \____ \ __\ | |
* | |/ __ \\ / / __ \_\___ \\ \___| | \/ | |_> > | | |
* /\__| (____ /\_/ (____ /____ >\___ >__| |__| __/|__| | |
* \______| \/ \/ \/ \/ |__| | |
* | |
* JS tasks | |
* | |
*/ | |
gulp.task('js', ['js:libs','js:app'], function(){}); | |
gulp.task('js:clean', [], function(cb){ | |
return del(s.js.dst + "/*", {force:true}, cb); | |
}); | |
gulp.task('js:libs', ['js:clean','layouts'], injectorLock.stream(function(){ | |
return procSources('js', libs.js,'libs.js',s.js.dst, 'lib' ); | |
})); | |
gulp.task('js:app', ['js:clean','layouts'], injectorLock.stream(function(){ | |
return procSources('js', s.js.src,'app.js',s.js.dst, 'app' ); | |
})); | |
gulp.task("dev:update:js", injectorLock.stream(function(){ | |
return procSources('js', s.js.src,'app.js',s.js.dst ); | |
})); | |
/* | |
* misc | |
* | |
* micellaneous tasks that can't quite fit in othe nice places. | |
* such as the odd bower asset | |
*/ | |
gulp.task("misc", function(){ | |
return gulp.src("bower_components/really-simple-colorpicker/arrow.gif") | |
.pipe(gulp.dest(s.dest)); | |
}) | |
/* | |
* | |
* | |
* DEV tasks | |
* | |
* | |
*/ | |
// kick off once for every call to "gulp" | |
gulp.task('init', function(cb){ | |
//gulp.src(['./bower.json', './package.json']) | |
// .pipe(p.install()); | |
cb(); | |
}) | |
// the main entrypoint to our productive building | |
gulp.task('main', ['css', 'js', 'fonts','nghtml','misc' ]); | |
// development helpers | |
gulp.task('watch', function(){ | |
gulp.watch(s.js.src, ['dev:update:js']); | |
gulp.watch(s.css.src, ['dev:update:css']); | |
gulp.watch(s.less.src, ['dev:update:css']); | |
gulp.watch(s.layouts.src, ['css','js','fonts']); | |
gulp.watch(s.nghtml.src, ['nghtml']); | |
}) | |
// called by automated build scripts | |
gulp.task('default', function(callback) { | |
var runSequence = require('run-sequence') | |
runSequence('init', | |
'main', | |
callback); | |
}); | |
// gulp normal | |
gulp.task('dev', function(callback) { | |
var runSequence = require('run-sequence') | |
runSequence('default', | |
'watch', | |
callback); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment