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