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', |
| 39 | options.static + 'css/xosSynchronizerNotifier.css' |
| 40 | ], |
| 41 | {force: true} |
| 42 | ); |
| 43 | }); |
| 44 | |
| 45 | // minify css |
| 46 | gulp.task('css', function () { |
| 47 | var processors = [ |
| 48 | autoprefixer({browsers: ['last 1 version']}), |
| 49 | mqpacker, |
| 50 | csswring |
| 51 | ]; |
| 52 | |
| 53 | gulp.src([ |
| 54 | `${options.css}**/*.css`, |
| 55 | `!${options.css}dev.css` |
| 56 | ]) |
| 57 | .pipe(postcss(processors)) |
| 58 | .pipe(gulp.dest(options.tmp + '/css/')); |
| 59 | }); |
| 60 | |
| 61 | // copy css in correct folder |
| 62 | gulp.task('copyCss', ['wait'], function(){ |
| 63 | return gulp.src([`${options.tmp}/css/*.css`]) |
| 64 | .pipe(concat('xosSynchronizerNotifier.css')) |
| 65 | .pipe(gulp.dest(options.static + 'css/')) |
| 66 | }); |
| 67 | |
| 68 | // compile and minify scripts |
| 69 | gulp.task('scripts', function() { |
| 70 | return gulp.src([ |
| 71 | options.tmp + '**/*.js' |
| 72 | ]) |
| 73 | .pipe(ngAnnotate()) |
| 74 | .pipe(angularFilesort()) |
| 75 | .pipe(concat('xosSynchronizerNotifier.js')) |
| 76 | .pipe(concat.header('//Autogenerated, do not edit!!!\n')) |
| 77 | .pipe(concat.footer(TEMPLATE_FOOTER)) |
| 78 | .pipe(uglify()) |
| 79 | .pipe(gulp.dest(options.static + 'js/')); |
| 80 | }); |
| 81 | |
| 82 | // set templates in cache |
| 83 | gulp.task('templates', function(){ |
| 84 | return gulp.src('./src/templates/*.html') |
| 85 | .pipe(templateCache({ |
| 86 | module: 'xos.synchronizerNotifier', |
| 87 | root: 'templates/' |
| 88 | })) |
| 89 | .pipe(gulp.dest(options.tmp)); |
| 90 | }); |
| 91 | |
| 92 | // copy html index to Django Folder |
| 93 | gulp.task('copyHtml', function(){ |
| 94 | return gulp.src(options.src + 'index.html') |
| 95 | // remove dev dependencies from html |
| 96 | .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, '')) |
| 97 | .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, '')) |
| 98 | // injecting minified files |
| 99 | .pipe( |
| 100 | inject( |
| 101 | gulp.src([ |
| 102 | options.static + 'js/vendor/xosSynchronizerNotifierVendor.js', |
| 103 | options.static + 'js/xosSynchronizerNotifier.js', |
| 104 | options.static + 'css/xosSynchronizerNotifier.css' |
| 105 | ]), |
| 106 | {ignorePath: '/../../../xos/core/xoslib'} |
| 107 | ) |
| 108 | ) |
| 109 | .pipe(rename('xosSynchronizerNotifier.html')) |
| 110 | .pipe(gulp.dest(options.dashboards)); |
| 111 | }); |
| 112 | |
| 113 | // minify vendor js files |
| 114 | gulp.task('wiredep', function(){ |
| 115 | var bowerDeps = wiredep().js; |
| 116 | if(!bowerDeps){ |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // remove angular (it's already loaded) |
| 121 | _.remove(bowerDeps, function(dep){ |
| 122 | return dep.indexOf('angular/angular.js') !== -1; |
| 123 | }); |
| 124 | |
| 125 | return gulp.src(bowerDeps) |
| 126 | .pipe(concat('xosSynchronizerNotifierVendor.js')) |
| 127 | .pipe(uglify()) |
| 128 | .pipe(gulp.dest(options.static + 'js/vendor/')); |
| 129 | }); |
| 130 | |
| 131 | gulp.task('lint', function () { |
| 132 | return gulp.src(['src/js/**/*.js']) |
| 133 | .pipe(eslint()) |
| 134 | .pipe(eslint.format()) |
| 135 | .pipe(eslint.failAfterError()); |
| 136 | }); |
| 137 | |
| 138 | gulp.task('wait', function (cb) { |
| 139 | // setTimeout could be any async task |
| 140 | setTimeout(function () { |
| 141 | cb(); |
| 142 | }, 1000); |
| 143 | }); |
| 144 | |
| 145 | gulp.task('build', function() { |
| 146 | runSequence( |
| 147 | 'clean', |
| 148 | 'sass', |
| 149 | 'templates', |
| 150 | 'babel', |
| 151 | 'scripts', |
| 152 | 'wiredep', |
| 153 | 'css', |
| 154 | 'copyCss', |
| 155 | 'copyHtml', |
| 156 | 'cleanTmp' |
| 157 | ); |
| 158 | }); |
| 159 | }; |