Matteo Scandolo | 50f3a73 | 2016-02-18 15:28:48 -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'); |
| 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'); |
| 25 | |
| 26 | var TEMPLATE_FOOTER = `}]); |
| 27 | angular.module('xos.mcordTopology').run(function($location){$location.path('/')}); |
| 28 | angular.bootstrap(angular.element('#xosMcordTopology'), ['xos.mcordTopology']);`; |
| 29 | |
| 30 | module.exports = function(options){ |
| 31 | |
| 32 | // delete previous builded file |
| 33 | gulp.task('clean', function(){ |
| 34 | return del( |
| 35 | [options.dashboards + 'xosMcordTopology.html'], |
| 36 | {force: true} |
| 37 | ); |
| 38 | }); |
| 39 | |
| 40 | // compile and minify scripts |
| 41 | gulp.task('scripts', function() { |
| 42 | return gulp.src([ |
| 43 | options.tmp + '**/*.js' |
| 44 | ]) |
| 45 | .pipe(ngAnnotate()) |
| 46 | .pipe(angularFilesort()) |
| 47 | .pipe(concat('xosMcordTopology.js')) |
| 48 | .pipe(uglify()) |
| 49 | .pipe(gulp.dest(options.static + 'js/')); |
| 50 | }); |
| 51 | |
| 52 | // set templates in cache |
| 53 | gulp.task('templates', function(){ |
| 54 | return gulp.src('./src/templates/*.html') |
| 55 | .pipe(templateCache({ |
| 56 | module: 'xos.mcordTopology', |
| 57 | root: 'templates/', |
| 58 | templateFooter: TEMPLATE_FOOTER |
| 59 | })) |
| 60 | .pipe(gulp.dest(options.tmp)); |
| 61 | }); |
| 62 | |
| 63 | // copy html index to Django Folder |
| 64 | gulp.task('copyHtml', ['clean'], function(){ |
| 65 | return gulp.src(options.src + 'index.html') |
| 66 | // remove dev dependencies from html |
| 67 | .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, '')) |
| 68 | .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, '')) |
| 69 | .pipe(replace(/ng-app=".*"\s/, '')) |
| 70 | // injecting minified files |
| 71 | .pipe( |
| 72 | inject( |
| 73 | gulp.src([ |
| 74 | options.static + 'js/vendor/xosMcordTopologyVendor.js', |
| 75 | options.static + 'js/xosMcordTopology.js' |
| 76 | ]), |
| 77 | {ignorePath: '/../../../xos/core/xoslib'} |
| 78 | ) |
| 79 | ) |
| 80 | .pipe(rename('xosMcordTopology.html')) |
| 81 | .pipe(gulp.dest(options.dashboards)); |
| 82 | }); |
| 83 | |
| 84 | // minify vendor js files |
| 85 | gulp.task('wiredep', function(){ |
| 86 | var bowerDeps = wiredep().js; |
| 87 | if(!bowerDeps){ |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | // remove angular (it's already loaded) |
| 92 | _.remove(bowerDeps, function(dep){ |
| 93 | return dep.indexOf('angular/angular.js') !== -1; |
| 94 | }); |
| 95 | |
| 96 | return gulp.src(bowerDeps) |
| 97 | .pipe(concat('xosMcordTopologyVendor.js')) |
| 98 | .pipe(uglify()) |
| 99 | .pipe(gulp.dest(options.static + 'js/vendor/')); |
| 100 | }); |
| 101 | |
| 102 | gulp.task('lint', function () { |
| 103 | return gulp.src(['src/js/**/*.js']) |
| 104 | .pipe(eslint()) |
| 105 | .pipe(eslint.format()) |
| 106 | .pipe(eslint.failAfterError()); |
| 107 | }); |
| 108 | |
| 109 | gulp.task('build', function() { |
| 110 | runSequence( |
| 111 | 'templates', |
| 112 | 'babel', |
| 113 | 'scripts', |
| 114 | 'wiredep', |
| 115 | 'copyHtml', |
| 116 | 'cleanTmp' |
| 117 | ); |
| 118 | }); |
| 119 | }; |