-
Notifications
You must be signed in to change notification settings - Fork 69
/
gulpfile.js
246 lines (220 loc) · 6.71 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
var {src, task, series, dest, watch} = require('gulp'),
autoprefixer = require('gulp-autoprefixer'),
concat = require('gulp-concat'),
connect = require('gulp-connect'),
declare = require('gulp-declare'),
fileinclude = require('gulp-file-include'),
handlebars = require('gulp-handlebars'),
insert = require('gulp-insert'),
livereload = require('gulp-livereload'),
merge = require('merge-stream'),
path = require('path'),
plumber = require('gulp-plumber'),
rename = require('gulp-rename'),
sass = require('gulp-sass'),
sourcemaps = require('gulp-sourcemaps'),
uglify = require('gulp-uglify'),
wrap = require('gulp-wrap'),
log = require('fancy-log'),
c = require('ansi-colors');
function errorHandler (err) {
log(c.red(err));
}
/**
* Task: `gulp webserver`
* spins up a webserver and livereloads
*/
function webserver (done) {
connect.server({
port: process.env.PORT || 8000,
livereload: true
});
done();
}
task('webserver', webserver)
/**
* Task: `gulp html`
* livereloads when html changes
*/
function html() {
return src([
'./*.html',
'./pattern-library/**/*.html',
//'!./pattern-library/partials/*.html'
])
.pipe(fileinclude())
.pipe(dest('./assets/html/'))
.pipe(livereload());
};
task('html', html);
var buildSass = function(rtl, compressed) {
var destName, source;
if (rtl) {
source = './assets/sass/utils/rtl/rtl.scss';
destName = 'rtl-style';
} else {
source = './assets/sass/utils/rtl/ltr.scss';
destName = 'style';
}
if (compressed) {
destName = destName + '.min';
}
destName = destName + '.css';
return src([source])
.pipe(plumber({
errorHandler: errorHandler
}))
.pipe(sourcemaps.init())
.pipe(sass({
includePaths : [
'node_modules/bourbon/core',
'node_modules/bourbon-neat/app/assets/stylesheets'
],
sourceComments: true,
outputStyle : compressed ? 'compressed' : 'nested'
}))
.pipe(autoprefixer())
.pipe(plumber.stop())
// Since we need the style.css file in the repo for gh-pages, but we do not review the file in Phabricator
// gulp-insert appends '/* @generated */' to the style.css file
// which automatically folds file in Phabricator
.pipe(insert.append('/* @generated */'))
.pipe(rename(destName))
.pipe(sourcemaps.write('./'))
.pipe(dest('./assets/css'))
.on('end', function () {
log.info(c.yellow('CSS compiled'));
})
.pipe(livereload());
};
/**
* Task: `sass`
* Converts Sass files to CSS (includes RTL support)
*/
/**
* Task: `rtl`
* Converts RTL Sass files to RTL CSS
*/
task('rtl', function() {
return buildSass(true, false);
});
task('rtlMin', function() {
return buildSass(true, true);
});
task('sassMin', function() {
return buildSass(false, true);
});
task('sass', series('rtl', 'sassMin', 'rtlMin', function() {
return buildSass(false, false);
}));
/**
* Task: `templates`
* Handlebars templates
*/
function templates (){
// Assume all partials start with an underscore
// You could also put them in a folder such as source/templates/partials/*.hbs
var partials = src(['./assets/templates/partials/_*.hbs', './assets/templates/front-end-guidelines/partials/_*.hbs'])
.pipe(handlebars())
.pipe(wrap('Handlebars.registerPartial(<%= processPartialName(file.relative) %>, Handlebars.template(<%= contents %>));', {}, {
imports: {
processPartialName: function(fileName) {
// Strip the extension and the underscore
// Escape the output with JSON.stringify
return JSON.stringify(path.basename(fileName, '.js').substr(1));
}
}
}));
var templates = src('./assets/templates/**/*.hbs')
.pipe(handlebars())
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(declare({
namespace: 'Ushahidi.templates',
noRedeclare: true // Avoid duplicate declarations
}));
// Output both the partials and the templates as build/js/templates.js
return merge(partials, templates)
.pipe(concat('handlebars.compiled.js'))
.pipe(dest('./assets/js/handlebars'));
};
task('templates', templates);
/**
* Task: `uglify`
* Minimizes and concatenates js files
*/
function uglifyJS() {
return src(['./assets/js/pattern-library/*','./assets/js/custom/*'])
.pipe(plumber({
errorHandler: errorHandler
}))
.pipe(uglify())
.pipe(concat('app.js'))
.pipe(plumber.stop())
.pipe(dest('./assets/js'))
.on('end', function () {
log.info(c.yellow('JS minified and concatenated into app.js'));
})
.pipe(livereload());
};
task('uglifyJS', uglifyJS);
function uglifyHandlebars () {
return src('./assets/js/handlebars/*')
.pipe(plumber({
errorHandler: errorHandler
}))
.pipe(uglify())
.pipe(concat('handlebars.js'))
.pipe(plumber.stop())
.pipe(dest('./assets/js'))
.on('end', function () {
log.info(c.yellow('Handlebars JS minified and concatenated into handlebars.js'));
})
.pipe(livereload());
};
task('uglifyHandlebars', uglifyHandlebars);
function uglifyCloudJS () {
return src([
'./assets/js/custom/_toggle.js',
'./assets/js/custom/survey-filter.js',
'./assets/js/custom/map.js',
'./assets/js/custom/_modal.js',
'./assets/js/cloud/*'
])
.pipe(plumber({
errorHandler: errorHandler
}))
.pipe(uglify())
.pipe(concat('cloud.js'))
.pipe(plumber.stop())
.pipe(dest('./assets/js'))
.on('end', function () {
log.info(c.yellow('JS minified and concatenated into cloud.js'));
})
.pipe(livereload());
};
task('uglifyCloudJS',uglifyCloudJS);
/**
* Task: `default`
* Default task optimized for development
*/
function watchFiles (done) {
// LiveReload
livereload.listen();
// Watch Handlebars templates
watch('./assets/templates/**/*.hbs', series('templates'))
// Watch JS
watch(['./assets/js/pattern-library/*', './assets/js/custom/*'], series('uglifyJS'));
watch(['./assets/js/handlebars/*'], series('uglifyHandlebars'));
watch(['./assets/js/cloud/*'], series('uglifyCloudJS'));
// Watch Sass
watch(['./assets/sass/**/*.scss'], series('sass'));
// Watch HTML
watch(['./*.html', './pattern-library/**/*.html'], series('html'));
done();
}
task('default', series('webserver', watchFiles));
/**
* Task: `build`
* Builds sass, fonts and js
*/
task('build', series('sass', 'templates', 'uglifyJS', 'uglifyHandlebars', 'uglifyCloudJS', 'html'));