blob: 9cba01550d1df8582ce589ed2254e924357aa5d0 [file] [log] [blame]
Matteo Scandolof0c32262016-03-11 13:47:27 -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');
16var concat = require('gulp-concat');
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');
Matteo Scandolo45586982016-03-11 13:53:12 -080025var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
Matteo Scandolof0c32262016-03-11 13:47:27 -080029
30var TEMPLATE_FOOTER = `}]);
31angular.module('xos.truckroll').run(function($location){$location.path('/')});
32angular.bootstrap(angular.element('#xosTruckroll'), ['xos.truckroll']);`;
33
34module.exports = function(options){
35
36 // delete previous builded file
37 gulp.task('clean', function(){
38 return del(
39 [options.dashboards + 'xosTruckroll.html'],
40 {force: true}
41 );
42 });
43
44 // minify css
45 gulp.task('css', function () {
46 var processors = [
47 autoprefixer({browsers: ['last 1 version']}),
48 mqpacker,
49 csswring
50 ];
Matteo Scandolo45586982016-03-11 13:53:12 -080051
Matteo Scandolof0c32262016-03-11 13:47:27 -080052 gulp.src([
53 `${options.css}**/*.css`,
54 `!${options.css}dev.css`
55 ])
56 .pipe(postcss(processors))
57 .pipe(gulp.dest(options.tmp + '/css/'));
58 });
59
60 gulp.task('copyCss', ['css'], function(){
61 return gulp.src([`${options.tmp}/css/*.css`])
Matteo Scandolo45586982016-03-11 13:53:12 -080062 .pipe(concat('xosTruckroll.css'))
Matteo Scandolof0c32262016-03-11 13:47:27 -080063 .pipe(gulp.dest(options.static + 'css/'))
64 });
65
66 // compile and minify scripts
67 gulp.task('scripts', function() {
68 return gulp.src([
69 options.tmp + '**/*.js'
70 ])
71 .pipe(ngAnnotate())
72 .pipe(angularFilesort())
73 .pipe(concat('xosTruckroll.js'))
74 .pipe(uglify())
75 .pipe(gulp.dest(options.static + 'js/'));
76 });
77
78 // set templates in cache
79 gulp.task('templates', function(){
80 return gulp.src('./src/templates/*.html')
81 .pipe(templateCache({
82 module: 'xos.truckroll',
83 root: 'templates/',
84 templateFooter: TEMPLATE_FOOTER
85 }))
86 .pipe(gulp.dest(options.tmp));
87 });
88
89 // copy html index to Django Folder
90 gulp.task('copyHtml', ['clean'], function(){
91 return gulp.src(options.src + 'index.html')
92 // remove dev dependencies from html
93 .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
94 .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
95 .pipe(replace(/ng-app=".*"\s/, ''))
96 // injecting minified files
97 .pipe(
98 inject(
99 gulp.src([
100 options.static + 'js/vendor/xosTruckrollVendor.js',
101 options.static + 'js/xosTruckroll.js'
102 ]),
103 {ignorePath: '/../../../xos/core/xoslib'}
104 )
105 )
106 .pipe(rename('xosTruckroll.html'))
107 .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)
123 .pipe(concat('xosTruckrollVendor.js'))
124 .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
135 gulp.task('build', function() {
136 runSequence(
137 'templates',
138 'babel',
139 'scripts',
140 'wiredep',
141 'copyHtml',
142 'cleanTmp',
143 'copyCss'
144 );
145 });
146};