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