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