Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | // BUILD |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 4 | // |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 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 |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 8 | // |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 9 | // The template are parsed and added to js with angular $templateCache |
| 10 | |
| 11 | var gulp = require('gulp'); |
Matteo Scandolo | d838fac | 2015-11-04 16:10:09 +0100 | [diff] [blame] | 12 | var ngAnnotate = require('gulp-ng-annotate'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 13 | var uglify = require('gulp-uglify'); |
| 14 | var templateCache = require('gulp-angular-templatecache'); |
| 15 | var runSequence = require('run-sequence'); |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 16 | var concat = require('gulp-concat'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 17 | var del = require('del'); |
| 18 | var wiredep = require('wiredep'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 19 | var angularFilesort = require('gulp-angular-filesort'); |
Matteo Scandolo | cb7ea1e | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 20 | var _ = require('lodash'); |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 21 | var eslint = require('gulp-eslint'); |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 22 | var inject = require('gulp-inject'); |
| 23 | var rename = require('gulp-rename'); |
Matteo Scandolo | 61a714d | 2015-11-06 11:16:12 +0100 | [diff] [blame] | 24 | var replace = require('gulp-replace'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 25 | |
Matteo Scandolo | f1072ed | 2015-11-09 11:25:40 +0100 | [diff] [blame] | 26 | var TEMPLATE_FOOTER = `}]); |
| 27 | angular.module('xos.<%= name %>').run(function($location){$location.path('/')}); |
| 28 | angular.bootstrap(angular.element('#xos<%= fileName %>'), ['xos.<%= name %>']);`; |
| 29 | |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 30 | module.exports = function(options){ |
| 31 | |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 32 | // delete previous builded file |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 33 | gulp.task('clean', function(){ |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 34 | return del( |
| 35 | [options.dashboards + 'xos<%= fileName %>.html'], |
| 36 | {force: true} |
| 37 | ); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 38 | }); |
| 39 | |
| 40 | // compile and minify scripts |
| 41 | gulp.task('scripts', function() { |
| 42 | return gulp.src([ |
Matteo Scandolo | 8a1f236 | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 43 | options.tmp + '**/*.js' |
| 44 | ]) |
| 45 | .pipe(ngAnnotate()) |
| 46 | .pipe(angularFilesort()) |
| 47 | .pipe(concat('xos<%= fileName %>.js')) |
| 48 | .pipe(uglify()) |
| 49 | .pipe(gulp.dest(options.static + 'js/')); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 50 | }); |
| 51 | |
| 52 | // set templates in cache |
| 53 | gulp.task('templates', function(){ |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 54 | return gulp.src('./src/templates/*.html') |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 55 | .pipe(templateCache({ |
Matteo Scandolo | 6be0cd2 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 56 | module: 'xos.<%= name %>', |
Matteo Scandolo | ba5e19d | 2015-11-06 18:25:52 +0100 | [diff] [blame] | 57 | root: 'templates/', |
| 58 | templateFooter: TEMPLATE_FOOTER |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 59 | })) |
Matteo Scandolo | ce1dd31 | 2015-11-04 11:46:54 +0100 | [diff] [blame] | 60 | .pipe(gulp.dest(options.tmp)); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 61 | }); |
| 62 | |
Matteo Scandolo | 6be0cd2 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 63 | // copy html index to Django Folder |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 64 | gulp.task('copyHtml', ['clean'], function(){ |
| 65 | return gulp.src(options.src + 'index.html') |
Matteo Scandolo | 61a714d | 2015-11-06 11:16:12 +0100 | [diff] [blame] | 66 | // remove dev dependencies from html |
| 67 | .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, '')) |
| 68 | .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, '')) |
Matteo Scandolo | ba5e19d | 2015-11-06 18:25:52 +0100 | [diff] [blame] | 69 | .pipe(replace(/ng-app=".*"\s/, '')) |
Matteo Scandolo | 61a714d | 2015-11-06 11:16:12 +0100 | [diff] [blame] | 70 | // injecting minified files |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 71 | .pipe( |
| 72 | inject( |
| 73 | gulp.src([ |
| 74 | options.static + 'js/vendor/xos<%= fileName %>Vendor.js', |
| 75 | options.static + 'js/xos<%= fileName %>.js' |
Matteo Scandolo | c0c9409 | 2016-02-02 17:25:18 -0800 | [diff] [blame] | 76 | ]), |
| 77 | {ignorePath: '/../../../xos/core/xoslib'} |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 78 | ) |
| 79 | ) |
| 80 | .pipe(rename('xos<%= fileName %>.html')) |
| 81 | .pipe(gulp.dest(options.dashboards)); |
| 82 | }); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 83 | |
| 84 | // minify vendor js files |
| 85 | gulp.task('wiredep', function(){ |
| 86 | var bowerDeps = wiredep().js; |
Matteo Scandolo | cb7ea1e | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 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 | |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 96 | return gulp.src(bowerDeps) |
Matteo Scandolo | cb7ea1e | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 97 | .pipe(concat('xos<%= fileName %>Vendor.js')) |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 98 | .pipe(uglify()) |
Matteo Scandolo | 8a1f236 | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 99 | .pipe(gulp.dest(options.static + 'js/vendor/')); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 100 | }); |
| 101 | |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 102 | gulp.task('lint', function () { |
| 103 | return gulp.src(['src/js/**/*.js']) |
| 104 | .pipe(eslint()) |
| 105 | .pipe(eslint.format()) |
| 106 | .pipe(eslint.failAfterError()); |
| 107 | }); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 108 | |
| 109 | gulp.task('build', function() { |
| 110 | runSequence( |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 111 | 'templates', |
Matteo Scandolo | ce1dd31 | 2015-11-04 11:46:54 +0100 | [diff] [blame] | 112 | 'babel', |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 113 | 'scripts', |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 114 | 'wiredep', |
Matteo Scandolo | 8a1f236 | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 115 | 'copyHtml', |
Matteo Scandolo | ce1dd31 | 2015-11-04 11:46:54 +0100 | [diff] [blame] | 116 | 'cleanTmp' |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 117 | ); |
| 118 | }); |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 119 | }; |