-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
319 lines (255 loc) · 9.14 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*=====================================
= Default Configuration =
=====================================*/
// Please use config.js to override these selectively:
var config = {
dest: 'www',
cordova: false,
minify_images: true,
vendor: {
js: [
'./bower_components/angular/angular.js',
'./bower_components/angular-route/angular-route.js',
'./bower_components/mobile-angular-ui/dist/js/mobile-angular-ui.js',
'./bower_components/angular-cookies/angular-cookies.js',
'./bower_components/angular-cookie/angular-cookie.js',
'./bower_components/angular-sanitize/angular-sanitize.js',
'./bower_components/angular-animate/angular-animate.js',
'./bower_components/angular-localization/angular-localization.js',
'./bower_components/angulartics/src/angulartics.js',
'./bower_components/angulartics/src/angulartics-ga.js',
],
fonts: [
'./bower_components/font-awesome/fonts/fontawesome-webfont.*'
]
},
server: {
host: '0.0.0.0',
port: '8000'
},
weinre: {
httpPort: 8001,
boundHost: 'localhost',
verbose: false,
debug: false,
readTimeout: 5,
deathTimeout: 15
}
};
if (require('fs').existsSync('./config.js')) {
var configFn = require('./config');
configFn(config);
}
/*----- End of Configuration ------*/
/*========================================
= Requiring stuffs =
========================================*/
var gulp = require('gulp'),
seq = require('run-sequence'),
connect = require('gulp-connect'),
less = require('gulp-less'),
uglify = require('gulp-uglify'),
sourcemaps = require('gulp-sourcemaps'),
cssmin = require('gulp-cssmin'),
order = require('gulp-order'),
concat = require('gulp-concat'),
ignore = require('gulp-ignore'),
rimraf = require('gulp-rimraf'),
imagemin = require('gulp-imagemin'),
pngcrush = require('imagemin-pngcrush'),
templateCache = require('gulp-angular-templatecache'),
mobilizer = require('gulp-mobilizer'),
ngAnnotate = require('gulp-ng-annotate'),
replace = require('gulp-replace'),
ngFilesort = require('gulp-angular-filesort'),
streamqueue = require('streamqueue'),
rename = require('gulp-rename'),
path = require('path'),
postcss = require('gulp-postcss'),
autoprefixer = require('autoprefixer-core');
/*================================================
= Report Errors to Console =
================================================*/
gulp.on('error', function(e) {
throw(e);
});
/*=========================================
= Clean dest folder =
=========================================*/
gulp.task('clean', function (cb) {
return gulp.src([
path.join(config.dest, 'index.html'),
path.join(config.dest, 'images'),
path.join(config.dest, 'css'),
path.join(config.dest, 'js'),
path.join(config.dest, 'fonts')
], { read: false })
.pipe(rimraf());
});
/*==========================================
= Start a web server =
==========================================*/
gulp.task('connect', function() {
if (typeof config.server === 'object') {
connect.server({
root: config.dest,
host: config.server.host,
port: config.server.port,
livereload: true
});
} else {
throw new Error('Connect is not configured');
}
});
/*==============================================================
= Setup live reloading on source changes =
==============================================================*/
gulp.task('livereload', function () {
gulp.src(path.join(config.dest, '*.html'))
.pipe(connect.reload());
});
/*=====================================
= Minify images =
=====================================*/
gulp.task('images', function () {
var stream = gulp.src('src/images/**/*');
if (config.minify_images) {
stream = stream.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}],
use: [pngcrush()]
}));
}
return stream.pipe(gulp.dest(path.join(config.dest, 'images')));
});
/*==================================
= Copy fonts =
==================================*/
gulp.task('fonts', function() {
return gulp.src(config.vendor.fonts)
.pipe(gulp.dest(path.join(config.dest, 'fonts')));
});
/*==================================
= Copy yaml =
==================================*/
gulp.task('yaml', function() {
return gulp.src('src/yaml/**/*')
.pipe(gulp.dest(path.join(config.dest, 'yaml')));
});
/*==================================
= Copy languages =
==================================*/
gulp.task('languages', function() {
return gulp.src('src/languages/**/*')
.pipe(gulp.dest(path.join(config.dest, 'languages')));
});
/*==================================
= Copy static js =
==================================*/
gulp.task('js-static', function() {
return gulp.src('src/js-static/**/*')
.pipe(gulp.dest(path.join(config.dest, 'js-static')));
});
/*=================================================
= Copy html files to dest =
=================================================*/
gulp.task('html', function() {
var inject = [];
if (typeof config.weinre === 'object') {
inject.push('<script src="http://'+config.weinre.boundHost+':'+config.weinre.httpPort+'/target/target-script-min.js"></script>');
}
if (config.cordova) {
inject.push('<script src="cordova.js"></script>');
}
gulp.src(['src/html/**/*.html'])
.pipe(replace('<!-- inject:js -->', inject.join('\n ')))
.pipe(gulp.dest(config.dest));
});
/*======================================================================
= Compile, minify, mobilize less =
======================================================================*/
gulp.task('less', function () {
gulp.src(['./src/less/app.less', './src/less/responsive.less'])
.pipe(less({
paths: [ path.resolve(__dirname, 'src/less'), path.resolve(__dirname, 'bower_components') ]
}))
.pipe(postcss([ autoprefixer({ browsers: ['last 2 version'] }) ]))
.pipe(mobilizer('app.css', {
'app.css': {
hover: 'exclude',
screens: ['0px']
},
'hover.css': {
hover: 'only',
screens: ['0px']
}
}))
.pipe(cssmin())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest(path.join(config.dest, 'css')));
});
/*====================================================================
= Compile and minify js generating source maps =
====================================================================*/
// - Orders ng deps automatically
// - Precompile templates to ng templateCache
gulp.task('js', function() {
streamqueue({ objectMode: true },
gulp.src(config.vendor.js),
gulp.src('./src/js/**/*.js').pipe(ngFilesort()),
gulp.src(['src/templates/**/*.html']).pipe(templateCache({ module: 'K2020' }))
)
.pipe(sourcemaps.init())
.pipe(concat('app.js'))
.pipe(ngAnnotate())
.pipe(uglify())
.pipe(rename({suffix: '.min'}))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(path.join(config.dest, 'js')));
});
/*===================================================================
= Watch for source changes and rebuild/reload =
===================================================================*/
gulp.task('watch', function () {
if (typeof config.server === 'object') {
gulp.watch([config.dest + '/**/*'], ['livereload']);
}
gulp.watch(['./src/html/**/*'], ['html']);
gulp.watch(['./src/less/**/*'], ['less']);
gulp.watch(['./src/yaml/**/*'], ['yaml']);
gulp.watch(['./src/languages/**/*'], ['languages']);
gulp.watch(['./src/js/**/*', './src/templates/**/*', config.vendor.js], ['js']);
gulp.watch(['./src/images/**/*'], ['images']);
});
/*===================================================
= Starts a Weinre Server =
===================================================*/
gulp.task('weinre', function() {
if (typeof config.weinre === 'object') {
var weinre = require('./node_modules/weinre/lib/weinre');
weinre.run(config.weinre);
} else {
throw new Error('Weinre is not configured');
}
});
/*======================================
= Build Sequence =
======================================*/
gulp.task('build', function(done) {
var tasks = ['html', 'fonts', 'images', 'yaml', 'languages', 'less', 'js', 'js-static'];
seq('clean', tasks, done);
});
/*====================================
= Default Task =
====================================*/
gulp.task('default', function(done){
var tasks = [];
if (typeof config.weinre === 'object') {
tasks.push('weinre');
}
if (typeof config.server === 'object') {
tasks.push('connect');
}
tasks.push('watch');
seq('build', tasks, done);
});