Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | // BUILD |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 4 | // |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +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 | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 8 | // |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 9 | // The template are parsed and added to js with angular $templateCache |
| 10 | |
| 11 | var gulp = require('gulp'); |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 12 | var ngAnnotate = require('gulp-ng-annotate'); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 13 | var uglify = require('gulp-uglify'); |
| 14 | var templateCache = require('gulp-angular-templatecache'); |
| 15 | var runSequence = require('run-sequence'); |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 16 | var concat = require('gulp-concat-util'); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 17 | var del = require('del'); |
| 18 | var wiredep = require('wiredep'); |
Matteo Scandolo | 6d97264 | 2015-10-29 15:50:15 +0100 | [diff] [blame] | 19 | var angularFilesort = require('gulp-angular-filesort'); |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 20 | var _ = require('lodash'); |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 21 | var eslint = require('gulp-eslint'); |
| 22 | var inject = require('gulp-inject'); |
| 23 | var rename = require('gulp-rename'); |
| 24 | var replace = require('gulp-replace'); |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -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 | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 29 | |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 30 | const TEMPLATE_FOOTER = ` |
| 31 | angular.module('xos.contentProvider') |
| 32 | .run(['$location', function(a){ |
| 33 | a.path('/'); |
| 34 | }]) |
| 35 | ` |
Matteo Scandolo | 9a60726 | 2015-11-10 17:13:04 +0100 | [diff] [blame] | 36 | |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 37 | module.exports = function(options){ |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 38 | |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 39 | // delete previous builded file |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 40 | gulp.task('clean', function(){ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 41 | return del( |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 42 | [ |
| 43 | options.dashboards + 'xosContentProvider.html', |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 44 | options.static + 'css/xosContentProvider.css', |
| 45 | options.static + 'images/contentProvider-icon.png', |
| 46 | options.static + 'images/contentProvider-icon-active.png' |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 47 | ], |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 48 | {force: true} |
| 49 | ); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 50 | }); |
| 51 | |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 52 | // minify css |
| 53 | gulp.task('css', function () { |
| 54 | var processors = [ |
| 55 | autoprefixer({browsers: ['last 1 version']}), |
| 56 | mqpacker, |
| 57 | csswring |
| 58 | ]; |
| 59 | |
| 60 | gulp.src([ |
| 61 | `${options.css}**/*.css`, |
| 62 | `!${options.css}dev.css` |
| 63 | ]) |
| 64 | .pipe(postcss(processors)) |
| 65 | .pipe(gulp.dest(options.tmp + '/css/')); |
| 66 | }); |
| 67 | |
| 68 | // copy css in correct folder |
| 69 | gulp.task('copyCss', ['wait'], function(){ |
| 70 | return gulp.src([`${options.tmp}/css/*.css`]) |
| 71 | .pipe(concat('xosContentProvider.css')) |
| 72 | .pipe(gulp.dest(options.static + 'css/')) |
| 73 | }); |
| 74 | |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 75 | // copy images in correct folder |
| 76 | gulp.task('copyImages', ['wait'], function(){ |
| 77 | return gulp.src([`${options.icon}/contentProvider-icon.png`,`${options.icon}/contentProvider-icon-active.png`]) |
| 78 | .pipe(gulp.dest(options.static + 'images/')) |
| 79 | }); |
| 80 | |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 81 | // compile and minify scripts |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 82 | gulp.task('scripts', function() { |
| 83 | return gulp.src([ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 84 | options.tmp + '**/*.js' |
| 85 | ]) |
| 86 | .pipe(ngAnnotate()) |
| 87 | .pipe(angularFilesort()) |
| 88 | .pipe(concat('xosContentProvider.js')) |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 89 | .pipe(concat.header('//Autogenerated, do not edit!!!\n')) |
| 90 | .pipe(concat.footer(TEMPLATE_FOOTER)) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 91 | .pipe(uglify()) |
| 92 | .pipe(gulp.dest(options.static + 'js/')); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 93 | }); |
| 94 | |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 95 | // set templates in cache |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 96 | gulp.task('templates', function(){ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 97 | return gulp.src('./src/templates/*.html') |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 98 | .pipe(templateCache({ |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 99 | module: 'xos.contentProvider', |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 100 | root: 'templates/' |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 101 | })) |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 102 | .pipe(gulp.dest(options.tmp)); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 103 | }); |
| 104 | |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 105 | // copy html index to Django Folder |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 106 | gulp.task('copyHtml', function(){ |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 107 | return gulp.src(options.src + 'index.html') |
| 108 | // remove dev dependencies from html |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 109 | .pipe(replace(/<!-- bower:css -->(\n^<link.*)*\n<!-- endbower -->/gmi, '')) |
| 110 | .pipe(replace(/<!-- bower:js -->(\n^<script.*)*\n<!-- endbower -->/gmi, '')) |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 111 | // injecting minified files |
| 112 | .pipe( |
| 113 | inject( |
| 114 | gulp.src([ |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 115 | options.static + 'vendor/xosContentProviderVendor.js', |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 116 | options.static + 'js/xosContentProvider.js', |
| 117 | options.static + 'css/xosContentProvider.css' |
Matteo Scandolo | c0c9409 | 2016-02-02 17:25:18 -0800 | [diff] [blame] | 118 | ]), |
| 119 | {ignorePath: '/../../../xos/core/xoslib'} |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 120 | ) |
| 121 | ) |
| 122 | .pipe(rename('xosContentProvider.html')) |
| 123 | .pipe(gulp.dest(options.dashboards)); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 124 | }); |
| 125 | |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 126 | // minify vendor js files |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 127 | gulp.task('wiredep', function(){ |
| 128 | var bowerDeps = wiredep().js; |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 129 | if(!bowerDeps){ |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | // remove angular (it's already loaded) |
| 134 | _.remove(bowerDeps, function(dep){ |
| 135 | return dep.indexOf('angular/angular.js') !== -1; |
| 136 | }); |
| 137 | |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 138 | return gulp.src(bowerDeps) |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 139 | .pipe(concat('xosContentProviderVendor.js')) |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 140 | .pipe(uglify()) |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 141 | .pipe(gulp.dest(options.static + 'vendor/')); |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 142 | }); |
| 143 | |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 144 | gulp.task('lint', function () { |
| 145 | return gulp.src(['src/js/**/*.js']) |
| 146 | .pipe(eslint()) |
| 147 | .pipe(eslint.format()) |
| 148 | .pipe(eslint.failAfterError()); |
| 149 | }); |
Matteo Scandolo | c2d0f53 | 2015-10-29 16:13:35 +0100 | [diff] [blame] | 150 | |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 151 | gulp.task('wait', function (cb) { |
| 152 | // setTimeout could be any async task |
| 153 | setTimeout(function () { |
| 154 | cb(); |
| 155 | }, 1000); |
| 156 | }); |
| 157 | |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 158 | gulp.task('build', function() { |
| 159 | runSequence( |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 160 | 'clean', |
| 161 | 'sass', |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 162 | 'templates', |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 163 | 'babel', |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 164 | 'scripts', |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 165 | 'wiredep', |
Matteo Scandolo | 97532ef | 2016-05-17 17:12:03 -0700 | [diff] [blame] | 166 | 'css', |
| 167 | 'copyCss', |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 168 | 'copyImages', |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 169 | 'copyHtml', |
Matteo Scandolo | bbcc01b | 2015-11-04 16:30:43 +0100 | [diff] [blame] | 170 | 'cleanTmp' |
Matteo Scandolo | 608f91d | 2015-10-29 11:54:54 +0100 | [diff] [blame] | 171 | ); |
| 172 | }); |
Matteo Scandolo | d44ceb4 | 2015-11-06 17:14:28 +0100 | [diff] [blame] | 173 | }; |