blob: 714f89f12a33ccf7e1a08a117222decb6e121327 [file] [log] [blame]
Matteo Scandolode76a452016-04-12 09:00:04 -07001'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');
12var ngAnnotate = require('gulp-ng-annotate');
13var 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 Scandolode76a452016-04-12 09:00:04 -070017var del = require('del');
18var wiredep = require('wiredep');
19var angularFilesort = require('gulp-angular-filesort');
20var _ = require('lodash');
21var eslint = require('gulp-eslint');
22var inject = require('gulp-inject');
23var rename = require('gulp-rename');
24var replace = require('gulp-replace');
25
Matteo Scandoloc44460c2016-04-13 10:46:08 -070026const TEMPLATE_FOOTER = `
27angular.module('xos.sampleView')
28.run(['$location', function(a){
29 a.path('/');
30}])
31`
32
Matteo Scandolode76a452016-04-12 09:00:04 -070033module.exports = function(options){
34
35 // delete previous builded file
36 gulp.task('clean', function(){
37 return del(
38 [options.dashboards + 'xosSampleView.html'],
39 {force: true}
40 );
41 });
42
43 // 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 Scandoloc44460c2016-04-13 10:46:08 -070076 .pipe(concat('xosSampleView.css'))
Matteo Scandolode76a452016-04-12 09:00:04 -070077 .pipe(gulp.dest(options.static + 'css/'))
78 });
79
80 // compile and minify scripts
81 gulp.task('scripts', function() {
82 return gulp.src([
83 options.tmp + '**/*.js'
84 ])
85 .pipe(ngAnnotate())
86 .pipe(angularFilesort())
87 .pipe(concat('xosSampleView.js'))
Matteo Scandoloc44460c2016-04-13 10:46:08 -070088 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
89 .pipe(concat.footer(TEMPLATE_FOOTER))
Matteo Scandolode76a452016-04-12 09:00:04 -070090 .pipe(uglify())
91 .pipe(gulp.dest(options.static + 'js/'));
92 });
93
94 // set templates in cache
95 gulp.task('templates', function(){
96 return gulp.src('./src/templates/*.html')
97 .pipe(templateCache({
98 module: 'xos.sampleView',
99 root: 'templates/'
100 }))
101 .pipe(gulp.dest(options.tmp));
102 });
103
104 // copy html index to Django Folder
105 gulp.task('copyHtml', ['clean'], function(){
106 return gulp.src(options.src + 'index.html')
107 // 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
111 .pipe(
112 inject(
113 gulp.src([
114 options.static + 'js/vendor/xosSampleViewVendor.js',
115 options.static + 'js/xosSampleView.js'
116 ]),
117 {ignorePath: '/../../../xos/core/xoslib'}
118 )
119 )
120 .pipe(rename('xosSampleView.html'))
121 .pipe(gulp.dest(options.dashboards));
122 });
123
124 // minify vendor js files
125 gulp.task('wiredep', function(){
126 var bowerDeps = wiredep().js;
127 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
136 return gulp.src(bowerDeps)
137 .pipe(concat('xosSampleViewVendor.js'))
138 .pipe(uglify())
139 .pipe(gulp.dest(options.static + 'js/vendor/'));
140 });
141
142 gulp.task('lint', function () {
143 return gulp.src(['src/js/**/*.js'])
144 .pipe(eslint())
145 .pipe(eslint.format())
146 .pipe(eslint.failAfterError());
147 });
148
149 gulp.task('build', function() {
150 runSequence(
151 'templates',
152 'babel',
153 'scripts',
154 'wiredep',
155 'injectCss',
156 'copyHtml',
157 'cleanTmp'
158 );
159 });
160};