Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | // BUILD |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 4 | // |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 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 |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 8 | // |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 9 | // The template are parsed and added to js with angular $templateCache |
| 10 | |
| 11 | var gulp = require('gulp'); |
Matteo Scandolo | d838fac | 2015-11-04 16:10:09 +0100 | [diff] [blame] | 12 | var ngAnnotate = require('gulp-ng-annotate'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 13 | var uglify = require('gulp-uglify'); |
| 14 | var templateCache = require('gulp-angular-templatecache'); |
| 15 | var runSequence = require('run-sequence'); |
Matteo Scandolo | dd55c13 | 2016-04-13 10:46:08 -0700 | [diff] [blame] | 16 | var concat = require('gulp-concat-util'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 17 | var del = require('del'); |
| 18 | var wiredep = require('wiredep'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 19 | var angularFilesort = require('gulp-angular-filesort'); |
Matteo Scandolo | cb7ea1e | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 20 | var _ = require('lodash'); |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 21 | var eslint = require('gulp-eslint'); |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 22 | var inject = require('gulp-inject'); |
| 23 | var rename = require('gulp-rename'); |
Matteo Scandolo | 61a714d | 2015-11-06 11:16:12 +0100 | [diff] [blame] | 24 | var replace = require('gulp-replace'); |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 25 | var postcss = require('gulp-postcss'); |
| 26 | var autoprefixer = require('autoprefixer'); |
| 27 | var mqpacker = require('css-mqpacker'); |
| 28 | var csswring = require('csswring'); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 29 | |
Matteo Scandolo | dd55c13 | 2016-04-13 10:46:08 -0700 | [diff] [blame] | 30 | const TEMPLATE_FOOTER = ` |
| 31 | angular.module('xos.<%= name %>') |
| 32 | .run(['$location', function(a){ |
| 33 | a.path('/'); |
| 34 | }]) |
| 35 | ` |
| 36 | |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 37 | module.exports = function(options){ |
| 38 | |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 39 | // delete previous builded file |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 40 | gulp.task('clean', function(){ |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 41 | return del( |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 42 | [ |
| 43 | options.dashboards + 'xos<%= fileName %>.html', |
| 44 | options.static + 'css/xos<%= fileName %>.css' |
| 45 | ], |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 46 | {force: true} |
| 47 | ); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 48 | }); |
| 49 | |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 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 |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 67 | gulp.task('copyCss', ['wait'], function(){ |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 68 | return gulp.src([`${options.tmp}/css/*.css`]) |
Matteo Scandolo | dd55c13 | 2016-04-13 10:46:08 -0700 | [diff] [blame] | 69 | .pipe(concat('xos<%= fileName %>.css')) |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 70 | .pipe(gulp.dest(options.static + 'css/')) |
| 71 | }); |
| 72 | |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 73 | // compile and minify scripts |
| 74 | gulp.task('scripts', function() { |
| 75 | return gulp.src([ |
Matteo Scandolo | 8a1f236 | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 76 | options.tmp + '**/*.js' |
| 77 | ]) |
| 78 | .pipe(ngAnnotate()) |
| 79 | .pipe(angularFilesort()) |
| 80 | .pipe(concat('xos<%= fileName %>.js')) |
Matteo Scandolo | dd55c13 | 2016-04-13 10:46:08 -0700 | [diff] [blame] | 81 | .pipe(concat.header('//Autogenerated, do not edit!!!\n')) |
| 82 | .pipe(concat.footer(TEMPLATE_FOOTER)) |
Matteo Scandolo | 8a1f236 | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 83 | .pipe(uglify()) |
| 84 | .pipe(gulp.dest(options.static + 'js/')); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 85 | }); |
| 86 | |
| 87 | // set templates in cache |
| 88 | gulp.task('templates', function(){ |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 89 | return gulp.src('./src/templates/*.html') |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 90 | .pipe(templateCache({ |
Matteo Scandolo | 6be0cd2 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 91 | module: 'xos.<%= name %>', |
Matteo Scandolo | b7c91cf | 2016-03-29 14:23:20 -0700 | [diff] [blame] | 92 | root: 'templates/' |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 93 | })) |
Matteo Scandolo | ce1dd31 | 2015-11-04 11:46:54 +0100 | [diff] [blame] | 94 | .pipe(gulp.dest(options.tmp)); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 95 | }); |
| 96 | |
Matteo Scandolo | 6be0cd2 | 2015-11-03 16:27:07 +0100 | [diff] [blame] | 97 | // copy html index to Django Folder |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 98 | gulp.task('copyHtml', function(){ |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 99 | return gulp.src(options.src + 'index.html') |
Matteo Scandolo | 61a714d | 2015-11-06 11:16:12 +0100 | [diff] [blame] | 100 | // remove dev dependencies from html |
Matteo Scandolo | 34d3596 | 2016-05-10 14:58:47 -0700 | [diff] [blame] | 101 | .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, '')) |
| 102 | .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, '')) |
Matteo Scandolo | 61a714d | 2015-11-06 11:16:12 +0100 | [diff] [blame] | 103 | // injecting minified files |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 104 | .pipe( |
| 105 | inject( |
| 106 | gulp.src([ |
| 107 | options.static + 'js/vendor/xos<%= fileName %>Vendor.js', |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 108 | options.static + 'js/xos<%= fileName %>.js', |
| 109 | options.static + 'css/xos<%= fileName %>.css' |
Matteo Scandolo | c0c9409 | 2016-02-02 17:25:18 -0800 | [diff] [blame] | 110 | ]), |
| 111 | {ignorePath: '/../../../xos/core/xoslib'} |
Matteo Scandolo | ba2af3d | 2015-11-06 10:35:20 +0100 | [diff] [blame] | 112 | ) |
| 113 | ) |
| 114 | .pipe(rename('xos<%= fileName %>.html')) |
| 115 | .pipe(gulp.dest(options.dashboards)); |
| 116 | }); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 117 | |
| 118 | // minify vendor js files |
| 119 | gulp.task('wiredep', function(){ |
| 120 | var bowerDeps = wiredep().js; |
Matteo Scandolo | cb7ea1e | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 121 | 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 Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 130 | return gulp.src(bowerDeps) |
Matteo Scandolo | cb7ea1e | 2015-11-04 15:07:36 +0100 | [diff] [blame] | 131 | .pipe(concat('xos<%= fileName %>Vendor.js')) |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 132 | .pipe(uglify()) |
Matteo Scandolo | 8a1f236 | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 133 | .pipe(gulp.dest(options.static + 'js/vendor/')); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 134 | }); |
| 135 | |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 136 | gulp.task('lint', function () { |
| 137 | return gulp.src(['src/js/**/*.js']) |
| 138 | .pipe(eslint()) |
| 139 | .pipe(eslint.format()) |
| 140 | .pipe(eslint.failAfterError()); |
| 141 | }); |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 142 | |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 143 | gulp.task('wait', function (cb) { |
| 144 | // setTimeout could be any async task |
| 145 | setTimeout(function () { |
| 146 | cb(); |
| 147 | }, 1000); |
| 148 | }); |
| 149 | |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 150 | gulp.task('build', function() { |
| 151 | runSequence( |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 152 | 'clean', |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 153 | 'sass', |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 154 | 'templates', |
Matteo Scandolo | ce1dd31 | 2015-11-04 11:46:54 +0100 | [diff] [blame] | 155 | 'babel', |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 156 | 'scripts', |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 157 | 'wiredep', |
Matteo Scandolo | 7ae7d8d | 2016-04-13 11:41:31 -0700 | [diff] [blame] | 158 | 'css', |
| 159 | 'copyCss', |
Matteo Scandolo | 8a1f236 | 2015-11-06 09:52:01 +0100 | [diff] [blame] | 160 | 'copyHtml', |
Matteo Scandolo | ce1dd31 | 2015-11-04 11:46:54 +0100 | [diff] [blame] | 161 | 'cleanTmp' |
Matteo Scandolo | e53ee05 | 2015-11-03 14:32:00 +0100 | [diff] [blame] | 162 | ); |
| 163 | }); |
Matteo Scandolo | e3bc18d | 2015-11-05 18:36:10 +0100 | [diff] [blame] | 164 | }; |