Matteo Scandolo | 8cc22e4 | 2016-04-12 09:00:04 -0700 | [diff] [blame] | 1 | '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 | |
| 11 | var gulp = require('gulp'); |
| 12 | var ngAnnotate = require('gulp-ng-annotate'); |
| 13 | var uglify = require('gulp-uglify'); |
| 14 | var templateCache = require('gulp-angular-templatecache'); |
| 15 | var runSequence = require('run-sequence'); |
| 16 | var concat = require('gulp-concat'); |
| 17 | var del = require('del'); |
| 18 | var wiredep = require('wiredep'); |
| 19 | var angularFilesort = require('gulp-angular-filesort'); |
| 20 | var _ = require('lodash'); |
| 21 | var eslint = require('gulp-eslint'); |
| 22 | var inject = require('gulp-inject'); |
| 23 | var rename = require('gulp-rename'); |
| 24 | var replace = require('gulp-replace'); |
| 25 | |
| 26 | module.exports = function(options){ |
| 27 | |
| 28 | // delete previous builded file |
| 29 | gulp.task('clean', function(){ |
| 30 | return del( |
| 31 | [options.dashboards + 'xosSampleView.html'], |
| 32 | {force: true} |
| 33 | ); |
| 34 | }); |
| 35 | |
| 36 | // inject CSS |
| 37 | gulp.task('injectCss', function(){ |
| 38 | return gulp.src(options.src + 'index.html') |
| 39 | .pipe( |
| 40 | inject( |
| 41 | gulp.src(options.src + 'css/*.css'), |
| 42 | { |
| 43 | ignorePath: [options.src] |
| 44 | } |
| 45 | ) |
| 46 | ) |
| 47 | .pipe(gulp.dest(options.src)); |
| 48 | }); |
| 49 | |
| 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 |
| 67 | gulp.task('copyCss', ['css'], function(){ |
| 68 | return gulp.src([`${options.tmp}/css/*.css`]) |
| 69 | .pipe(concat('xosDiagnostic.css')) |
| 70 | .pipe(gulp.dest(options.static + 'css/')) |
| 71 | }); |
| 72 | |
| 73 | // compile and minify scripts |
| 74 | gulp.task('scripts', function() { |
| 75 | return gulp.src([ |
| 76 | options.tmp + '**/*.js' |
| 77 | ]) |
| 78 | .pipe(ngAnnotate()) |
| 79 | .pipe(angularFilesort()) |
| 80 | .pipe(concat('xosSampleView.js')) |
| 81 | .pipe(uglify()) |
| 82 | .pipe(gulp.dest(options.static + 'js/')); |
| 83 | }); |
| 84 | |
| 85 | // set templates in cache |
| 86 | gulp.task('templates', function(){ |
| 87 | return gulp.src('./src/templates/*.html') |
| 88 | .pipe(templateCache({ |
| 89 | module: 'xos.sampleView', |
| 90 | root: 'templates/' |
| 91 | })) |
| 92 | .pipe(gulp.dest(options.tmp)); |
| 93 | }); |
| 94 | |
| 95 | // copy html index to Django Folder |
| 96 | gulp.task('copyHtml', ['clean'], function(){ |
| 97 | return gulp.src(options.src + 'index.html') |
| 98 | // remove dev dependencies from html |
| 99 | .pipe(replace(/<!-- bower:css -->(\n.*)*\n<!-- endbower --><!-- endcss -->/, '')) |
| 100 | .pipe(replace(/<!-- bower:js -->(\n.*)*\n<!-- endbower --><!-- endjs -->/, '')) |
| 101 | // injecting minified files |
| 102 | .pipe( |
| 103 | inject( |
| 104 | gulp.src([ |
| 105 | options.static + 'js/vendor/xosSampleViewVendor.js', |
| 106 | options.static + 'js/xosSampleView.js' |
| 107 | ]), |
| 108 | {ignorePath: '/../../../xos/core/xoslib'} |
| 109 | ) |
| 110 | ) |
| 111 | .pipe(rename('xosSampleView.html')) |
| 112 | .pipe(gulp.dest(options.dashboards)); |
| 113 | }); |
| 114 | |
| 115 | // minify vendor js files |
| 116 | gulp.task('wiredep', function(){ |
| 117 | var bowerDeps = wiredep().js; |
| 118 | if(!bowerDeps){ |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | // remove angular (it's already loaded) |
| 123 | _.remove(bowerDeps, function(dep){ |
| 124 | return dep.indexOf('angular/angular.js') !== -1; |
| 125 | }); |
| 126 | |
| 127 | return gulp.src(bowerDeps) |
| 128 | .pipe(concat('xosSampleViewVendor.js')) |
| 129 | .pipe(uglify()) |
| 130 | .pipe(gulp.dest(options.static + 'js/vendor/')); |
| 131 | }); |
| 132 | |
| 133 | gulp.task('lint', function () { |
| 134 | return gulp.src(['src/js/**/*.js']) |
| 135 | .pipe(eslint()) |
| 136 | .pipe(eslint.format()) |
| 137 | .pipe(eslint.failAfterError()); |
| 138 | }); |
| 139 | |
| 140 | gulp.task('build', function() { |
| 141 | runSequence( |
| 142 | 'templates', |
| 143 | 'babel', |
| 144 | 'scripts', |
| 145 | 'wiredep', |
| 146 | 'injectCss', |
| 147 | 'copyHtml', |
| 148 | 'cleanTmp' |
| 149 | ); |
| 150 | }); |
| 151 | }; |