github.com/nsqio/nsq@v1.3.0/nsqadmin/gulpfile.js (about) 1 var browserify = require('browserify'); 2 var clean = require('gulp-clean'); 3 var gulp = require('gulp'); 4 var notify = require('gulp-notify'); 5 var path = require('path'); 6 var sass = require('gulp-dart-sass'); 7 var source = require('vinyl-source-stream'); 8 var taskListing = require('gulp-task-listing'); 9 var uglify = require('gulp-uglify'); 10 var sourcemaps = require('gulp-sourcemaps'); 11 var buffer = require('vinyl-buffer'); 12 13 14 var ROOT = 'static'; 15 16 var VENDOR_CONFIG = { 17 'src': [ 18 'backbone', 19 'jquery', 20 'underscore', 21 'bootbox', 22 ], 23 'target': 'vendor.js', 24 'targetDir': './static/build/' 25 }; 26 27 function excludeVendor(b) { 28 VENDOR_CONFIG.src.forEach(function(vendorLib) { 29 b.exclude(vendorLib); 30 }); 31 } 32 33 function bytesToKB(bytes) { return Math.floor(+bytes/1024); } 34 35 function logBundle(filename, watching) { 36 return function (err, buf) { 37 if (err) { 38 console.error(err.toString()); 39 if (!watching) { 40 process.exit(1); 41 } 42 } 43 if (!watching) { 44 console.log(filename + ' ' + bytesToKB(buf.length) + ' KB written'); 45 } 46 } 47 } 48 49 50 function sassTask(root, inputFile) { 51 return function sassing() { 52 var onError = function(err) { 53 notify({'title': 'Sass Compile Error'}).write(err); 54 }; 55 return gulp.src(path.join(root, 'css', inputFile)) 56 .pipe(sass({ 57 'sourceComments': 'map', 58 }).on('error', onError)) 59 .pipe(gulp.dest(path.join(root, 'build/'))); 60 }; 61 } 62 63 64 function browserifyTask(root, inputFile) { 65 return function browserifying() { 66 var onError = function() { 67 var args = Array.prototype.slice.call(arguments); 68 notify.onError({ 69 'title': 'JS Compile Error', 70 'message': '<%= error.message %>' 71 }).apply(this, args); 72 // Keep gulp from hanging on this task 73 this.emit('end'); 74 }; 75 76 // Browserify needs a node module to import as its arg, so we need to 77 // force the leading "./" to be included. 78 var b = browserify({ 79 entries: './' + path.join(root, 'js', inputFile), 80 debug: true 81 }) 82 83 excludeVendor(b); 84 85 return b.bundle() 86 .pipe(source(inputFile)) 87 .pipe(buffer()) 88 .pipe(sourcemaps.init({'loadMaps': true, 'debug': true})) 89 // Add transformation tasks to the pipeline here. 90 .pipe(uglify()) 91 .on('error', onError) 92 .pipe(sourcemaps.write('./')) 93 .pipe(gulp.dest(path.join(root, 'build/'))); 94 }; 95 } 96 97 98 function watchTask(root) { 99 return function watching() { 100 gulp.watch(path.join(root, 'sass/**/*.scss'), gulp.series('sass')); 101 gulp.watch([ 102 path.join(root, 'js/**/*.js'), 103 path.join(root, 'js/**/*.hbs') 104 ], gulp.series('browserify')); 105 gulp.watch([ 106 path.join(root, 'html/**'), 107 path.join(root, 'fonts/**') 108 ], gulp.series('sync-static-assets')) 109 }; 110 } 111 112 113 function cleanTask() { 114 var paths = Array.prototype.slice.apply(arguments); 115 return function cleaning() { 116 return gulp.src(paths, {allowEmpty: true}).pipe(clean()); 117 }; 118 } 119 120 gulp.task('vendor-build-js', function() { 121 var onError = function() { 122 var args = Array.prototype.slice.call(arguments); 123 notify.onError({ 124 'title': 'JS Compile Error', 125 'message': '<%= error.message %>' 126 }).apply(this, args); 127 // Keep gulp from hanging on this task 128 this.emit('end'); 129 }; 130 131 var b = browserify() 132 .require(VENDOR_CONFIG.src); 133 134 return b.bundle(logBundle(VENDOR_CONFIG.target)) 135 .pipe(source(VENDOR_CONFIG.target)) 136 .pipe(buffer()) 137 // Add transformation tasks to the pipeline here. 138 .pipe(uglify()) 139 .on('error', onError) 140 .pipe(gulp.dest(VENDOR_CONFIG.targetDir)); 141 }); 142 143 gulp.task('help', taskListing); 144 145 gulp.task('sync-static-assets', function() { 146 return gulp.src([ 147 path.join(ROOT, 'html/**'), 148 path.join(ROOT, 'fonts/**'), 149 path.join(ROOT, 'img/**') 150 ]).pipe(gulp.dest(path.join(ROOT, 'build'))); 151 }); 152 153 gulp.task('sass', sassTask(ROOT, '*.*css')); 154 gulp.task('browserify', browserifyTask(ROOT, 'main.js')); 155 gulp.task('build', gulp.parallel('sass', 'browserify', 'sync-static-assets', 'vendor-build-js')); 156 gulp.task('watch', gulp.series('build', watchTask(ROOT))); 157 gulp.task('clean', gulp.series(cleanTask(path.join(ROOT, 'build')))); 158 159 gulp.task('default', gulp.series('help'));