blob: 88d88d33bffb243133ca4701a0e011c67101caa6 [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 Scandolodd55c132016-04-13 10:46:08 -070016var concat = require('gulp-concat-util');
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 Scandolo7ae7d8d2016-04-13 11:41:31 -070025var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010029
Matteo Scandolodd55c132016-04-13 10:46:08 -070030const TEMPLATE_FOOTER = `
31angular.module('xos.<%= name %>')
32.run(['$location', function(a){
33 a.path('/');
34}])
35`
36
Matteo Scandoloe53ee052015-11-03 14:32:00 +010037module.exports = function(options){
38
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010039 // delete previous builded file
Matteo Scandoloe53ee052015-11-03 14:32:00 +010040 gulp.task('clean', function(){
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010041 return del(
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070042 [
43 options.dashboards + 'xos<%= fileName %>.html',
Arpit Agarwalfecac782016-07-18 17:30:32 -070044 options.static + 'css/xos<%= fileName %>.css',
45 options.static + 'images/<%= name %>-icon.png',
46 options.static + 'images/<%= name %>-icon-active.png'
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070047 ],
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010048 {force: true}
49 );
Matteo Scandoloe53ee052015-11-03 14:32:00 +010050 });
51
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070052 // minify css
53 gulp.task('css', function () {
54 var processors = [
55 autoprefixer({browsers: ['last 1 version']}),
56 mqpacker,
57 csswring
58 ];
59
60 gulp.src([
61 `${options.css}**/*.css`,
62 `!${options.css}dev.css`
63 ])
64 .pipe(postcss(processors))
65 .pipe(gulp.dest(options.tmp + '/css/'));
66 });
67
68 // copy css in correct folder
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -070069 gulp.task('copyCss', ['wait'], function(){
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070070 return gulp.src([`${options.tmp}/css/*.css`])
Matteo Scandolodd55c132016-04-13 10:46:08 -070071 .pipe(concat('xos<%= fileName %>.css'))
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070072 .pipe(gulp.dest(options.static + 'css/'))
73 });
74
Arpit Agarwalfecac782016-07-18 17:30:32 -070075 // copy images in correct folder
76 gulp.task('copyImages', ['wait'], function(){
77 return gulp.src([`${options.icon}/<%= name %>-icon.png`,`${options.icon}/<%= name %>-icon-active.png`])
78 .pipe(gulp.dest(options.static + 'images/'))
79 });
80
Matteo Scandoloe53ee052015-11-03 14:32:00 +010081 // compile and minify scripts
82 gulp.task('scripts', function() {
83 return gulp.src([
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010084 options.tmp + '**/*.js'
85 ])
86 .pipe(ngAnnotate())
87 .pipe(angularFilesort())
88 .pipe(concat('xos<%= fileName %>.js'))
Matteo Scandolodd55c132016-04-13 10:46:08 -070089 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
90 .pipe(concat.footer(TEMPLATE_FOOTER))
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010091 .pipe(uglify())
92 .pipe(gulp.dest(options.static + 'js/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +010093 });
94
95 // set templates in cache
96 gulp.task('templates', function(){
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010097 return gulp.src('./src/templates/*.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +010098 .pipe(templateCache({
Matteo Scandolo6be0cd22015-11-03 16:27:07 +010099 module: 'xos.<%= name %>',
Matteo Scandolob7c91cf2016-03-29 14:23:20 -0700100 root: 'templates/'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100101 }))
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100102 .pipe(gulp.dest(options.tmp));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100103 });
104
Matteo Scandolo6be0cd22015-11-03 16:27:07 +0100105 // copy html index to Django Folder
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700106 gulp.task('copyHtml', function(){
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100107 return gulp.src(options.src + 'index.html')
Matteo Scandolo61a714d2015-11-06 11:16:12 +0100108 // remove dev dependencies from html
Matteo Scandolo34d35962016-05-10 14:58:47 -0700109 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
110 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
Matteo Scandolo61a714d2015-11-06 11:16:12 +0100111 // injecting minified files
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100112 .pipe(
113 inject(
114 gulp.src([
Matteo Scandolo195dde92016-07-25 16:43:16 -0700115 options.static + 'vendor/xos<%= fileName %>Vendor.js',
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700116 options.static + 'js/xos<%= fileName %>.js',
117 options.static + 'css/xos<%= fileName %>.css'
Matteo Scandoloc0c94092016-02-02 17:25:18 -0800118 ]),
119 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100120 )
121 )
122 .pipe(rename('xos<%= fileName %>.html'))
123 .pipe(gulp.dest(options.dashboards));
124 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100125
126 // minify vendor js files
127 gulp.task('wiredep', function(){
128 var bowerDeps = wiredep().js;
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100129 if(!bowerDeps){
130 return;
131 }
132
133 // remove angular (it's already loaded)
134 _.remove(bowerDeps, function(dep){
135 return dep.indexOf('angular/angular.js') !== -1;
136 });
137
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100138 return gulp.src(bowerDeps)
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100139 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100140 .pipe(uglify())
Matteo Scandolo195dde92016-07-25 16:43:16 -0700141 .pipe(gulp.dest(options.static + 'vendor/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100142 });
143
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100144 gulp.task('lint', function () {
145 return gulp.src(['src/js/**/*.js'])
146 .pipe(eslint())
147 .pipe(eslint.format())
148 .pipe(eslint.failAfterError());
149 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100150
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700151 gulp.task('wait', function (cb) {
152 // setTimeout could be any async task
153 setTimeout(function () {
154 cb();
155 }, 1000);
156 });
157
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100158 gulp.task('build', function() {
159 runSequence(
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700160 'clean',
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700161 'sass',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100162 'templates',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100163 'babel',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100164 'scripts',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100165 'wiredep',
Matteo Scandolo7ae7d8d2016-04-13 11:41:31 -0700166 'css',
167 'copyCss',
Arpit Agarwalfecac782016-07-18 17:30:32 -0700168 'copyImages',
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100169 'copyHtml',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100170 'cleanTmp'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100171 );
172 });
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100173};