Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | // BUILD |
Matteo Scandolo | 7cf73ad | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 4 | // |
Matteo Scandolo | f813b6a | 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 | 7cf73ad | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 8 | // |
Matteo Scandolo | f813b6a | 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 | ff81c69 | 2015-11-04 16:10:09 +0100 | [diff] [blame] | 12 | var ngAnnotate = require('gulp-ng-annotate'); |
Matteo Scandolo | f813b6a | 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 | 7cf73ad | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 16 | var concat = require('gulp-concat'); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 17 | var del = require('del'); |
| 18 | var wiredep = require('wiredep'); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 19 | var angularFilesort = require('gulp-angular-filesort'); |
Matteo Scandolo | 352b601 | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 20 | var _ = require('lodash'); |
Matteo Scandolo | 7cf73ad | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 21 | var eslint = require('gulp-eslint'); |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 22 | var inject = require('gulp-inject'); |
| 23 | var rename = require('gulp-rename'); |
Matteo Scandolo | 1585579 | 2015-11-06 11:16:12 +0100 | [diff] [blame] | 24 | var replace = require('gulp-replace'); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 25 | |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 26 | module.exports = function(options){ |
| 27 | |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 28 | // delete previous builded file |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 29 | gulp.task('clean', function(){ |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 30 | return del( |
| 31 | [options.dashboards + 'xos<%= fileName %>.html'], |
| 32 | {force: true} |
| 33 | ); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 34 | }); |
| 35 | |
Matteo Scandolo | ba1ed91 | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 36 | // inject CSS |
| 37 | gulp.task('injectCss', function(){ |
| 38 | return gulp.src(options.src + 'index.html') |
| 39 | .pipe( |
| 40 | inject( |
| 41 | gulp.src(options.src + 'css/*.css'), |
| 42 | { |
| 43 | ignorePath: [options.src] |
| 44 | } |
| 45 | ) |
| 46 | ) |
| 47 | .pipe(gulp.dest(options.src)); |
| 48 | }); |
| 49 | |
| 50 | // minify css |
| 51 | gulp.task('css', function () { |
| 52 | var processors = [ |
| 53 | autoprefixer({browsers: ['last 1 version']}), |
| 54 | mqpacker, |
| 55 | csswring |
| 56 | ]; |
| 57 | |
| 58 | gulp.src([ |
| 59 | `${options.css}**/*.css`, |
| 60 | `!${options.css}dev.css` |
| 61 | ]) |
| 62 | .pipe(postcss(processors)) |
| 63 | .pipe(gulp.dest(options.tmp + '/css/')); |
| 64 | }); |
| 65 | |
| 66 | // copy css in correct folder |
| 67 | gulp.task('copyCss', ['css'], function(){ |
| 68 | return gulp.src([`${options.tmp}/css/*.css`]) |
| 69 | .pipe(concat('xosDiagnostic.css')) |
| 70 | .pipe(gulp.dest(options.static + 'css/')) |
| 71 | }); |
| 72 | |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 73 | // compile and minify scripts |
| 74 | gulp.task('scripts', function() { |
| 75 | return gulp.src([ |
Matteo Scandolo | 724747c | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 76 | options.tmp + '**/*.js' |
| 77 | ]) |
| 78 | .pipe(ngAnnotate()) |
| 79 | .pipe(angularFilesort()) |
| 80 | .pipe(concat('xos<%= fileName %>.js')) |
| 81 | .pipe(uglify()) |
| 82 | .pipe(gulp.dest(options.static + 'js/')); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 83 | }); |
| 84 | |
| 85 | // set templates in cache |
| 86 | gulp.task('templates', function(){ |
Matteo Scandolo | 7cf73ad | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 87 | return gulp.src('./src/templates/*.html') |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 88 | .pipe(templateCache({ |
Matteo Scandolo | dc88bf1 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 89 | module: 'xos.<%= name %>', |
Matteo Scandolo | ba1ed91 | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 90 | root: 'templates/' |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 91 | })) |
Matteo Scandolo | ac92b72 | 2015-11-04 11:46:54 +0100 | [diff] [blame] | 92 | .pipe(gulp.dest(options.tmp)); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 93 | }); |
| 94 | |
Matteo Scandolo | dc88bf1 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 95 | // copy html index to Django Folder |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 96 | gulp.task('copyHtml', ['clean'], function(){ |
| 97 | return gulp.src(options.src + 'index.html') |
Matteo Scandolo | 1585579 | 2015-11-06 11:16:12 +0100 | [diff] [blame] | 98 | // remove dev dependencies from html |
| 99 | .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, '')) |
| 100 | .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, '')) |
| 101 | // injecting minified files |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 102 | .pipe( |
| 103 | inject( |
| 104 | gulp.src([ |
| 105 | options.static + 'js/vendor/xos<%= fileName %>Vendor.js', |
| 106 | options.static + 'js/xos<%= fileName %>.js' |
Matteo Scandolo | 0c52348 | 2016-02-02 17:25:18 -0800 | [diff] [blame] | 107 | ]), |
| 108 | {ignorePath: '/../../../xos/core/xoslib'} |
Matteo Scandolo | c779496 | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 109 | ) |
| 110 | ) |
| 111 | .pipe(rename('xos<%= fileName %>.html')) |
| 112 | .pipe(gulp.dest(options.dashboards)); |
| 113 | }); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 114 | |
| 115 | // minify vendor js files |
| 116 | gulp.task('wiredep', function(){ |
| 117 | var bowerDeps = wiredep().js; |
Matteo Scandolo | 352b601 | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 118 | if(!bowerDeps){ |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | // remove angular (it's already loaded) |
| 123 | _.remove(bowerDeps, function(dep){ |
| 124 | return dep.indexOf('angular/angular.js') !== -1; |
| 125 | }); |
| 126 | |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 127 | return gulp.src(bowerDeps) |
Matteo Scandolo | 352b601 | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 128 | .pipe(concat('xos<%= fileName %>Vendor.js')) |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 129 | .pipe(uglify()) |
Matteo Scandolo | 724747c | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 130 | .pipe(gulp.dest(options.static + 'js/vendor/')); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 131 | }); |
| 132 | |
Matteo Scandolo | 7cf73ad | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 133 | gulp.task('lint', function () { |
| 134 | return gulp.src(['src/js/**/*.js']) |
| 135 | .pipe(eslint()) |
| 136 | .pipe(eslint.format()) |
| 137 | .pipe(eslint.failAfterError()); |
| 138 | }); |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 139 | |
| 140 | gulp.task('build', function() { |
| 141 | runSequence( |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 142 | 'templates', |
Matteo Scandolo | ac92b72 | 2015-11-04 11:46:54 +0100 | [diff] [blame] | 143 | 'babel', |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 144 | 'scripts', |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 145 | 'wiredep', |
Matteo Scandolo | ba1ed91 | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 146 | 'injectCss', |
Matteo Scandolo | 724747c | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 147 | 'copyHtml', |
Matteo Scandolo | ac92b72 | 2015-11-04 11:46:54 +0100 | [diff] [blame] | 148 | 'cleanTmp' |
Matteo Scandolo | f813b6a | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 149 | ); |
| 150 | }); |
Matteo Scandolo | 7cf73ad | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 151 | }; |