Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [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'); |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 16 | var concat = require('gulp-concat-util'); |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 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'); |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 25 | var postcss = require('gulp-postcss'); |
| 26 | var autoprefixer = require('autoprefixer'); |
| 27 | var mqpacker = require('css-mqpacker'); |
| 28 | var csswring = require('csswring'); |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 29 | |
| 30 | module.exports = function(options){ |
| 31 | |
| 32 | // delete previous builded file |
| 33 | gulp.task('clean', function(){ |
| 34 | return del( |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 35 | [options.dashboards + 'xosDiagnostic.html'], |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 36 | {force: true} |
| 37 | ); |
| 38 | }); |
| 39 | |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 40 | // minify css |
| 41 | gulp.task('css', function () { |
| 42 | var processors = [ |
| 43 | autoprefixer({browsers: ['last 1 version']}), |
| 44 | mqpacker, |
| 45 | csswring |
| 46 | ]; |
Matteo Scandolo | 9d3face | 2016-03-29 14:10:16 -0700 | [diff] [blame] | 47 | |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 48 | gulp.src([ |
| 49 | `${options.css}**/*.css`, |
| 50 | `!${options.css}dev.css` |
| 51 | ]) |
| 52 | .pipe(postcss(processors)) |
| 53 | .pipe(gulp.dest(options.tmp + '/css/')); |
| 54 | }); |
| 55 | |
| 56 | gulp.task('copyCss', ['css'], function(){ |
| 57 | return gulp.src([`${options.tmp}/css/*.css`]) |
| 58 | .pipe(concat('xosDiagnostic.css')) |
| 59 | .pipe(gulp.dest(options.static + 'css/')) |
| 60 | }); |
| 61 | |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 62 | // compile and minify scripts |
| 63 | gulp.task('scripts', function() { |
| 64 | return gulp.src([ |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 65 | options.tmp + '**/*.js', |
| 66 | options.tmp + 'templates.js' |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 67 | ]) |
| 68 | .pipe(ngAnnotate()) |
| 69 | .pipe(angularFilesort()) |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 70 | .pipe(concat('xosDiagnostic.js')) |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 71 | .pipe(concat.header('//Autogenerated, do not edit!!!\n')) |
Matteo Scandolo | 1df2173 | 2016-03-29 14:06:13 -0700 | [diff] [blame] | 72 | // .pipe(concat.footer(TEMPLATE_FOOTER)) |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 73 | // .pipe(uglify()) |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 74 | .pipe(gulp.dest(options.static + 'js/')); |
| 75 | }); |
| 76 | |
| 77 | // set templates in cache |
| 78 | gulp.task('templates', function(){ |
| 79 | return gulp.src('./src/templates/*.html') |
| 80 | .pipe(templateCache({ |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 81 | module: 'xos.diagnostic', |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 82 | root: 'templates/', |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 83 | // templateFooter: TEMPLATE_FOOTER |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 84 | })) |
| 85 | .pipe(gulp.dest(options.tmp)); |
| 86 | }); |
| 87 | |
| 88 | // copy html index to Django Folder |
| 89 | gulp.task('copyHtml', ['clean'], function(){ |
| 90 | return gulp.src(options.src + 'index.html') |
| 91 | // remove dev dependencies from html |
| 92 | .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, '')) |
| 93 | .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, '')) |
Matteo Scandolo | 1df2173 | 2016-03-29 14:06:13 -0700 | [diff] [blame] | 94 | // .pipe(replace(/ng-app=".*"\s/, '')) |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 95 | // injecting minified files |
| 96 | .pipe( |
| 97 | inject( |
| 98 | gulp.src([ |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 99 | options.static + 'js/vendor/xosDiagnosticVendor.js', |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 100 | options.static + 'js/xosDiagnostic.js', |
| 101 | options.static + 'css/xosDiagnostic.css' |
| 102 | ]), |
| 103 | {ignorePath: '/../../../xos/core/xoslib'} |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 104 | ) |
| 105 | ) |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 106 | .pipe(rename('xosDiagnostic.html')) |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 107 | .pipe(gulp.dest(options.dashboards)); |
| 108 | }); |
| 109 | |
| 110 | // minify vendor js files |
| 111 | gulp.task('wiredep', function(){ |
| 112 | var bowerDeps = wiredep().js; |
| 113 | if(!bowerDeps){ |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | // remove angular (it's already loaded) |
| 118 | _.remove(bowerDeps, function(dep){ |
| 119 | return dep.indexOf('angular/angular.js') !== -1; |
| 120 | }); |
| 121 | |
| 122 | return gulp.src(bowerDeps) |
Matteo Scandolo | 0456495 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 123 | .pipe(concat('xosDiagnosticVendor.js')) |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 124 | .pipe(uglify()) |
| 125 | .pipe(gulp.dest(options.static + 'js/vendor/')); |
| 126 | }); |
| 127 | |
| 128 | gulp.task('lint', function () { |
| 129 | return gulp.src(['src/js/**/*.js']) |
| 130 | .pipe(eslint()) |
| 131 | .pipe(eslint.format()) |
| 132 | .pipe(eslint.failAfterError()); |
| 133 | }); |
| 134 | |
Matteo Scandolo | 9d3face | 2016-03-29 14:10:16 -0700 | [diff] [blame] | 135 | // inject CSS |
| 136 | gulp.task('injectCss', function(){ |
| 137 | return gulp.src(options.src + 'index.html') |
| 138 | .pipe( |
| 139 | inject( |
| 140 | gulp.src(options.src + 'css/*.css'), |
| 141 | { |
| 142 | ignorePath: [options.src] |
| 143 | } |
| 144 | ) |
| 145 | ) |
| 146 | .pipe(gulp.dest(options.src)); |
| 147 | }); |
| 148 | |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 149 | gulp.task('build', function() { |
| 150 | runSequence( |
Matteo Scandolo | 9d3face | 2016-03-29 14:10:16 -0700 | [diff] [blame] | 151 | 'lint', |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 152 | 'templates', |
| 153 | 'babel', |
| 154 | 'scripts', |
| 155 | 'wiredep', |
Matteo Scandolo | 9d3face | 2016-03-29 14:10:16 -0700 | [diff] [blame] | 156 | 'injectCss', |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 157 | 'copyHtml', |
Matteo Scandolo | 70ac216 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 158 | 'copyCss' |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 159 | ); |
| 160 | }); |
| 161 | }; |