blob: b232ef926c5d5260383b9380bc1f200aa0ef9bff [file] [log] [blame]
Matteo Scandolof813b6a2015-11-03 14:32:00 +01001'use strict';
2
3// BUILD
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +01004//
Matteo Scandolof813b6a2015-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 Scandolo7cf73ad2015-11-05 18:36:10 +01008//
Matteo Scandolof813b6a2015-11-03 14:32:00 +01009// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
Matteo Scandoloff81c692015-11-04 16:10:09 +010012var ngAnnotate = require('gulp-ng-annotate');
Matteo Scandolof813b6a2015-11-03 14:32:00 +010013var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +010016var concat = require('gulp-concat');
Matteo Scandolof813b6a2015-11-03 14:32:00 +010017var del = require('del');
18var wiredep = require('wiredep');
Matteo Scandolof813b6a2015-11-03 14:32:00 +010019var angularFilesort = require('gulp-angular-filesort');
Matteo Scandolo352b6012015-11-04 15:07:36 +010020var _ = require('lodash');
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +010021var eslint = require('gulp-eslint');
Matteo Scandoloc7794962015-11-06 10:35:20 +010022var inject = require('gulp-inject');
23var rename = require('gulp-rename');
Matteo Scandolo15855792015-11-06 11:16:12 +010024var replace = require('gulp-replace');
Matteo Scandolof813b6a2015-11-03 14:32:00 +010025
Matteo Scandolo807fb012015-11-09 11:25:40 +010026var TEMPLATE_FOOTER = `}]);
27angular.module('xos.<%= name %>').run(function($location){$location.path('/')});
28angular.bootstrap(angular.element('#xos<%= fileName %>'), ['xos.<%= name %>']);`;
29
Matteo Scandolof813b6a2015-11-03 14:32:00 +010030module.exports = function(options){
31
Matteo Scandoloc7794962015-11-06 10:35:20 +010032 // delete previous builded file
Matteo Scandolof813b6a2015-11-03 14:32:00 +010033 gulp.task('clean', function(){
Matteo Scandoloc7794962015-11-06 10:35:20 +010034 return del(
35 [options.dashboards + 'xos<%= fileName %>.html'],
36 {force: true}
37 );
Matteo Scandolof813b6a2015-11-03 14:32:00 +010038 });
39
40 // compile and minify scripts
41 gulp.task('scripts', function() {
42 return gulp.src([
Matteo Scandolo724747c2015-11-06 09:52:01 +010043 options.tmp + '**/*.js'
44 ])
45 .pipe(ngAnnotate())
46 .pipe(angularFilesort())
47 .pipe(concat('xos<%= fileName %>.js'))
48 .pipe(uglify())
49 .pipe(gulp.dest(options.static + 'js/'));
Matteo Scandolof813b6a2015-11-03 14:32:00 +010050 });
51
52 // set templates in cache
53 gulp.task('templates', function(){
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +010054 return gulp.src('./src/templates/*.html')
Matteo Scandolof813b6a2015-11-03 14:32:00 +010055 .pipe(templateCache({
Matteo Scandolodc88bf12015-11-03 16:27:07 +010056 module: 'xos.<%= name %>',
Matteo Scandoloab91f432015-11-06 18:25:52 +010057 root: 'templates/',
58 templateFooter: TEMPLATE_FOOTER
Matteo Scandolof813b6a2015-11-03 14:32:00 +010059 }))
Matteo Scandoloac92b722015-11-04 11:46:54 +010060 .pipe(gulp.dest(options.tmp));
Matteo Scandolof813b6a2015-11-03 14:32:00 +010061 });
62
Matteo Scandolodc88bf12015-11-03 16:27:07 +010063 // copy html index to Django Folder
Matteo Scandoloc7794962015-11-06 10:35:20 +010064 gulp.task('copyHtml', ['clean'], function(){
65 return gulp.src(options.src + 'index.html')
Matteo Scandolo15855792015-11-06 11:16:12 +010066 // remove dev dependencies from html
67 .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
68 .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
Matteo Scandoloab91f432015-11-06 18:25:52 +010069 .pipe(replace(/ng-app=".*"\s/, ''))
Matteo Scandolo15855792015-11-06 11:16:12 +010070 // injecting minified files
Matteo Scandoloc7794962015-11-06 10:35:20 +010071 .pipe(
72 inject(
73 gulp.src([
74 options.static + 'js/vendor/xos<%= fileName %>Vendor.js',
75 options.static + 'js/xos<%= fileName %>.js'
Matteo Scandolo0c523482016-02-02 17:25:18 -080076 ]),
77 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloc7794962015-11-06 10:35:20 +010078 )
79 )
80 .pipe(rename('xos<%= fileName %>.html'))
81 .pipe(gulp.dest(options.dashboards));
82 });
Matteo Scandolof813b6a2015-11-03 14:32:00 +010083
84 // minify vendor js files
85 gulp.task('wiredep', function(){
86 var bowerDeps = wiredep().js;
Matteo Scandolo352b6012015-11-04 15:07:36 +010087 if(!bowerDeps){
88 return;
89 }
90
91 // remove angular (it's already loaded)
92 _.remove(bowerDeps, function(dep){
93 return dep.indexOf('angular/angular.js') !== -1;
94 });
95
Matteo Scandolof813b6a2015-11-03 14:32:00 +010096 return gulp.src(bowerDeps)
Matteo Scandolo352b6012015-11-04 15:07:36 +010097 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandolof813b6a2015-11-03 14:32:00 +010098 .pipe(uglify())
Matteo Scandolo724747c2015-11-06 09:52:01 +010099 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100100 });
101
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +0100102 gulp.task('lint', function () {
103 return gulp.src(['src/js/**/*.js'])
104 .pipe(eslint())
105 .pipe(eslint.format())
106 .pipe(eslint.failAfterError());
107 });
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100108
109 gulp.task('build', function() {
110 runSequence(
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100111 'templates',
Matteo Scandoloac92b722015-11-04 11:46:54 +0100112 'babel',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100113 'scripts',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100114 'wiredep',
Matteo Scandolo724747c2015-11-06 09:52:01 +0100115 'copyHtml',
Matteo Scandoloac92b722015-11-04 11:46:54 +0100116 'cleanTmp'
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100117 );
118 });
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +0100119};