Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -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'); |
| 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'); |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 25 | var postcss = require('gulp-postcss'); |
| 26 | var autoprefixer = require('autoprefixer'); |
| 27 | var mqpacker = require('css-mqpacker'); |
| 28 | var csswring = require('csswring'); |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 29 | |
| 30 | var TEMPLATE_FOOTER = `}]); |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 31 | angular.module('xos.openVPNDashboard').run(function($location){$location.path('/')}); |
| 32 | angular.bootstrap(angular.element('#xosOpenVPNDashboard'), ['xos.openVPNDashboard']);`; |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 33 | |
| 34 | module.exports = function(options){ |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 35 | |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 36 | // delete previous builded file |
| 37 | gulp.task('clean', function(){ |
| 38 | return del( |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 39 | [options.dashboards + 'xosOpenVPNDashboard.html'], |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 40 | {force: true} |
| 41 | ); |
| 42 | }); |
| 43 | |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 44 | // minify css |
| 45 | gulp.task('css', function () { |
| 46 | var processors = [ |
| 47 | autoprefixer({browsers: ['last 1 version']}), |
| 48 | mqpacker, |
| 49 | csswring |
| 50 | ]; |
| 51 | |
| 52 | gulp.src([ |
| 53 | `${options.css}**/*.css`, |
| 54 | `!${options.css}dev.css` |
| 55 | ]) |
| 56 | .pipe(postcss(processors)) |
| 57 | .pipe(gulp.dest(options.tmp + '/css/')); |
| 58 | }); |
| 59 | |
| 60 | gulp.task('copyCss', ['css'], function(){ |
| 61 | return gulp.src([`${options.tmp}/css/*.css`]) |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 62 | .pipe(concat('xosOpenVPNDashboard.css')) |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 63 | .pipe(gulp.dest(options.static + 'css/')) |
| 64 | }); |
| 65 | |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 66 | // compile and minify scripts |
| 67 | gulp.task('scripts', function() { |
| 68 | return gulp.src([ |
| 69 | options.tmp + '**/*.js' |
| 70 | ]) |
| 71 | .pipe(ngAnnotate()) |
| 72 | .pipe(angularFilesort()) |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 73 | .pipe(concat('xosOpenVPNDashboard.js')) |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 74 | .pipe(uglify()) |
| 75 | .pipe(gulp.dest(options.static + 'js/')); |
| 76 | }); |
| 77 | |
| 78 | // set templates in cache |
| 79 | gulp.task('templates', function(){ |
| 80 | return gulp.src('./src/templates/*.html') |
| 81 | .pipe(templateCache({ |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 82 | module: 'xos.openVPNDashboard', |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 83 | root: 'templates/', |
| 84 | templateFooter: TEMPLATE_FOOTER |
| 85 | })) |
| 86 | .pipe(gulp.dest(options.tmp)); |
| 87 | }); |
| 88 | |
| 89 | // copy html index to Django Folder |
| 90 | gulp.task('copyHtml', ['clean'], function(){ |
| 91 | return gulp.src(options.src + 'index.html') |
| 92 | // remove dev dependencies from html |
| 93 | .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, '')) |
| 94 | .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, '')) |
| 95 | .pipe(replace(/ng-app=".*"\s/, '')) |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 96 | // rewriting css path |
| 97 | // .pipe(replace(/(<link.*">)/, '')) |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 98 | // injecting minified files |
| 99 | .pipe( |
| 100 | inject( |
| 101 | gulp.src([ |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 102 | options.static + 'js/vendor/xosOpenVPNDashboardVendor.js', |
| 103 | options.static + 'js/xosOpenVPNDashboard.js', |
| 104 | options.static + 'css/xosOpenVPNDashboard.css' |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 105 | ]), |
| 106 | {ignorePath: '/../../../xos/core/xoslib'} |
| 107 | ) |
| 108 | ) |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 109 | .pipe(rename('xosOpenVPNDashboard.html')) |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 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) |
Jeremy Mowery | 1b09b33 | 2016-04-19 17:01:48 -0700 | [diff] [blame] | 126 | .pipe(concat('xosOpenVPNDashboardVendor.js')) |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 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('build', function() { |
| 139 | runSequence( |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 140 | 'lint', |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 141 | 'templates', |
| 142 | 'babel', |
| 143 | 'scripts', |
| 144 | 'wiredep', |
| 145 | 'copyHtml', |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 146 | 'copyCss', |
Jeremy Mowery | 159f97b | 2016-02-25 09:32:00 -0700 | [diff] [blame] | 147 | 'cleanTmp' |
| 148 | ); |
| 149 | }); |
Jeremy Mowery | 5ab3415 | 2016-04-17 19:20:01 -0700 | [diff] [blame] | 150 | }; |