blob: e90581d295321dd43ca4bf2fa1eca95a1192f185 [file] [log] [blame]
Matteo Scandolo42e3fe22016-05-27 14:52:37 -07001'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');
16var concat = require('gulp-concat-util');
17var 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');
25var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
29
Matteo Scandolo29194952016-06-17 11:57:05 -070030const TEMPLATE_FOOTER = ``;
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070031
32module.exports = function(options){
33
34 // delete previous builded file
35 gulp.task('clean', function(){
36 return del(
37 [
38 options.dashboards + 'xosSynchronizerNotifier.html',
Matteo Scandolo195dde92016-07-25 16:43:16 -070039 options.static + 'css/xosSynchronizerNotifier.css',
40 options.static + 'images/synchronizerNotifier-icon.png',
41 options.static + 'images/synchronizerNotifier-icon-active.png'
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070042 ],
43 {force: true}
44 );
45 });
46
47 // minify css
48 gulp.task('css', function () {
49 var processors = [
50 autoprefixer({browsers: ['last 1 version']}),
51 mqpacker,
52 csswring
53 ];
54
55 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 // copy css in correct folder
64 gulp.task('copyCss', ['wait'], function(){
65 return gulp.src([`${options.tmp}/css/*.css`])
66 .pipe(concat('xosSynchronizerNotifier.css'))
67 .pipe(gulp.dest(options.static + 'css/'))
68 });
69
Matteo Scandolo195dde92016-07-25 16:43:16 -070070 // copy images in correct folder
71 gulp.task('copyImages', ['wait'], function(){
72 return gulp.src([`${options.icon}/synchronizerNotifier-icon.png`,`${options.icon}/synchronizerNotifier-icon-active.png`])
73 .pipe(gulp.dest(options.static + 'images/'))
74 });
75
Matteo Scandolo42e3fe22016-05-27 14:52:37 -070076 // compile and minify scripts
77 gulp.task('scripts', function() {
78 return gulp.src([
79 options.tmp + '**/*.js'
80 ])
81 .pipe(ngAnnotate())
82 .pipe(angularFilesort())
83 .pipe(concat('xosSynchronizerNotifier.js'))
84 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
85 .pipe(concat.footer(TEMPLATE_FOOTER))
86 .pipe(uglify())
87 .pipe(gulp.dest(options.static + 'js/'));
88 });
89
90 // set templates in cache
91 gulp.task('templates', function(){
92 return gulp.src('./src/templates/*.html')
93 .pipe(templateCache({
94 module: 'xos.synchronizerNotifier',
95 root: 'templates/'
96 }))
97 .pipe(gulp.dest(options.tmp));
98 });
99
100 // copy html index to Django Folder
101 gulp.task('copyHtml', function(){
102 return gulp.src(options.src + 'index.html')
103 // remove dev dependencies from html
104 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
105 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
106 // injecting minified files
107 .pipe(
108 inject(
109 gulp.src([
Matteo Scandolo195dde92016-07-25 16:43:16 -0700110 options.static + 'vendor/xosSynchronizerNotifierVendor.js',
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700111 options.static + 'js/xosSynchronizerNotifier.js',
112 options.static + 'css/xosSynchronizerNotifier.css'
113 ]),
114 {ignorePath: '/../../../xos/core/xoslib'}
115 )
116 )
117 .pipe(rename('xosSynchronizerNotifier.html'))
118 .pipe(gulp.dest(options.dashboards));
119 });
120
121 // minify vendor js files
122 gulp.task('wiredep', function(){
123 var bowerDeps = wiredep().js;
124 if(!bowerDeps){
125 return;
126 }
127
128 // remove angular (it's already loaded)
129 _.remove(bowerDeps, function(dep){
130 return dep.indexOf('angular/angular.js') !== -1;
131 });
132
133 return gulp.src(bowerDeps)
134 .pipe(concat('xosSynchronizerNotifierVendor.js'))
135 .pipe(uglify())
Matteo Scandolo195dde92016-07-25 16:43:16 -0700136 .pipe(gulp.dest(options.static + 'vendor/'));
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700137 });
138
139 gulp.task('lint', function () {
140 return gulp.src(['src/js/**/*.js'])
141 .pipe(eslint())
142 .pipe(eslint.format())
143 .pipe(eslint.failAfterError());
144 });
145
146 gulp.task('wait', function (cb) {
147 // setTimeout could be any async task
148 setTimeout(function () {
149 cb();
150 }, 1000);
151 });
152
153 gulp.task('build', function() {
154 runSequence(
155 'clean',
156 'sass',
157 'templates',
158 'babel',
159 'scripts',
160 'wiredep',
161 'css',
162 'copyCss',
Matteo Scandolo195dde92016-07-25 16:43:16 -0700163 'copyImages',
Matteo Scandolo42e3fe22016-05-27 14:52:37 -0700164 'copyHtml',
165 'cleanTmp'
166 );
167 });
168};