blob: 6bef382c3d4001e8c7df63b0e0670e18b9075e19 [file] [log] [blame]
Matteo Scandolod90e19f2015-10-29 11:54:54 +01001'use strict';
2
3// BUILD
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +01004//
Matteo Scandolod90e19f2015-10-29 11:54:54 +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 Scandoloc5ee7b52015-11-06 17:14:28 +01008//
Matteo Scandolod90e19f2015-10-29 11:54:54 +01009// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
Matteo Scandolo82697c02015-11-04 16:30:43 +010012var ngAnnotate = require('gulp-ng-annotate');
Matteo Scandolod90e19f2015-10-29 11:54:54 +010013var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
Matteo Scandolo57fd1152016-05-17 17:12:03 -070016var concat = require('gulp-concat-util');
Matteo Scandolod90e19f2015-10-29 11:54:54 +010017var del = require('del');
18var wiredep = require('wiredep');
Matteo Scandolocb11e0c2015-10-29 15:50:15 +010019var angularFilesort = require('gulp-angular-filesort');
Matteo Scandolo82697c02015-11-04 16:30:43 +010020var _ = require('lodash');
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010021var eslint = require('gulp-eslint');
22var inject = require('gulp-inject');
23var rename = require('gulp-rename');
24var replace = require('gulp-replace');
Matteo Scandolo57fd1152016-05-17 17:12:03 -070025var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
Matteo Scandolod90e19f2015-10-29 11:54:54 +010029
Matteo Scandolo57fd1152016-05-17 17:12:03 -070030const TEMPLATE_FOOTER = `
31angular.module('xos.contentProvider')
32.run(['$location', function(a){
33 a.path('/');
34}])
35`
Matteo Scandolo2d2484f2015-11-10 17:13:04 +010036
Matteo Scandolod90e19f2015-10-29 11:54:54 +010037module.exports = function(options){
Matteo Scandolo2729d082015-10-29 16:13:35 +010038
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010039 // delete previous builded file
Matteo Scandolod90e19f2015-10-29 11:54:54 +010040 gulp.task('clean', function(){
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010041 return del(
Matteo Scandolo57fd1152016-05-17 17:12:03 -070042 [
43 options.dashboards + 'xosContentProvider.html',
44 options.static + 'css/xosContentProvider.css'
45 ],
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010046 {force: true}
47 );
Matteo Scandolod90e19f2015-10-29 11:54:54 +010048 });
49
Matteo Scandolo57fd1152016-05-17 17:12:03 -070050 // minify css
51 gulp.task('css', function () {
52 var processors = [
53 autoprefixer({browsers: ['last 1 version']}),
54 mqpacker,
55 csswring
56 ];
57
58 gulp.src([
59 `${options.css}**/*.css`,
60 `!${options.css}dev.css`
61 ])
62 .pipe(postcss(processors))
63 .pipe(gulp.dest(options.tmp + '/css/'));
64 });
65
66 // copy css in correct folder
67 gulp.task('copyCss', ['wait'], function(){
68 return gulp.src([`${options.tmp}/css/*.css`])
69 .pipe(concat('xosContentProvider.css'))
70 .pipe(gulp.dest(options.static + 'css/'))
71 });
72
Matteo Scandolo2729d082015-10-29 16:13:35 +010073 // compile and minify scripts
Matteo Scandolod90e19f2015-10-29 11:54:54 +010074 gulp.task('scripts', function() {
75 return gulp.src([
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010076 options.tmp + '**/*.js'
77 ])
78 .pipe(ngAnnotate())
79 .pipe(angularFilesort())
80 .pipe(concat('xosContentProvider.js'))
Matteo Scandolo57fd1152016-05-17 17:12:03 -070081 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
82 .pipe(concat.footer(TEMPLATE_FOOTER))
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010083 .pipe(uglify())
84 .pipe(gulp.dest(options.static + 'js/'));
Matteo Scandolod90e19f2015-10-29 11:54:54 +010085 });
86
Matteo Scandolo2729d082015-10-29 16:13:35 +010087 // set templates in cache
Matteo Scandolod90e19f2015-10-29 11:54:54 +010088 gulp.task('templates', function(){
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010089 return gulp.src('./src/templates/*.html')
Matteo Scandolod90e19f2015-10-29 11:54:54 +010090 .pipe(templateCache({
Matteo Scandolo82697c02015-11-04 16:30:43 +010091 module: 'xos.contentProvider',
Matteo Scandolo57fd1152016-05-17 17:12:03 -070092 root: 'templates/'
Matteo Scandolod90e19f2015-10-29 11:54:54 +010093 }))
Matteo Scandolo82697c02015-11-04 16:30:43 +010094 .pipe(gulp.dest(options.tmp));
Matteo Scandolod90e19f2015-10-29 11:54:54 +010095 });
96
Matteo Scandolo82697c02015-11-04 16:30:43 +010097 // copy html index to Django Folder
Matteo Scandolo57fd1152016-05-17 17:12:03 -070098 gulp.task('copyHtml', function(){
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +010099 return gulp.src(options.src + 'index.html')
100 // remove dev dependencies from html
Matteo Scandolo57fd1152016-05-17 17:12:03 -0700101 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
102 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100103 // injecting minified files
104 .pipe(
105 inject(
106 gulp.src([
107 options.static + 'js/vendor/xosContentProviderVendor.js',
Matteo Scandolo57fd1152016-05-17 17:12:03 -0700108 options.static + 'js/xosContentProvider.js',
109 options.static + 'css/xosContentProvider.css'
Matteo Scandolo0c523482016-02-02 17:25:18 -0800110 ]),
111 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100112 )
113 )
114 .pipe(rename('xosContentProvider.html'))
115 .pipe(gulp.dest(options.dashboards));
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100116 });
117
Matteo Scandolo2729d082015-10-29 16:13:35 +0100118 // minify vendor js files
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100119 gulp.task('wiredep', function(){
120 var bowerDeps = wiredep().js;
Matteo Scandolo82697c02015-11-04 16:30:43 +0100121 if(!bowerDeps){
122 return;
123 }
124
125 // remove angular (it's already loaded)
126 _.remove(bowerDeps, function(dep){
127 return dep.indexOf('angular/angular.js') !== -1;
128 });
129
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100130 return gulp.src(bowerDeps)
Matteo Scandolo82697c02015-11-04 16:30:43 +0100131 .pipe(concat('xosContentProviderVendor.js'))
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100132 .pipe(uglify())
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100133 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100134 });
135
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100136 gulp.task('lint', function () {
137 return gulp.src(['src/js/**/*.js'])
138 .pipe(eslint())
139 .pipe(eslint.format())
140 .pipe(eslint.failAfterError());
141 });
Matteo Scandolo2729d082015-10-29 16:13:35 +0100142
Matteo Scandolo57fd1152016-05-17 17:12:03 -0700143 gulp.task('wait', function (cb) {
144 // setTimeout could be any async task
145 setTimeout(function () {
146 cb();
147 }, 1000);
148 });
149
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100150 gulp.task('build', function() {
151 runSequence(
Matteo Scandolo57fd1152016-05-17 17:12:03 -0700152 'clean',
153 'sass',
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100154 'templates',
Matteo Scandolo82697c02015-11-04 16:30:43 +0100155 'babel',
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100156 'scripts',
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100157 'wiredep',
Matteo Scandolo57fd1152016-05-17 17:12:03 -0700158 'css',
159 'copyCss',
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100160 'copyHtml',
Matteo Scandolo82697c02015-11-04 16:30:43 +0100161 'cleanTmp'
Matteo Scandolod90e19f2015-10-29 11:54:54 +0100162 );
163 });
Matteo Scandoloc5ee7b52015-11-06 17:14:28 +0100164};