Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | // BUILD |
| 4 | // |
| 5 | // The only purpose of this gulpfile is to build a XOS view and copy the correct files into |
| 6 | // .html => dashboards |
| 7 | // .js (minified and concat) => static/js |
| 8 | // |
| 9 | // The template are parsed and added to js with angular $templateCache |
| 10 | |
| 11 | var gulp = require('gulp'); |
| 12 | var ngAnnotate = require('gulp-ng-annotate'); |
| 13 | var uglify = require('gulp-uglify'); |
| 14 | var templateCache = require('gulp-angular-templatecache'); |
| 15 | var runSequence = require('run-sequence'); |
| 16 | var concat = require('gulp-concat-util'); |
| 17 | var del = require('del'); |
| 18 | var wiredep = require('wiredep'); |
| 19 | var angularFilesort = require('gulp-angular-filesort'); |
| 20 | var _ = require('lodash'); |
| 21 | var eslint = require('gulp-eslint'); |
| 22 | var inject = require('gulp-inject'); |
| 23 | var rename = require('gulp-rename'); |
| 24 | var replace = require('gulp-replace'); |
| 25 | var postcss = require('gulp-postcss'); |
| 26 | var autoprefixer = require('autoprefixer'); |
| 27 | var mqpacker = require('css-mqpacker'); |
| 28 | var csswring = require('csswring'); |
| 29 | |
Matteo Scandolo | 2919495 | 2016-06-17 11:57:05 -0700 | [diff] [blame] | 30 | const TEMPLATE_FOOTER = ``; |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 31 | |
| 32 | module.exports = function(options){ |
| 33 | |
| 34 | // delete previous builded file |
| 35 | gulp.task('clean', function(){ |
| 36 | return del( |
| 37 | [ |
| 38 | options.dashboards + 'xosSynchronizerNotifier.html', |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 39 | options.static + 'css/xosSynchronizerNotifier.css', |
| 40 | options.static + 'images/synchronizerNotifier-icon.png', |
| 41 | options.static + 'images/synchronizerNotifier-icon-active.png' |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 42 | ], |
| 43 | {force: true} |
| 44 | ); |
| 45 | }); |
| 46 | |
| 47 | // minify css |
| 48 | gulp.task('css', function () { |
| 49 | var processors = [ |
| 50 | autoprefixer({browsers: ['last 1 version']}), |
| 51 | mqpacker, |
| 52 | csswring |
| 53 | ]; |
| 54 | |
| 55 | gulp.src([ |
| 56 | `${options.css}**/*.css`, |
| 57 | `!${options.css}dev.css` |
| 58 | ]) |
| 59 | .pipe(postcss(processors)) |
| 60 | .pipe(gulp.dest(options.tmp + '/css/')); |
| 61 | }); |
| 62 | |
| 63 | // copy css in correct folder |
| 64 | gulp.task('copyCss', ['wait'], function(){ |
| 65 | return gulp.src([`${options.tmp}/css/*.css`]) |
| 66 | .pipe(concat('xosSynchronizerNotifier.css')) |
| 67 | .pipe(gulp.dest(options.static + 'css/')) |
| 68 | }); |
| 69 | |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 70 | // copy images in correct folder |
| 71 | gulp.task('copyImages', ['wait'], function(){ |
| 72 | return gulp.src([`${options.icon}/synchronizerNotifier-icon.png`,`${options.icon}/synchronizerNotifier-icon-active.png`]) |
| 73 | .pipe(gulp.dest(options.static + 'images/')) |
| 74 | }); |
| 75 | |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 76 | // compile and minify scripts |
| 77 | gulp.task('scripts', function() { |
| 78 | return gulp.src([ |
| 79 | options.tmp + '**/*.js' |
| 80 | ]) |
| 81 | .pipe(ngAnnotate()) |
| 82 | .pipe(angularFilesort()) |
| 83 | .pipe(concat('xosSynchronizerNotifier.js')) |
| 84 | .pipe(concat.header('//Autogenerated, do not edit!!!\n')) |
| 85 | .pipe(concat.footer(TEMPLATE_FOOTER)) |
| 86 | .pipe(uglify()) |
| 87 | .pipe(gulp.dest(options.static + 'js/')); |
| 88 | }); |
| 89 | |
| 90 | // set templates in cache |
| 91 | gulp.task('templates', function(){ |
| 92 | return gulp.src('./src/templates/*.html') |
| 93 | .pipe(templateCache({ |
| 94 | module: 'xos.synchronizerNotifier', |
| 95 | root: 'templates/' |
| 96 | })) |
| 97 | .pipe(gulp.dest(options.tmp)); |
| 98 | }); |
| 99 | |
| 100 | // copy html index to Django Folder |
| 101 | gulp.task('copyHtml', function(){ |
| 102 | return gulp.src(options.src + 'index.html') |
| 103 | // remove dev dependencies from html |
| 104 | .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, '')) |
| 105 | .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, '')) |
| 106 | // injecting minified files |
| 107 | .pipe( |
| 108 | inject( |
| 109 | gulp.src([ |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 110 | options.static + 'vendor/xosSynchronizerNotifierVendor.js', |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 111 | options.static + 'js/xosSynchronizerNotifier.js', |
| 112 | options.static + 'css/xosSynchronizerNotifier.css' |
| 113 | ]), |
| 114 | {ignorePath: '/../../../xos/core/xoslib'} |
| 115 | ) |
| 116 | ) |
| 117 | .pipe(rename('xosSynchronizerNotifier.html')) |
| 118 | .pipe(gulp.dest(options.dashboards)); |
| 119 | }); |
| 120 | |
| 121 | // minify vendor js files |
| 122 | gulp.task('wiredep', function(){ |
| 123 | var bowerDeps = wiredep().js; |
| 124 | if(!bowerDeps){ |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | // remove angular (it's already loaded) |
| 129 | _.remove(bowerDeps, function(dep){ |
| 130 | return dep.indexOf('angular/angular.js') !== -1; |
| 131 | }); |
| 132 | |
| 133 | return gulp.src(bowerDeps) |
| 134 | .pipe(concat('xosSynchronizerNotifierVendor.js')) |
| 135 | .pipe(uglify()) |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 136 | .pipe(gulp.dest(options.static + 'vendor/')); |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 137 | }); |
| 138 | |
| 139 | gulp.task('lint', function () { |
| 140 | return gulp.src(['src/js/**/*.js']) |
| 141 | .pipe(eslint()) |
| 142 | .pipe(eslint.format()) |
| 143 | .pipe(eslint.failAfterError()); |
| 144 | }); |
| 145 | |
| 146 | gulp.task('wait', function (cb) { |
| 147 | // setTimeout could be any async task |
| 148 | setTimeout(function () { |
| 149 | cb(); |
| 150 | }, 1000); |
| 151 | }); |
| 152 | |
| 153 | gulp.task('build', function() { |
| 154 | runSequence( |
| 155 | 'clean', |
| 156 | 'sass', |
| 157 | 'templates', |
| 158 | 'babel', |
| 159 | 'scripts', |
| 160 | 'wiredep', |
| 161 | 'css', |
| 162 | 'copyCss', |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 163 | 'copyImages', |
Matteo Scandolo | 42e3fe2 | 2016-05-27 14:52:37 -0700 | [diff] [blame] | 164 | 'copyHtml', |
| 165 | 'cleanTmp' |
| 166 | ); |
| 167 | }); |
| 168 | }; |