blob: bd31a256112d5e0434906392a63f0206161682e0 [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 Scandolof813b6a2015-11-03 14:32:00 +010026module.exports = function(options){
27
Matteo Scandoloc7794962015-11-06 10:35:20 +010028 // delete previous builded file
Matteo Scandolof813b6a2015-11-03 14:32:00 +010029 gulp.task('clean', function(){
Matteo Scandoloc7794962015-11-06 10:35:20 +010030 return del(
31 [options.dashboards + 'xos<%= fileName %>.html'],
32 {force: true}
33 );
Matteo Scandolof813b6a2015-11-03 14:32:00 +010034 });
35
Matteo Scandoloba1ed912016-03-29 14:23:20 -070036 // inject CSS
37 gulp.task('injectCss', function(){
38 return gulp.src(options.src + 'index.html')
39 .pipe(
40 inject(
41 gulp.src(options.src + 'css/*.css'),
42 {
43 ignorePath: [options.src]
44 }
45 )
46 )
47 .pipe(gulp.dest(options.src));
48 });
49
50 // 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', ['css'], function(){
68 return gulp.src([`${options.tmp}/css/*.css`])
69 .pipe(concat('xosDiagnostic.css'))
70 .pipe(gulp.dest(options.static + 'css/'))
71 });
72
Matteo Scandolof813b6a2015-11-03 14:32:00 +010073 // compile and minify scripts
74 gulp.task('scripts', function() {
75 return gulp.src([
Matteo Scandolo724747c2015-11-06 09:52:01 +010076 options.tmp + '**/*.js'
77 ])
78 .pipe(ngAnnotate())
79 .pipe(angularFilesort())
80 .pipe(concat('xos<%= fileName %>.js'))
81 .pipe(uglify())
82 .pipe(gulp.dest(options.static + 'js/'));
Matteo Scandolof813b6a2015-11-03 14:32:00 +010083 });
84
85 // set templates in cache
86 gulp.task('templates', function(){
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +010087 return gulp.src('./src/templates/*.html')
Matteo Scandolof813b6a2015-11-03 14:32:00 +010088 .pipe(templateCache({
Matteo Scandolodc88bf12015-11-03 16:27:07 +010089 module: 'xos.<%= name %>',
Matteo Scandoloba1ed912016-03-29 14:23:20 -070090 root: 'templates/'
Matteo Scandolof813b6a2015-11-03 14:32:00 +010091 }))
Matteo Scandoloac92b722015-11-04 11:46:54 +010092 .pipe(gulp.dest(options.tmp));
Matteo Scandolof813b6a2015-11-03 14:32:00 +010093 });
94
Matteo Scandolodc88bf12015-11-03 16:27:07 +010095 // copy html index to Django Folder
Matteo Scandoloc7794962015-11-06 10:35:20 +010096 gulp.task('copyHtml', ['clean'], function(){
97 return gulp.src(options.src + 'index.html')
Matteo Scandolo15855792015-11-06 11:16:12 +010098 // remove dev dependencies from html
99 .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
100 .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
101 // injecting minified files
Matteo Scandoloc7794962015-11-06 10:35:20 +0100102 .pipe(
103 inject(
104 gulp.src([
105 options.static + 'js/vendor/xos<%= fileName %>Vendor.js',
106 options.static + 'js/xos<%= fileName %>.js'
Matteo Scandolo0c523482016-02-02 17:25:18 -0800107 ]),
108 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloc7794962015-11-06 10:35:20 +0100109 )
110 )
111 .pipe(rename('xos<%= fileName %>.html'))
112 .pipe(gulp.dest(options.dashboards));
113 });
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100114
115 // minify vendor js files
116 gulp.task('wiredep', function(){
117 var bowerDeps = wiredep().js;
Matteo Scandolo352b6012015-11-04 15:07:36 +0100118 if(!bowerDeps){
119 return;
120 }
121
122 // remove angular (it's already loaded)
123 _.remove(bowerDeps, function(dep){
124 return dep.indexOf('angular/angular.js') !== -1;
125 });
126
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100127 return gulp.src(bowerDeps)
Matteo Scandolo352b6012015-11-04 15:07:36 +0100128 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100129 .pipe(uglify())
Matteo Scandolo724747c2015-11-06 09:52:01 +0100130 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100131 });
132
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +0100133 gulp.task('lint', function () {
134 return gulp.src(['src/js/**/*.js'])
135 .pipe(eslint())
136 .pipe(eslint.format())
137 .pipe(eslint.failAfterError());
138 });
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100139
140 gulp.task('build', function() {
141 runSequence(
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100142 'templates',
Matteo Scandoloac92b722015-11-04 11:46:54 +0100143 'babel',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100144 'scripts',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100145 'wiredep',
Matteo Scandoloba1ed912016-03-29 14:23:20 -0700146 'injectCss',
Matteo Scandolo724747c2015-11-06 09:52:01 +0100147 'copyHtml',
Matteo Scandoloac92b722015-11-04 11:46:54 +0100148 'cleanTmp'
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100149 );
150 });
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +0100151};