Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 1 | /*eslint-env node */ |
| 2 | /*global require: true, module: true */ |
| 3 | |
| 4 | 'use strict'; |
| 5 | |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 6 | var options = require('../gulp-options.js'), gulp = require('gulp'), |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 7 | |
| 8 | // Base dependencies |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 9 | clean = require('gulp-clean'), rename = require('gulp-rename'), |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 10 | util = require('gulp-util'), |
| 11 | |
| 12 | // Angular gulp dependencies |
| 13 | ngTemplateCache = require('gulp-angular-templatecache'), |
| 14 | ngAnnotate = require('gulp-ng-annotate'), |
| 15 | ngConstant = require('gulp-ng-constant'), |
| 16 | |
| 17 | // Classical gulp dependencies |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 18 | stripDebug = require('gulp-strip-debug'), uglify = require('gulp-uglify'), |
| 19 | sass = require('gulp-sass'), cleanCss = require('gulp-clean-css'), |
| 20 | rev = require('gulp-rev'), revReplace = require('gulp-rev-replace'), |
| 21 | gulpIf = require('gulp-if'), useref = require('gulp-useref'), |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 22 | jsoncombine = require('gulp-jsoncombine'), |
| 23 | htmlParser = require('gulp-htmlparser'); |
| 24 | |
| 25 | |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 26 | var runSequence = require('run-sequence'), es = require('event-stream'); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 27 | |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 28 | gulp.task('webapp:clean', function() { |
| 29 | return gulp |
| 30 | .src( |
| 31 | [options.targetFolderPath + '/webapp', options.dirname + '/.temp'], |
| 32 | {'read': false}) |
| 33 | .pipe(clean({'force': true})); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 34 | }); |
| 35 | |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 36 | gulp.task('webapp:sasscompile', function() { |
| 37 | return gulp.src('app/styles/index.scss') |
| 38 | .pipe(sass.sync().on('error', util.log)) |
| 39 | .pipe(gulp.dest(options.srcFolderPath + '/styles')); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 40 | }); |
| 41 | |
| 42 | // ----- To .temp from app |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 43 | gulp.task('webapp:copyjs', function() { |
| 44 | return gulp.src(options.srcFolderPath + '/**/*.js') |
| 45 | .pipe(ngAnnotate()) // Check angular dependencies injection |
| 46 | .pipe(stripDebug()) // Remove all logs |
| 47 | .pipe(uglify({'mangle': false})) |
| 48 | .pipe(gulp.dest(options.dirname + '/.temp')); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 49 | }); |
| 50 | |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 51 | gulp.task('webapp:copyothers', function() { |
| 52 | return gulp |
| 53 | .src( |
| 54 | ['**/*', '!**/*.js', '!**/*.css', '!**/*.scss'], |
| 55 | {'cwd': options.srcFolderPath}) // All except JS files |
| 56 | .pipe(gulp.dest(options.tempFolderPath)); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 57 | }); |
| 58 | |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 59 | gulp.task('webapp:copycss', function() { |
| 60 | return gulp.src('app/styles/index.css') |
| 61 | .pipe(cleanCss()) |
| 62 | .pipe(gulp.dest(options.tempFolderPath + '/styles')); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 63 | }); |
| 64 | |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 65 | // ----- To target/webapp from .temp and bower_components |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 66 | gulp.task('webapp:template', function() { |
| 67 | return gulp |
| 68 | .src([ |
| 69 | options.srcFolderPath + '/**/*.html', |
| 70 | '!' + options.srcFolderPath + '/index.html' |
| 71 | ]) |
| 72 | .pipe(ngTemplateCache( |
| 73 | 'templates.js', {'module': 'app.templates', 'standalone': true})) |
| 74 | .pipe(gulp.dest(options.tempFolderPath)); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 75 | }); |
| 76 | |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 77 | gulp.task('webapp:useref', function() { |
| 78 | var tasks = ['index.html'].map(function(indexPage) { |
| 79 | var assets = useref.assets({}); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 80 | |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 81 | return gulp.src(options.tempFolderPath + '/' + indexPage) |
| 82 | .pipe(assets) |
| 83 | .pipe(assets.restore()) |
| 84 | .pipe(useref()) |
| 85 | .pipe(revReplace()) // Force useref to apply the 'rev' method |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 86 | .pipe(gulp.dest(options.targetFolderPath + '/webapp')); |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 87 | }); |
| 88 | |
| 89 | return es.concat.apply(null, tasks); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 90 | }); |
| 91 | |
Andrew Geissler | 7d6e629 | 2018-05-24 12:06:58 -0700 | [diff] [blame] | 92 | gulp.task('webapp:copyresources', function() { |
| 93 | return gulp |
| 94 | .src( |
| 95 | ['**/*.*', '!**/*.js', '!**/*.css', '!**/*.html', '!**/*.log'], |
| 96 | {'cwd': options.tempFolderPath}) |
| 97 | .pipe(gulp.dest(options.targetFolderPath + '/webapp')); |
| 98 | }); |
| 99 | |
| 100 | module.exports = function(callback) { |
| 101 | return runSequence( |
| 102 | 'webapp:clean', 'webapp:sasscompile', |
| 103 | ['webapp:copyjs', 'webapp:copycss', 'webapp:copyothers'], |
Gunnar Mills | 2b9a0d9 | 2018-07-11 15:10:08 -0500 | [diff] [blame] | 104 | ['webapp:template'], ['webapp:useref'], ['webapp:copyresources'], |
| 105 | callback); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 106 | }; |