blob: 238001dec2fa88225a7de9de97f775e361f6bc8a [file] [log] [blame]
Matteo Scandolo608f91d2015-10-29 11:54:54 +01001'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');
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010012var ngAnnotate = require('gulp-ng-annotate');
Matteo Scandolo608f91d2015-10-29 11:54:54 +010013var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
16var minifyHtml = require("gulp-minify-html");
17var concat = require("gulp-concat");
18var del = require('del');
19var wiredep = require('wiredep');
20var babel = require('gulp-babel');
Matteo Scandolo6d972642015-10-29 15:50:15 +010021var angularFilesort = require('gulp-angular-filesort');
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010022var _ = require('lodash');
Matteo Scandolo608f91d2015-10-29 11:54:54 +010023
24module.exports = function(options){
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010025
26 // empty the dist folder
Matteo Scandolo608f91d2015-10-29 11:54:54 +010027 gulp.task('clean', function(){
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010028 return del([options.dist + '**/*']);
Matteo Scandolo608f91d2015-10-29 11:54:54 +010029 });
30
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010031 // compile and minify scripts
Matteo Scandolo608f91d2015-10-29 11:54:54 +010032 gulp.task('scripts', function() {
33 return gulp.src([
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010034 options.tmp + '**/*.js'
Matteo Scandolo608f91d2015-10-29 11:54:54 +010035 ])
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010036 .pipe(ngAnnotate())
Matteo Scandolo6d972642015-10-29 15:50:15 +010037 .pipe(angularFilesort())
Matteo Scandolo608f91d2015-10-29 11:54:54 +010038 .pipe(concat('xosContentProvider.js'))
39 .pipe(uglify())
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010040 .pipe(gulp.dest(options.dist));
Matteo Scandolo608f91d2015-10-29 11:54:54 +010041 });
42
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010043 // set templates in cache
Matteo Scandolo608f91d2015-10-29 11:54:54 +010044 gulp.task('templates', function(){
45 return gulp.src("./src/templates/*.html")
46 .pipe(templateCache({
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010047 module: 'xos.contentProvider',
48 root: 'templates/'
Matteo Scandolo608f91d2015-10-29 11:54:54 +010049 }))
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010050 .pipe(gulp.dest(options.tmp));
Matteo Scandolo608f91d2015-10-29 11:54:54 +010051 });
52
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010053 // copy js output to Django Folder
Matteo Scandolo608f91d2015-10-29 11:54:54 +010054 gulp.task('copyJs', function(){
55 return gulp.src('dist/xosContentProvider.js')
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010056 .pipe(gulp.dest(options.static + 'js/'))
Matteo Scandolo608f91d2015-10-29 11:54:54 +010057 });
58
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010059 // copy vendor js output to Django Folder
Matteo Scandolo608f91d2015-10-29 11:54:54 +010060 gulp.task('copyVendor', function(){
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010061 return gulp.src(options.dist + 'xosContentProviderVendor.js')
62 .pipe(gulp.dest(options.static + 'js/vendor/'));
63 });
64
65 // copy html index to Django Folder
66 gulp.task('copyHtml', function(){
67 return gulp.src(options.src + 'xosContentProvider.html')
68 .pipe(gulp.dest(options.dashboards))
Matteo Scandolo608f91d2015-10-29 11:54:54 +010069 });
70
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010071 // minify vendor js files
Matteo Scandolo608f91d2015-10-29 11:54:54 +010072 gulp.task('wiredep', function(){
73 var bowerDeps = wiredep().js;
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010074 if(!bowerDeps){
75 return;
76 }
77
78 // remove angular (it's already loaded)
79 _.remove(bowerDeps, function(dep){
80 return dep.indexOf('angular/angular.js') !== -1;
81 });
82
Matteo Scandolo608f91d2015-10-29 11:54:54 +010083 return gulp.src(bowerDeps)
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010084 .pipe(concat('xosContentProviderVendor.js'))
Matteo Scandolo608f91d2015-10-29 11:54:54 +010085 .pipe(uglify())
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010086 .pipe(gulp.dest(options.dist));
Matteo Scandolo608f91d2015-10-29 11:54:54 +010087 });
88
Matteo Scandoloc2d0f532015-10-29 16:13:35 +010089 // TODO vendor
90 // - define a list of common components (eg: angular, angular-route, ...)
91 // - find the difference between local components e common components
92 // - minify only the local
93 // - unify wiredep, filter and copyVendor task
94
Matteo Scandolo608f91d2015-10-29 11:54:54 +010095 gulp.task('build', function() {
96 runSequence(
97 'clean',
98 'templates',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010099 'babel',
Matteo Scandolo608f91d2015-10-29 11:54:54 +0100100 'scripts',
101 'copyJs',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100102 'copyHtml',
Matteo Scandolo608f91d2015-10-29 11:54:54 +0100103 'wiredep',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100104 'copyVendor',
105 'cleanTmp'
Matteo Scandolo608f91d2015-10-29 11:54:54 +0100106 );
107 });
108}