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