blob: b66cdbc7782c467cb18ef1689c12713a21af9d3e [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 Scandoloc44460c2016-04-13 10:46:08 -070016var concat = require('gulp-concat-util');
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 Scandolo3fff3102016-04-13 11:41:31 -070025var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
Matteo Scandolof813b6a2015-11-03 14:32:00 +010029
Matteo Scandoloc44460c2016-04-13 10:46:08 -070030const TEMPLATE_FOOTER = `
31angular.module('xos.<%= name %>')
32.run(['$location', function(a){
33 a.path('/');
34}])
35`
36
Matteo Scandolof813b6a2015-11-03 14:32:00 +010037module.exports = function(options){
38
Matteo Scandoloc7794962015-11-06 10:35:20 +010039 // delete previous builded file
Matteo Scandolof813b6a2015-11-03 14:32:00 +010040 gulp.task('clean', function(){
Matteo Scandoloc7794962015-11-06 10:35:20 +010041 return del(
Matteo Scandolo3fff3102016-04-13 11:41:31 -070042 [
43 options.dashboards + 'xos<%= fileName %>.html',
44 options.static + 'css/xos<%= fileName %>.css'
45 ],
Matteo Scandoloc7794962015-11-06 10:35:20 +010046 {force: true}
47 );
Matteo Scandolof813b6a2015-11-03 14:32:00 +010048 });
49
Matteo Scandoloba1ed912016-03-29 14:23:20 -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
Matteo Scandolo3fff3102016-04-13 11:41:31 -070067 gulp.task('copyCss', ['wait'], function(){
Matteo Scandoloba1ed912016-03-29 14:23:20 -070068 return gulp.src([`${options.tmp}/css/*.css`])
Matteo Scandoloc44460c2016-04-13 10:46:08 -070069 .pipe(concat('xos<%= fileName %>.css'))
Matteo Scandoloba1ed912016-03-29 14:23:20 -070070 .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'))
Matteo Scandoloc44460c2016-04-13 10:46:08 -070081 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
82 .pipe(concat.footer(TEMPLATE_FOOTER))
Matteo Scandolo724747c2015-11-06 09:52:01 +010083 .pipe(uglify())
84 .pipe(gulp.dest(options.static + 'js/'));
Matteo Scandolof813b6a2015-11-03 14:32:00 +010085 });
86
87 // set templates in cache
88 gulp.task('templates', function(){
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +010089 return gulp.src('./src/templates/*.html')
Matteo Scandolof813b6a2015-11-03 14:32:00 +010090 .pipe(templateCache({
Matteo Scandolodc88bf12015-11-03 16:27:07 +010091 module: 'xos.<%= name %>',
Matteo Scandoloba1ed912016-03-29 14:23:20 -070092 root: 'templates/'
Matteo Scandolof813b6a2015-11-03 14:32:00 +010093 }))
Matteo Scandoloac92b722015-11-04 11:46:54 +010094 .pipe(gulp.dest(options.tmp));
Matteo Scandolof813b6a2015-11-03 14:32:00 +010095 });
96
Matteo Scandolodc88bf12015-11-03 16:27:07 +010097 // copy html index to Django Folder
Matteo Scandolo3fff3102016-04-13 11:41:31 -070098 gulp.task('copyHtml', function(){
Matteo Scandoloc7794962015-11-06 10:35:20 +010099 return gulp.src(options.src + 'index.html')
Matteo Scandolo15855792015-11-06 11:16:12 +0100100 // remove dev dependencies from html
Matteo Scandolo6045edc2016-05-10 14:58:47 -0700101 .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, ''))
102 .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, ''))
Matteo Scandolo15855792015-11-06 11:16:12 +0100103 // injecting minified files
Matteo Scandoloc7794962015-11-06 10:35:20 +0100104 .pipe(
105 inject(
106 gulp.src([
107 options.static + 'js/vendor/xos<%= fileName %>Vendor.js',
Matteo Scandolo3fff3102016-04-13 11:41:31 -0700108 options.static + 'js/xos<%= fileName %>.js',
109 options.static + 'css/xos<%= fileName %>.css'
Matteo Scandolo0c523482016-02-02 17:25:18 -0800110 ]),
111 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloc7794962015-11-06 10:35:20 +0100112 )
113 )
114 .pipe(rename('xos<%= fileName %>.html'))
115 .pipe(gulp.dest(options.dashboards));
116 });
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100117
118 // minify vendor js files
119 gulp.task('wiredep', function(){
120 var bowerDeps = wiredep().js;
Matteo Scandolo352b6012015-11-04 15:07:36 +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 Scandolof813b6a2015-11-03 14:32:00 +0100130 return gulp.src(bowerDeps)
Matteo Scandolo352b6012015-11-04 15:07:36 +0100131 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100132 .pipe(uglify())
Matteo Scandolo724747c2015-11-06 09:52:01 +0100133 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100134 });
135
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +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 Scandolof813b6a2015-11-03 14:32:00 +0100142
Matteo Scandolo3fff3102016-04-13 11:41:31 -0700143 gulp.task('wait', function (cb) {
144 // setTimeout could be any async task
145 setTimeout(function () {
146 cb();
147 }, 1000);
148 });
149
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100150 gulp.task('build', function() {
151 runSequence(
Matteo Scandolo3fff3102016-04-13 11:41:31 -0700152 'clean',
Matteo Scandolo9069d592016-05-11 14:53:20 -0700153 'sass',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100154 'templates',
Matteo Scandoloac92b722015-11-04 11:46:54 +0100155 'babel',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100156 'scripts',
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100157 'wiredep',
Matteo Scandolo3fff3102016-04-13 11:41:31 -0700158 'css',
159 'copyCss',
Matteo Scandolo724747c2015-11-06 09:52:01 +0100160 'copyHtml',
Matteo Scandoloac92b722015-11-04 11:46:54 +0100161 'cleanTmp'
Matteo Scandolof813b6a2015-11-03 14:32:00 +0100162 );
163 });
Matteo Scandolo7cf73ad2015-11-05 18:36:10 +0100164};