blob: 2861020d52bf13c887d93585a1f7ec7388478caf [file] [log] [blame]
Matteo Scandoloe53ee052015-11-03 14:32:00 +01001'use strict';
2
3// BUILD
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +01004//
Matteo Scandoloe53ee052015-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 Scandoloe3bc18d2015-11-05 18:36:10 +01008//
Matteo Scandoloe53ee052015-11-03 14:32:00 +01009// The template are parsed and added to js with angular $templateCache
10
11var gulp = require('gulp');
Matteo Scandolod838fac2015-11-04 16:10:09 +010012var ngAnnotate = require('gulp-ng-annotate');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010013var uglify = require('gulp-uglify');
14var templateCache = require('gulp-angular-templatecache');
15var runSequence = require('run-sequence');
Matteo Scandolodd55c132016-04-13 10:46:08 -070016var concat = require('gulp-concat-util');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010017var del = require('del');
18var wiredep = require('wiredep');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010019var angularFilesort = require('gulp-angular-filesort');
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +010020var _ = require('lodash');
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010021var eslint = require('gulp-eslint');
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010022var inject = require('gulp-inject');
23var rename = require('gulp-rename');
Matteo Scandolo61a714d2015-11-06 11:16:12 +010024var replace = require('gulp-replace');
Matteo Scandoloe53ee052015-11-03 14:32:00 +010025
Matteo Scandolodd55c132016-04-13 10:46:08 -070026const TEMPLATE_FOOTER = `
27angular.module('xos.<%= name %>')
28.run(['$location', function(a){
29 a.path('/');
30}])
31`
32
Matteo Scandoloe53ee052015-11-03 14:32:00 +010033module.exports = function(options){
34
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010035 // delete previous builded file
Matteo Scandoloe53ee052015-11-03 14:32:00 +010036 gulp.task('clean', function(){
Matteo Scandoloba2af3d2015-11-06 10:35:20 +010037 return del(
38 [options.dashboards + 'xos<%= fileName %>.html'],
39 {force: true}
40 );
Matteo Scandoloe53ee052015-11-03 14:32:00 +010041 });
42
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070043 // inject CSS
44 gulp.task('injectCss', function(){
45 return gulp.src(options.src + 'index.html')
46 .pipe(
47 inject(
48 gulp.src(options.src + 'css/*.css'),
49 {
50 ignorePath: [options.src]
51 }
52 )
53 )
54 .pipe(gulp.dest(options.src));
55 });
56
57 // minify css
58 gulp.task('css', function () {
59 var processors = [
60 autoprefixer({browsers: ['last 1 version']}),
61 mqpacker,
62 csswring
63 ];
64
65 gulp.src([
66 `${options.css}**/*.css`,
67 `!${options.css}dev.css`
68 ])
69 .pipe(postcss(processors))
70 .pipe(gulp.dest(options.tmp + '/css/'));
71 });
72
73 // copy css in correct folder
74 gulp.task('copyCss', ['css'], function(){
75 return gulp.src([`${options.tmp}/css/*.css`])
Matteo Scandolodd55c132016-04-13 10:46:08 -070076 .pipe(concat('xos<%= fileName %>.css'))
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070077 .pipe(gulp.dest(options.static + 'css/'))
78 });
79
Matteo Scandoloe53ee052015-11-03 14:32:00 +010080 // compile and minify scripts
81 gulp.task('scripts', function() {
82 return gulp.src([
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010083 options.tmp + '**/*.js'
84 ])
85 .pipe(ngAnnotate())
86 .pipe(angularFilesort())
87 .pipe(concat('xos<%= fileName %>.js'))
Matteo Scandolodd55c132016-04-13 10:46:08 -070088 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
89 .pipe(concat.footer(TEMPLATE_FOOTER))
Matteo Scandolo8a1f2362015-11-06 09:52:01 +010090 .pipe(uglify())
91 .pipe(gulp.dest(options.static + 'js/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +010092 });
93
94 // set templates in cache
95 gulp.task('templates', function(){
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +010096 return gulp.src('./src/templates/*.html')
Matteo Scandoloe53ee052015-11-03 14:32:00 +010097 .pipe(templateCache({
Matteo Scandolo6be0cd22015-11-03 16:27:07 +010098 module: 'xos.<%= name %>',
Matteo Scandolob7c91cf2016-03-29 14:23:20 -070099 root: 'templates/'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100100 }))
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100101 .pipe(gulp.dest(options.tmp));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100102 });
103
Matteo Scandolo6be0cd22015-11-03 16:27:07 +0100104 // copy html index to Django Folder
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100105 gulp.task('copyHtml', ['clean'], function(){
106 return gulp.src(options.src + 'index.html')
Matteo Scandolo61a714d2015-11-06 11:16:12 +0100107 // remove dev dependencies from html
108 .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
109 .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
110 // injecting minified files
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100111 .pipe(
112 inject(
113 gulp.src([
114 options.static + 'js/vendor/xos<%= fileName %>Vendor.js',
115 options.static + 'js/xos<%= fileName %>.js'
Matteo Scandoloc0c94092016-02-02 17:25:18 -0800116 ]),
117 {ignorePath: '/../../../xos/core/xoslib'}
Matteo Scandoloba2af3d2015-11-06 10:35:20 +0100118 )
119 )
120 .pipe(rename('xos<%= fileName %>.html'))
121 .pipe(gulp.dest(options.dashboards));
122 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100123
124 // minify vendor js files
125 gulp.task('wiredep', function(){
126 var bowerDeps = wiredep().js;
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100127 if(!bowerDeps){
128 return;
129 }
130
131 // remove angular (it's already loaded)
132 _.remove(bowerDeps, function(dep){
133 return dep.indexOf('angular/angular.js') !== -1;
134 });
135
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100136 return gulp.src(bowerDeps)
Matteo Scandolocb7ea1e2015-11-04 15:07:36 +0100137 .pipe(concat('xos<%= fileName %>Vendor.js'))
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100138 .pipe(uglify())
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100139 .pipe(gulp.dest(options.static + 'js/vendor/'));
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100140 });
141
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100142 gulp.task('lint', function () {
143 return gulp.src(['src/js/**/*.js'])
144 .pipe(eslint())
145 .pipe(eslint.format())
146 .pipe(eslint.failAfterError());
147 });
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100148
149 gulp.task('build', function() {
150 runSequence(
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100151 'templates',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100152 'babel',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100153 'scripts',
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100154 'wiredep',
Matteo Scandolob7c91cf2016-03-29 14:23:20 -0700155 'injectCss',
Matteo Scandolo8a1f2362015-11-06 09:52:01 +0100156 'copyHtml',
Matteo Scandoloce1dd312015-11-04 11:46:54 +0100157 'cleanTmp'
Matteo Scandoloe53ee052015-11-03 14:32:00 +0100158 );
159 });
Matteo Scandoloe3bc18d2015-11-05 18:36:10 +0100160};