blob: f892310a85aa51c24de5762de8844eb740b6ff67 [file] [log] [blame]
Matteo Scandolo74802522016-02-25 09:19:41 -08001'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 Scandoloc0c6e062016-02-25 09:38:59 -080016var concat = require('gulp-concat-util');
Matteo Scandolo74802522016-02-25 09:19:41 -080017var 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');
Matteo Scandoloc0c6e062016-02-25 09:38:59 -080025var postcss = require('gulp-postcss');
26var autoprefixer = require('autoprefixer');
27var mqpacker = require('css-mqpacker');
28var csswring = require('csswring');
Matteo Scandolo74802522016-02-25 09:19:41 -080029
Matteo Scandoloc0c6e062016-02-25 09:38:59 -080030var TEMPLATE_FOOTER = `
Matteo Scandolo74802522016-02-25 09:19:41 -080031angular.module('xos.mcordTopology').run(function($location){$location.path('/')});
32angular.bootstrap(angular.element('#xosMcordTopology'), ['xos.mcordTopology']);`;
33
34module.exports = function(options){
35
36 // delete previous builded file
37 gulp.task('clean', function(){
38 return del(
39 [options.dashboards + 'xosMcordTopology.html'],
40 {force: true}
41 );
42 });
43
Matteo Scandoloc0c6e062016-02-25 09:38:59 -080044 // minify css
45 gulp.task('css', function () {
46 var processors = [
47 autoprefixer({browsers: ['last 1 version']}),
48 mqpacker,
49 csswring
50 ];
51 gulp.src([
52 `${options.css}**/*.css`,
53 `!${options.css}dev.css`
54 ])
55 .pipe(postcss(processors))
56 .pipe(gulp.dest(options.tmp + '/css/'));
57 });
58
59 gulp.task('copyCss', ['css'], function(){
60 return gulp.src([`${options.tmp}/css/*.css`])
61 .pipe(concat('xosMcordTopology.css'))
62 .pipe(gulp.dest(options.static + 'css/'))
63 });
64
Matteo Scandolo74802522016-02-25 09:19:41 -080065 // compile and minify scripts
66 gulp.task('scripts', function() {
67 return gulp.src([
68 options.tmp + '**/*.js'
69 ])
70 .pipe(ngAnnotate())
71 .pipe(angularFilesort())
72 .pipe(concat('xosMcordTopology.js'))
Matteo Scandoloc0c6e062016-02-25 09:38:59 -080073 .pipe(concat.header('//Autogenerated, do not edit!!!\n'))
74 .pipe(concat.footer(TEMPLATE_FOOTER))
75 // .pipe(uglify())
Matteo Scandolo74802522016-02-25 09:19:41 -080076 .pipe(gulp.dest(options.static + 'js/'));
77 });
78
79 // set templates in cache
80 gulp.task('templates', function(){
81 return gulp.src('./src/templates/*.html')
82 .pipe(templateCache({
83 module: 'xos.mcordTopology',
84 root: 'templates/',
85 templateFooter: TEMPLATE_FOOTER
86 }))
87 .pipe(gulp.dest(options.tmp));
88 });
89
90 // copy html index to Django Folder
91 gulp.task('copyHtml', ['clean'], function(){
92 return gulp.src(options.src + 'index.html')
93 // remove dev dependencies from html
94 .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, ''))
95 .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, ''))
96 .pipe(replace(/ng-app=".*"\s/, ''))
97 // injecting minified files
98 .pipe(
99 inject(
100 gulp.src([
101 options.static + 'js/vendor/xosMcordTopologyVendor.js',
Matteo Scandoloc0c6e062016-02-25 09:38:59 -0800102 options.static + 'js/xosMcordTopology.js',
103 options.static + 'css/xosMcordTopology.css'
Matteo Scandolo74802522016-02-25 09:19:41 -0800104 ]),
105 {ignorePath: '/../../../xos/core/xoslib'}
106 )
107 )
108 .pipe(rename('xosMcordTopology.html'))
109 .pipe(gulp.dest(options.dashboards));
110 });
111
112 // minify vendor js files
113 gulp.task('wiredep', function(){
114 var bowerDeps = wiredep().js;
115 if(!bowerDeps){
116 return;
117 }
118
119 // remove angular (it's already loaded)
120 _.remove(bowerDeps, function(dep){
121 return dep.indexOf('angular/angular.js') !== -1;
122 });
123
124 return gulp.src(bowerDeps)
125 .pipe(concat('xosMcordTopologyVendor.js'))
126 .pipe(uglify())
127 .pipe(gulp.dest(options.static + 'js/vendor/'));
128 });
129
130 gulp.task('lint', function () {
131 return gulp.src(['src/js/**/*.js'])
132 .pipe(eslint())
133 .pipe(eslint.format())
134 .pipe(eslint.failAfterError());
135 });
136
137 gulp.task('build', function() {
138 runSequence(
139 'templates',
140 'babel',
141 'scripts',
142 'wiredep',
143 'copyHtml',
Matteo Scandoloc0c6e062016-02-25 09:38:59 -0800144 'copyCss'
Matteo Scandolo74802522016-02-25 09:19:41 -0800145 );
146 });
147};