blob: 20bbc14d960ac66e211c8b0db962d4cf47a91230 [file] [log] [blame]
Matteo Scandolobe9b13d2016-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 Scandolo16763e92016-02-24 15:40:22 -080016var concat = require('gulp-concat-util');
Matteo Scandolobe9b13d2016-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 Scandolo16763e92016-02-24 15:40:22 -080025var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080029
Matteo Scandoloc44460c2016-04-13 10:46:08 -070030const TEMPLATE_FOOTER = `
31angular.module('xos.diagnostic')
32.run(['$location', function(a){
33 a.path('/');
34}])
35`
36
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080037module.exports = function(options){
38
39 // delete previous builded file
40 gulp.task('clean', function(){
41 return del(
Matteo Scandolo881fed52016-05-23 15:31:25 -070042 [
43 options.dashboards + 'xosDiagnostic.html',
44 options.static + 'css/xosDiagnostic.css'
45 ],
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080046 {force: true}
47 );
48 });
49
Matteo Scandolo16763e92016-02-24 15:40:22 -080050 // minify css
51 gulp.task('css', function () {
52 var processors = [
53 autoprefixer({browsers: ['last 1 version']}),
54 mqpacker,
55 csswring
56 ];
Matteo Scandolo55f39472016-03-29 14:10:16 -070057
Matteo Scandolo16763e92016-02-24 15:40:22 -080058 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
Matteo Scandolo881fed52016-05-23 15:31:25 -070066 // copy css in correct folder
67 gulp.task('copyCss', ['wait'], function(){
Matteo Scandolo16763e92016-02-24 15:40:22 -080068 return gulp.src([`${options.tmp}/css/*.css`])
69 .pipe(concat('xosDiagnostic.css'))
70 .pipe(gulp.dest(options.static + 'css/'))
71 });
72
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080073 // compile and minify scripts
74 gulp.task('scripts', function() {
75 return gulp.src([
Matteo Scandolo881fed52016-05-23 15:31:25 -070076 options.tmp + '**/*.js'
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080077 ])
78 .pipe(ngAnnotate())
79 .pipe(angularFilesort())
Matteo Scandolo4b3d8722016-02-24 11:22:48 -080080 .pipe(concat('xosDiagnostic.js'))
Matteo Scandolo16763e92016-02-24 15:40:22 -080081 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
Matteo Scandoloc44460c2016-04-13 10:46:08 -070082 .pipe(concat.footer(TEMPLATE_FOOTER))
83 .pipe(uglify())
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080084 .pipe(gulp.dest(options.static + 'js/'));
85 });
86
87 // set templates in cache
88 gulp.task('templates', function(){
89 return gulp.src('./src/templates/*.html')
90 .pipe(templateCache({
Matteo Scandolo16763e92016-02-24 15:40:22 -080091 module: 'xos.diagnostic',
Matteo Scandolo881fed52016-05-23 15:31:25 -070092 root: 'templates/'
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080093 }))
94 .pipe(gulp.dest(options.tmp));
95 });
96
97 // copy html index to Django Folder
Matteo Scandolo881fed52016-05-23 15:31:25 -070098 gulp.task('copyHtml', function(){
Matteo Scandolobe9b13d2016-01-21 11:21:03 -080099 return gulp.src(options.src + 'index.html')
100 // remove dev dependencies from html
Matteo Scandolo881fed52016-05-23 15:31:25 -0700101 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
102 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800103 // injecting minified files
104 .pipe(
105 inject(
106 gulp.src([
Matteo Scandolo4b3d8722016-02-24 11:22:48 -0800107 options.static + 'js/vendor/xosDiagnosticVendor.js',
Matteo Scandolo16763e92016-02-24 15:40:22 -0800108 options.static + 'js/xosDiagnostic.js',
109 options.static + 'css/xosDiagnostic.css'
110 ]),
111 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800112 )
113 )
Matteo Scandolo4b3d8722016-02-24 11:22:48 -0800114 .pipe(rename('xosDiagnostic.html'))
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800115 .pipe(gulp.dest(options.dashboards));
116 });
117
118 // minify vendor js files
119 gulp.task('wiredep', function(){
120 var bowerDeps = wiredep().js;
121 if(!bowerDeps){
122 return;
123 }
124
125 // remove angular (it's already loaded)
126 _.remove(bowerDeps, function(dep){
127 return dep.indexOf('angular/angular.js') !== -1;
128 });
129
130 return gulp.src(bowerDeps)
Matteo Scandolo4b3d8722016-02-24 11:22:48 -0800131 .pipe(concat('xosDiagnosticVendor.js'))
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800132 .pipe(uglify())
133 .pipe(gulp.dest(options.static + 'js/vendor/'));
134 });
135
136 gulp.task('lint', function () {
137 return gulp.src(['src/js/**/*.js'])
138 .pipe(eslint())
139 .pipe(eslint.format())
140 .pipe(eslint.failAfterError());
141 });
142
Matteo Scandolo881fed52016-05-23 15:31:25 -0700143 gulp.task('wait', function (cb) {
144 // setTimeout could be any async task
145 setTimeout(function () {
146 cb();
147 }, 1000);
Matteo Scandolo55f39472016-03-29 14:10:16 -0700148 });
149
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800150 gulp.task('build', function() {
151 runSequence(
Matteo Scandolo881fed52016-05-23 15:31:25 -0700152 'clean',
153 'sass',
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800154 'templates',
155 'babel',
156 'scripts',
157 'wiredep',
Matteo Scandolo881fed52016-05-23 15:31:25 -0700158 'css',
159 'copyCss',
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800160 'copyHtml',
Matteo Scandolo881fed52016-05-23 15:31:25 -0700161 'cleanTmp'
Matteo Scandolobe9b13d2016-01-21 11:21:03 -0800162 );
163 });
164};