blob: b540fccb1c7810bc2a3fe02efceb3ed02e104879 [file] [log] [blame]
arpiagariud4f6db12016-06-06 15:25:28 -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
30const TEMPLATE_FOOTER = `
31angular.module('xos.tenant')
32.run(['$location', function(a){
33 a.path('/');
34}])
35`
36
37module.exports = function(options){
38
39 // delete previous builded file
40 gulp.task('clean', function(){
41 return del(
42 [
43 options.dashboards + 'xosTenant.html',
Arpit Agarwal03fcec32016-07-18 17:30:32 -070044 options.static + 'css/xosTenant.css',
45 options.static + 'images/*'
arpiagariud4f6db12016-06-06 15:25:28 -070046 ],
47 {force: true}
48 );
49 });
50
51 // minify css
52 gulp.task('css', function () {
53 var processors = [
54 autoprefixer({browsers: ['last 1 version']}),
55 mqpacker,
56 csswring
57 ];
58
59 gulp.src([
60 `${options.css}**/*.css`,
61 `!${options.css}dev.css`
62 ])
63 .pipe(postcss(processors))
64 .pipe(gulp.dest(options.tmp + '/css/'));
65 });
66
67 // copy css in correct folder
68 gulp.task('copyCss', ['wait'], function(){
69 return gulp.src([`${options.tmp}/css/*.css`])
70 .pipe(concat('xosTenant.css'))
71 .pipe(gulp.dest(options.static + 'css/'))
72 });
73
Arpit Agarwal03fcec32016-07-18 17:30:32 -070074 // copy images in correct folder
75 gulp.task('copyImages', ['wait'], function(){
76 return gulp.src([`${options.icon}/*.png`])
77 .pipe(gulp.dest(options.static + 'images/'))
78 });
79
arpiagariud4f6db12016-06-06 15:25:28 -070080 // compile and minify scripts
81 gulp.task('scripts', function() {
82 return gulp.src([
83 options.tmp + '**/*.js'
84 ])
85 .pipe(ngAnnotate())
86 .pipe(angularFilesort())
87 .pipe(concat('xosTenant.js'))
88 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
89 .pipe(concat.footer(TEMPLATE_FOOTER))
90 .pipe(uglify())
91 .pipe(gulp.dest(options.static + 'js/'));
92 });
93
94 // set templates in cache
95 gulp.task('templates', function(){
96 return gulp.src('./src/templates/*.html')
97 .pipe(templateCache({
98 module: 'xos.tenant',
99 root: 'templates/'
100 }))
101 .pipe(gulp.dest(options.tmp));
102 });
103
104 // copy html index to Django Folder
105 gulp.task('copyHtml', function(){
106 return gulp.src(options.src + 'index.html')
107 // remove dev dependencies from html
108 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
109 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
110 // injecting minified files
111 .pipe(
112 inject(
113 gulp.src([
114 options.static + 'js/vendor/xosTenantVendor.js',
115 options.static + 'js/xosTenant.js',
116 options.static + 'css/xosTenant.css'
117 ]),
118 {ignorePath: '/../../../xos/core/xoslib'}
119 )
120 )
121 .pipe(rename('xosTenant.html'))
122 .pipe(gulp.dest(options.dashboards));
123 });
124
125 // minify vendor js files
126 gulp.task('wiredep', function(){
127 var bowerDeps = wiredep().js;
128 if(!bowerDeps){
129 return;
130 }
131
132 // remove angular (it's already loaded)
133 _.remove(bowerDeps, function(dep){
134 return dep.indexOf('angular/angular.js') !== -1;
135 });
136
137 return gulp.src(bowerDeps)
138 .pipe(concat('xosTenantVendor.js'))
139 .pipe(uglify())
140 .pipe(gulp.dest(options.static + 'js/vendor/'));
141 });
142
143 gulp.task('lint', function () {
144 return gulp.src(['src/js/**/*.js'])
145 .pipe(eslint())
146 .pipe(eslint.format())
147 .pipe(eslint.failAfterError());
148 });
149
150 gulp.task('wait', function (cb) {
151 // setTimeout could be any async task
152 setTimeout(function () {
153 cb();
154 }, 1000);
155 });
156
157 gulp.task('build', function() {
158 runSequence(
159 'clean',
160 'sass',
161 'templates',
162 'babel',
163 'scripts',
164 'wiredep',
165 'css',
166 'copyCss',
Arpit Agarwal03fcec32016-07-18 17:30:32 -0700167 'copyImages',
arpiagariud4f6db12016-06-06 15:25:28 -0700168 'copyHtml',
169 'cleanTmp'
170 );
171 });
172};