blob: 1a1be80566f5b6a5fa153256aef9a0ff6e62e05f [file] [log] [blame]
Matteo Scandolo8a64fa42016-01-21 11:21:03 -08001'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
11var gulp = require('gulp');
12var ngAnnotate = require('gulp-ng-annotate');
13var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
Matteo Scandolo70ac2162016-02-24 15:40:22 -080016var concat = require('gulp-concat-util');
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080017var del = require('del');
18var wiredep = require('wiredep');
19var angularFilesort = require('gulp-angular-filesort');
20var _ = require('lodash');
21var eslint = require('gulp-eslint');
22var inject = require('gulp-inject');
23var rename = require('gulp-rename');
24var replace = require('gulp-replace');
Matteo Scandolo70ac2162016-02-24 15:40:22 -080025var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080029
Matteo Scandolodd55c132016-04-13 10:46:08 -070030const TEMPLATE_FOOTER = `
31angular.module('xos.diagnostic')
32.run(['$location', function(a){
33 a.path('/');
34}])
35`
36
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080037module.exports = function(options){
38
39 // delete previous builded file
40 gulp.task('clean', function(){
41 return del(
Matteo Scandolo04564952016-02-24 11:22:48 -080042 [options.dashboards + 'xosDiagnostic.html'],
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080043 {force: true}
44 );
45 });
46
Matteo Scandolo70ac2162016-02-24 15:40:22 -080047 // minify css
48 gulp.task('css', function () {
49 var processors = [
50 autoprefixer({browsers: ['last 1 version']}),
51 mqpacker,
52 csswring
53 ];
Matteo Scandolo9d3face2016-03-29 14:10:16 -070054
Matteo Scandolo70ac2162016-02-24 15:40:22 -080055 gulp.src([
56 `${options.css}**/*.css`,
57 `!${options.css}dev.css`
58 ])
59 .pipe(postcss(processors))
60 .pipe(gulp.dest(options.tmp + '/css/'));
61 });
62
63 gulp.task('copyCss', ['css'], function(){
64 return gulp.src([`${options.tmp}/css/*.css`])
65 .pipe(concat('xosDiagnostic.css'))
66 .pipe(gulp.dest(options.static + 'css/'))
67 });
68
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080069 // compile and minify scripts
70 gulp.task('scripts', function() {
71 return gulp.src([
Matteo Scandolo70ac2162016-02-24 15:40:22 -080072 options.tmp + '**/*.js',
73 options.tmp + 'templates.js'
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080074 ])
75 .pipe(ngAnnotate())
76 .pipe(angularFilesort())
Matteo Scandolo04564952016-02-24 11:22:48 -080077 .pipe(concat('xosDiagnostic.js'))
Matteo Scandolo70ac2162016-02-24 15:40:22 -080078 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
Matteo Scandolodd55c132016-04-13 10:46:08 -070079 .pipe(concat.footer(TEMPLATE_FOOTER))
80 .pipe(uglify())
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080081 .pipe(gulp.dest(options.static + 'js/'));
82 });
83
84 // set templates in cache
85 gulp.task('templates', function(){
86 return gulp.src('./src/templates/*.html')
87 .pipe(templateCache({
Matteo Scandolo70ac2162016-02-24 15:40:22 -080088 module: 'xos.diagnostic',
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080089 root: 'templates/',
Matteo Scandolo70ac2162016-02-24 15:40:22 -080090 // templateFooter: TEMPLATE_FOOTER
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080091 }))
92 .pipe(gulp.dest(options.tmp));
93 });
94
95 // copy html index to Django Folder
96 gulp.task('copyHtml', ['clean'], function(){
97 return gulp.src(options.src + 'index.html')
98 // remove dev dependencies from html
99 .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
100 .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
Matteo Scandolo1df21732016-03-29 14:06:13 -0700101 // .pipe(replace(/ng-app=".*"\s/, ''))
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800102 // injecting minified files
103 .pipe(
104 inject(
105 gulp.src([
Matteo Scandolo04564952016-02-24 11:22:48 -0800106 options.static + 'js/vendor/xosDiagnosticVendor.js',
Matteo Scandolo70ac2162016-02-24 15:40:22 -0800107 options.static + 'js/xosDiagnostic.js',
108 options.static + 'css/xosDiagnostic.css'
109 ]),
110 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800111 )
112 )
Matteo Scandolo04564952016-02-24 11:22:48 -0800113 .pipe(rename('xosDiagnostic.html'))
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800114 .pipe(gulp.dest(options.dashboards));
115 });
116
117 // minify vendor js files
118 gulp.task('wiredep', function(){
119 var bowerDeps = wiredep().js;
120 if(!bowerDeps){
121 return;
122 }
123
124 // remove angular (it's already loaded)
125 _.remove(bowerDeps, function(dep){
126 return dep.indexOf('angular/angular.js') !== -1;
127 });
128
129 return gulp.src(bowerDeps)
Matteo Scandolo04564952016-02-24 11:22:48 -0800130 .pipe(concat('xosDiagnosticVendor.js'))
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800131 .pipe(uglify())
132 .pipe(gulp.dest(options.static + 'js/vendor/'));
133 });
134
135 gulp.task('lint', function () {
136 return gulp.src(['src/js/**/*.js'])
137 .pipe(eslint())
138 .pipe(eslint.format())
139 .pipe(eslint.failAfterError());
140 });
141
Matteo Scandolo9d3face2016-03-29 14:10:16 -0700142 // inject CSS
143 gulp.task('injectCss', function(){
144 return gulp.src(options.src + 'index.html')
145 .pipe(
146 inject(
147 gulp.src(options.src + 'css/*.css'),
148 {
149 ignorePath: [options.src]
150 }
151 )
152 )
153 .pipe(gulp.dest(options.src));
154 });
155
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800156 gulp.task('build', function() {
157 runSequence(
Matteo Scandolo9d3face2016-03-29 14:10:16 -0700158 'lint',
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800159 'templates',
160 'babel',
161 'scripts',
162 'wiredep',
Matteo Scandolo9d3face2016-03-29 14:10:16 -0700163 'injectCss',
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800164 'copyHtml',
Matteo Scandolo70ac2162016-02-24 15:40:22 -0800165 'copyCss'
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800166 );
167 });
168};