blob: bd31a256112d5e0434906392a63f0206161682e0 [file] [log] [blame]
Matteo Scandoloe53ee052015-11-03 14:32:00 +01001'use strict';
2
3// BUILD
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +01004//
Matteo Scandoloe53ee052015-11-03 14:32:00 +01005// 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 Scandoloe3bc18d2015-11-05 18:36:10 +01008//
Matteo Scandoloe53ee052015-11-03 14:32:00 +01009// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
Matteo Scandolod838fac2015-11-04 16:10:09 +010012var ngAnnotate = require('gulp-ng-annotate');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010013var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010016var concat = require('gulp-concat');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010017var del = require('del');
18var wiredep = require('wiredep');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010019var angularFilesort = require('gulp-angular-filesort');
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +010020var _ = require('lodash');
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010021var eslint = require('gulp-eslint');
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010022var inject = require('gulp-inject');
23var rename = require('gulp-rename');
Matteo Scandolo61a714d2015-11-06 11:16:12 +010024var replace = require('gulp-replace');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010025
Matteo Scandoloe53ee052015-11-03 14:32:00 +010026module.exports = function(options){
27
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010028 // delete previous builded file
Matteo Scandoloe53ee052015-11-03 14:32:00 +010029 gulp.task('clean', function(){
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010030 return del(
31 [options.dashboards + 'xos<%= fileName %>.html'],
32 {force: true}
33 );
Matteo Scandoloe53ee052015-11-03 14:32:00 +010034 });
35
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070036 // 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 Scandoloe53ee052015-11-03 14:32:00 +010073 // compile and minify scripts
74 gulp.task('scripts', function() {
75 return gulp.src([
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010076 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 Scandoloe53ee052015-11-03 14:32:00 +010083 });
84
85 // set templates in cache
86 gulp.task('templates', function(){
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010087 return gulp.src('./src/templates/*.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +010088 .pipe(templateCache({
Matteo Scandolo6be0cd22015-11-03 16:27:07 +010089 module: 'xos.<%= name %>',
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070090 root: 'templates/'
Matteo Scandoloe53ee052015-11-03 14:32:00 +010091 }))
Matteo Scandoloce1dd312015-11-04 11:46:54 +010092 .pipe(gulp.dest(options.tmp));
Matteo Scandoloe53ee052015-11-03 14:32:00 +010093 });
94
Matteo Scandolo6be0cd22015-11-03 16:27:07 +010095 // copy html index to Django Folder
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010096 gulp.task('copyHtml', ['clean'], function(){
97 return gulp.src(options.src + 'index.html')
Matteo Scandolo61a714d2015-11-06 11:16:12 +010098 // 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 Scandoloba2af3d2015-11-06 10:35:20 +0100102 .pipe(
103 inject(
104 gulp.src([
105 options.static + 'js/vendor/xos<%= fileName %>Vendor.js',
106 options.static + 'js/xos<%= fileName %>.js'
Matteo Scandoloc0c94092016-02-02 17:25:18 -0800107 ]),
108 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100109 )
110 )
111 .pipe(rename('xos<%= fileName %>.html'))
112 .pipe(gulp.dest(options.dashboards));
113 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100114
115 // minify vendor js files
116 gulp.task('wiredep', function(){
117 var bowerDeps = wiredep().js;
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100118 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 Scandoloe53ee052015-11-03 14:32:00 +0100127 return gulp.src(bowerDeps)
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100128 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100129 .pipe(uglify())
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100130 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100131 });
132
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100133 gulp.task('lint', function () {
134 return gulp.src(['src/js/**/*.js'])
135 .pipe(eslint())
136 .pipe(eslint.format())
137 .pipe(eslint.failAfterError());
138 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100139
140 gulp.task('build', function() {
141 runSequence(
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100142 'templates',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100143 'babel',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100144 'scripts',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100145 'wiredep',
Matteo Scandolob7c91cf2016-03-29 14:23:20 -0700146 'injectCss',
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100147 'copyHtml',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100148 'cleanTmp'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100149 );
150 });
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100151};