blob: c9cd9cb6517885b72f53d0bec9c7aef2a0b5590e [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
30module.exports = function(options){
31
32 // delete previous builded file
33 gulp.task('clean', function(){
34 return del(
Matteo Scandolo04564952016-02-24 11:22:48 -080035 [options.dashboards + 'xosDiagnostic.html'],
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080036 {force: true}
37 );
38 });
39
Matteo Scandolo70ac2162016-02-24 15:40:22 -080040 // minify css
41 gulp.task('css', function () {
42 var processors = [
43 autoprefixer({browsers: ['last 1 version']}),
44 mqpacker,
45 csswring
46 ];
Matteo Scandolo9d3face2016-03-29 14:10:16 -070047
Matteo Scandolo70ac2162016-02-24 15:40:22 -080048 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
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080062 // compile and minify scripts
63 gulp.task('scripts', function() {
64 return gulp.src([
Matteo Scandolo70ac2162016-02-24 15:40:22 -080065 options.tmp + '**/*.js',
66 options.tmp + 'templates.js'
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080067 ])
68 .pipe(ngAnnotate())
69 .pipe(angularFilesort())
Matteo Scandolo04564952016-02-24 11:22:48 -080070 .pipe(concat('xosDiagnostic.js'))
Matteo Scandolo70ac2162016-02-24 15:40:22 -080071 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
Matteo Scandolo1df21732016-03-29 14:06:13 -070072 // .pipe(concat.footer(TEMPLATE_FOOTER))
Matteo Scandolo70ac2162016-02-24 15:40:22 -080073 // .pipe(uglify())
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080074 .pipe(gulp.dest(options.static + 'js/'));
75 });
76
77 // set templates in cache
78 gulp.task('templates', function(){
79 return gulp.src('./src/templates/*.html')
80 .pipe(templateCache({
Matteo Scandolo70ac2162016-02-24 15:40:22 -080081 module: 'xos.diagnostic',
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080082 root: 'templates/',
Matteo Scandolo70ac2162016-02-24 15:40:22 -080083 // templateFooter: TEMPLATE_FOOTER
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080084 }))
85 .pipe(gulp.dest(options.tmp));
86 });
87
88 // copy html index to Django Folder
89 gulp.task('copyHtml', ['clean'], function(){
90 return gulp.src(options.src + 'index.html')
91 // remove dev dependencies from html
92 .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
93 .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
Matteo Scandolo1df21732016-03-29 14:06:13 -070094 // .pipe(replace(/ng-app=".*"\s/, ''))
Matteo Scandolo8a64fa42016-01-21 11:21:03 -080095 // injecting minified files
96 .pipe(
97 inject(
98 gulp.src([
Matteo Scandolo04564952016-02-24 11:22:48 -080099 options.static + 'js/vendor/xosDiagnosticVendor.js',
Matteo Scandolo70ac2162016-02-24 15:40:22 -0800100 options.static + 'js/xosDiagnostic.js',
101 options.static + 'css/xosDiagnostic.css'
102 ]),
103 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800104 )
105 )
Matteo Scandolo04564952016-02-24 11:22:48 -0800106 .pipe(rename('xosDiagnostic.html'))
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800107 .pipe(gulp.dest(options.dashboards));
108 });
109
110 // minify vendor js files
111 gulp.task('wiredep', function(){
112 var bowerDeps = wiredep().js;
113 if(!bowerDeps){
114 return;
115 }
116
117 // remove angular (it's already loaded)
118 _.remove(bowerDeps, function(dep){
119 return dep.indexOf('angular/angular.js') !== -1;
120 });
121
122 return gulp.src(bowerDeps)
Matteo Scandolo04564952016-02-24 11:22:48 -0800123 .pipe(concat('xosDiagnosticVendor.js'))
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800124 .pipe(uglify())
125 .pipe(gulp.dest(options.static + 'js/vendor/'));
126 });
127
128 gulp.task('lint', function () {
129 return gulp.src(['src/js/**/*.js'])
130 .pipe(eslint())
131 .pipe(eslint.format())
132 .pipe(eslint.failAfterError());
133 });
134
Matteo Scandolo9d3face2016-03-29 14:10:16 -0700135 // inject CSS
136 gulp.task('injectCss', function(){
137 return gulp.src(options.src + 'index.html')
138 .pipe(
139 inject(
140 gulp.src(options.src + 'css/*.css'),
141 {
142 ignorePath: [options.src]
143 }
144 )
145 )
146 .pipe(gulp.dest(options.src));
147 });
148
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800149 gulp.task('build', function() {
150 runSequence(
Matteo Scandolo9d3face2016-03-29 14:10:16 -0700151 'lint',
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800152 'templates',
153 'babel',
154 'scripts',
155 'wiredep',
Matteo Scandolo9d3face2016-03-29 14:10:16 -0700156 'injectCss',
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800157 'copyHtml',
Matteo Scandolo70ac2162016-02-24 15:40:22 -0800158 'copyCss'
Matteo Scandolo8a64fa42016-01-21 11:21:03 -0800159 );
160 });
161};