blob: 6a8b99ee8e4219acce414e76dc95ae563a7e7dec [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
Iftekharul Islamcd789502017-04-19 14:37:55 -050048gulp.task('webapp:minifyvendorjs', function () {
49 return gulp
50 .src(options.bowerFolderPath + '/**/*.js')
51 .pipe(uglify({
52 preserveComments: 'false'
53 }))
54 .pipe(rename({suffix: '.min'}))
55 .pipe(gulp.dest(function(file) {
56 return file.base;
57 }))
58});
59
Iftekharul Islam99d199f2017-03-24 15:28:25 -050060// ----- To .temp from app
61gulp.task('webapp:copyjs', function () {
62 return gulp.src(options.srcFolderPath + '/**/*.js')
63 .pipe(ngAnnotate()) // Check angular dependencies injection
64 .pipe(stripDebug()) // Remove all logs
65 .pipe(uglify({ 'mangle': false }))
66 .pipe(gulp.dest(options.dirname + '/.temp'));
67});
68
69gulp.task('webapp:copyothers', function () {
70 return gulp.src(['**/*', '!**/*.js', '!**/*.css', '!**/*.scss'], { 'cwd': options.srcFolderPath }) // All except JS files
71 .pipe(gulp.dest(options.tempFolderPath));
72});
73
74gulp.task('webapp:copycss', function () {
75 return gulp
76 .src('app/styles/index.css')
77 .pipe(cleanCss())
78 .pipe(gulp.dest(options.tempFolderPath + '/styles'));
79});
80
81gulp.task('webapp:constants', function () {
82 return gulp
83 .src('environment-constants.json')
84 .pipe(ngConstant({
85 'name': 'app.constants',
86 'deps': false
87 }))
88 .pipe(rename('environment-constants.js'))
89 .pipe(gulp.dest(options.tempFolderPath + '/constants'));
90});
91
92// ----- To target/webapp from .temp and bower_components
93gulp.task('webapp:template', function () {
94 return gulp.src([options.srcFolderPath + '/**/*.html', '!' + options.srcFolderPath + '/index.html'])
95 .pipe(ngTemplateCache('templates.js', {
96 'module': 'app.templates',
97 'standalone': true
98 }))
99 .pipe(gulp.dest(options.tempFolderPath));
100});
101
102gulp.task('webapp:useref', function () {
103 var tasks = ['index.html'].map(function (indexPage) {
104 var assets = useref.assets({ });
105
106 return gulp.src(options.tempFolderPath + '/' + indexPage)
107 .pipe(assets)
108 .pipe(assets.restore())
109 .pipe(useref())
110 .pipe(revReplace()) // Force useref to apply the 'rev' method
111 .pipe(gulp.dest(options.targetFolderPath + '/webapp'));
112 });
113
114 return es.concat.apply(null, tasks);
115});
116
117gulp.task('webapp:copyresources', function () {
118 return gulp.src(['**/*.*', '!**/*.js', '!**/*.css', '!**/*.html', '!**/*.log'], { 'cwd': options.tempFolderPath })
119 .pipe(gulp.dest(options.targetFolderPath + '/webapp'));
120});
121
122module.exports = function (callback) {
123 return runSequence(
124 'webapp:clean',
125 'webapp:sasscompile',
Iftekharul Islamcd789502017-04-19 14:37:55 -0500126 'webapp:minifyvendorjs',
Iftekharul Islam99d199f2017-03-24 15:28:25 -0500127 [
128 'webapp:copyjs',
129 'webapp:copycss',
130 'webapp:copyothers'
131 ],
132 [
133 'webapp:constants',
134 'webapp:template'
135 ],
136 [
137 'webapp:useref'
138 ],
139 [
140 'webapp:copyresources'
141 ],
142 callback
143 );
144};