-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
365 lines (273 loc) · 12.2 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'use strict';
var gulp = require('gulp'),
stylish = require('jshint-stylish'),
path = require('path'),
open = require('open'),
fs = require('fs'),
chalk = require('chalk'),
args = require('yargs').argv,
map = require('map-stream'),
browserSync = require('browser-sync'),
runSequence = require('run-sequence'),
gulpPlugins = require('gulp-load-plugins')();
// chalk config
var errorLog = chalk.red.bold,
hintLog = chalk.blue,
changeLog = chalk.red;
var SETTINGS = {
src: {
app: 'app/',
css: 'app/css/',
js: 'app/js/',
templates: 'app/templates/',
images: 'app/images/',
fonts: 'app/fonts/',
bower: 'bower_components/'
},
build: {
app: 'build/',
css: 'build/css/',
js: 'build/js/',
templates: 'build/templates/',
images: 'build/images/',
fonts: 'build/fonts/',
bower: 'build/bower/' // If you change this, you will have to change in index.html as well.
},
scss: 'scss/'
};
//server and live reload config
var serverConfig = {
root: 'build',
host: 'localhost',
port: 9001,
livereload: true
};
// jsHint Options.
var hintOptions = JSON.parse(fs.readFileSync('.jshintrc', 'utf8'));
// Flag for generating production code.
var isProduction = args.type === 'production';
/*============================================================
=> Server
============================================================*/
gulp.task('server', function () {
console.log('------------------>>>> firing server <<<<-----------------------');
gulpPlugins.connect.server(serverConfig);
console.log('Started connect web server on http://localhost:' + serverConfig.port + '.');
open('http://localhost:' + serverConfig.port);
});
gulp.task('tasks', gulpPlugins.taskListing);
/*============================================================
= JS-HINT =
============================================================*/
gulp.task('js:hint', function () {
console.log('-------------------------------------------------- JS - HINT');
var stream = gulp.src([SETTINGS.src.js + 'app.js', '!' + SETTINGS.src.js + 'plugins/*.js', SETTINGS.src.js + '**/*.js', 'gulpfile.js'])
.pipe(gulpPlugins.jshint(hintOptions))
.pipe(gulpPlugins.jshint.reporter(stylish));
return stream;
});
/*============================================================
= Concat =
============================================================*/
gulp.task('concat', ['concat:bower', 'concat:js', 'concat:css']);
gulp.task('concat:bower', function () {
console.log('-------------------------------------------------- CONCAT :bower');
var jsFilter = gulpPlugins.filter('**/*.js'),
cssFilter = gulpPlugins.filter('**/*.css'),
assetsFilter = gulpPlugins.filter(['!**/*.js', '!**/*.css', '!**/*.scss']);
gulpPlugins.bowerFiles()
.pipe(jsFilter)
.pipe(gulpPlugins.concat('bower.js'))
.pipe(gulpPlugins.if(isProduction, gulpPlugins.uglify()))
.pipe(gulp.dest(SETTINGS.build.bower))
.pipe(jsFilter.restore())
.pipe(cssFilter)
.pipe(gulpPlugins.sass())
.pipe(map(function (file, callback) {
var relativePath = path.dirname(path.relative(path.resolve(SETTINGS.src.bower), file.path));
// CSS path resolving
// Taken from https://github.com/enyojs/enyo/blob/master/tools/minifier/minify.js
var contents = file.contents.toString().replace(/url\([^)]*\)/g, function (match) {
// find the url path, ignore quotes in url string
var matches = /url\s*\(\s*(('([^']*)')|("([^"]*)")|([^'"]*))\s*\)/.exec(match),
url = matches[3] || matches[5] || matches[6];
// Don't modify data and http(s) urls
if (/^data:/.test(url) || /^http(:?s)?:/.test(url)) {
return 'url(' + url + ')';
}
return 'url(' + path.join(path.relative(SETTINGS.build.bower, SETTINGS.build.app), SETTINGS.build.bower, relativePath, url) + ')';
});
file.contents = new Buffer(contents);
callback(null, file);
}))
.pipe(gulpPlugins.concat('bower.css'))
.pipe(gulp.dest(SETTINGS.build.bower))
.pipe(cssFilter.restore())
.pipe(assetsFilter)
.pipe(gulp.dest(SETTINGS.build.bower))
.pipe(assetsFilter.restore())
.pipe(gulpPlugins.connect.reload());
});
gulp.task('concat:js', /*['js:hint'],*/ function () {
console.log('-------------------------------------------------- CONCAT :js');
gulp.src([SETTINGS.src.js + 'plugins/*.js', SETTINGS.src.js + 'app.js', SETTINGS.src.js + '*.js', SETTINGS.src.js + '**/*.js'])
.pipe(gulpPlugins.concat('all.js'))
.pipe(gulpPlugins.if(isProduction, gulpPlugins.uglify()))
.pipe(gulp.dest(SETTINGS.build.js))
.pipe(gulpPlugins.connect.reload());
});
gulp.task('convert:scss', function () {
console.log('-------------------------------------------------- COVERT - scss');
// Callback to show sass error
var showError = function (err) {
console.log(errorLog('\n SASS file has error clear it to see changes, see below log ------------->>> \n'));
console.log(errorLog(err));
};
var stream = gulp.src(SETTINGS.src.css + 'application.scss')
.pipe(gulpPlugins.sass({includePaths: [SETTINGS.src.css], onError: showError}))
.pipe(gulp.dest(SETTINGS.scss))
.pipe(gulpPlugins.connect.reload());
return stream;
});
gulp.task('concat:css', ['convert:scss'], function () {
console.log('-------------------------------------------------- CONCAT :css ');
gulp.src([SETTINGS.src.css + 'fonts.css', SETTINGS.scss + 'application.css', SETTINGS.src.css + '*.css'])
.pipe(gulpPlugins.concat('styles.css'))
.pipe(gulpPlugins.if(isProduction, gulpPlugins.minifyCss({keepSpecialComments: '*'})))
.pipe(gulp.dest(SETTINGS.build.css))
.pipe(gulpPlugins.connect.reload());
});
/*============================================================
= Minify =
============================================================*/
gulp.task('image:min', function () {
gulp.src(SETTINGS.src.images + '**')
.pipe(gulpPlugins.imagemin())
.pipe(gulp.dest(SETTINGS.build.images))
.pipe(gulpPlugins.connect.reload());
});
/*============================================================
= Copy =
============================================================*/
gulp.task('copy', ['copy:html', 'copy:images', 'copy:fonts', 'copy:html:root']);
gulp.task('copy:html', function () {
console.log('-------------------------------------------------- COPY :html');
gulp.src([SETTINGS.src.templates + '*.html', SETTINGS.src.templates + '**/*.html'])
.pipe(gulpPlugins.if(isProduction, gulpPlugins.minifyHtml({comments: false, quotes: true, spare: true, empty: true, cdata: true})))
.pipe(gulp.dest(SETTINGS.build.templates))
.pipe(gulpPlugins.connect.reload());
});
gulp.task('copy:html:root', function () {
console.log('-------------------------------------------------- COPY :html:root');
gulp.src(SETTINGS.src.app + '*.html')
.pipe(gulpPlugins.if(isProduction, gulpPlugins.minifyHtml({comments: false, quotes: true, spare: true, empty: true, cdata: true})))
.pipe(gulp.dest(SETTINGS.build.app))
.pipe(gulpPlugins.connect.reload());
});
gulp.task('copy:images', function () {
console.log('-------------------------------------------------- COPY :images');
gulp.src([SETTINGS.src.images + '*.*', SETTINGS.src.images + '**/*.*'])
.pipe(gulp.dest(SETTINGS.build.images));
});
gulp.task('copy:fonts', function () {
console.log('-------------------------------------------------- COPY :fonts');
gulp.src([SETTINGS.src.fonts + '*', SETTINGS.src.fonts + '**/*'])
.pipe(gulp.dest(SETTINGS.build.fonts));
});
/*=========================================================================================================
= Watch
Incase the watch fails due to limited number of watches available on your sysmtem, the execute this
command on terminal
$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p
=========================================================================================================*/
gulp.task('watch', function () {
console.log('watching all the files.....');
var watchedFiles = [];
watchedFiles.push(gulp.watch([SETTINGS.src.css + '*.css', SETTINGS.src.css + '**/*.css'], ['concat:css']));
watchedFiles.push(gulp.watch([SETTINGS.src.css + '*.scss', SETTINGS.src.css + '**/*.scss'], ['concat:css']));
watchedFiles.push(gulp.watch([SETTINGS.src.js + '*.js', SETTINGS.src.js + '**/*.js'], ['concat:js']));
watchedFiles.push(gulp.watch([SETTINGS.src.app + '*.html'], ['copy:html:root']));
watchedFiles.push(gulp.watch([SETTINGS.src.images + '*.*', SETTINGS.src.images + '**/*.*'], ['copy:images']));
watchedFiles.push(gulp.watch([SETTINGS.src.fonts + '*.*', SETTINGS.src.fonts + '**/*.*'], ['copy:fonts']));
watchedFiles.push(gulp.watch([SETTINGS.src.bower + '*.js', SETTINGS.src.bower + '**/*.js'], ['concat:bower']));
watchedFiles.push(gulp.watch([SETTINGS.src.templates + '*.html', SETTINGS.src.templates + '**/*.html'], ['copy:html']));
// Just to add log messages on Terminal, in case any file is changed
var onChange = function (event) {
if (event.type === 'deleted') {
runSequence('clean');
setTimeout(function () {
runSequence('copy', 'concat', 'watch');
}, 500);
}
console.log(changeLog('-------------------------------------------------->>>> File ' + event.path + ' was ------->>>> ' + event.type));
};
watchedFiles.forEach(function (watchedFile) {
watchedFile.on('change', onChange);
});
});
/*============================================================
= Clean =
============================================================*/
var cleanFiles = function (files, logMessage) {
console.log('-------------------------------------------------- CLEAN :' + logMessage);
gulp.src(files, {read: false})
.pipe(gulpPlugins.clean({force: true}));
};
gulp.task('clean', function () {
cleanFiles([SETTINGS.build.app], 'all files');
});
gulp.task('clean:css', function () {
cleanFiles([SETTINGS.build.css], 'css');
});
gulp.task('clean:js', function () {
cleanFiles([SETTINGS.build.js], 'js');
});
gulp.task('clean:html', function () {
cleanFiles([SETTINGS.build.templates], 'html');
});
gulp.task('clean:images', function () {
cleanFiles([SETTINGS.build.images], 'images');
});
gulp.task('clean:fonts', function () {
cleanFiles([SETTINGS.build.fonts + '*.*', SETTINGS.build.fonts + '**/*.*'], 'fonts');
});
gulp.task('clean:zip', function () {
cleanFiles(['zip/**/*', '!zip/build-*.zip'], 'zip');
});
/*============================================================
= Zip =
============================================================*/
gulp.task('zip', function () {
gulp.src([SETTINGS.build.app + '*', SETTINGS.build.app + '**/*'])
.pipe(gulpPlugins.zip('build-' + new Date() + '.zip'))
.pipe(gulp.dest('./zip/'));
setTimeout(function () {
runSequence('clean:zip');
}, 500); // wait for file creation
});
/*============================================================
= Start =
============================================================*/
gulp.task('build', function () {
console.log(hintLog('-------------------------------------------------- BUILD - Development Mode'));
runSequence('copy', 'concat', 'watch');
});
gulp.task('build:prod', function () {
console.log(hintLog('-------------------------------------------------- BUILD - Production Mode'));
isProduction = true;
runSequence('copy', 'concat', 'watch');
});
gulp.task('default', ['build', 'server']);
// Just in case you are too lazy to type: $ gulp --type production
gulp.task('prod', ['build:prod', 'server']);
/*============================================================
= Browser Sync =
============================================================*/
gulp.task('bs', function () {
browserSync.init([SETTINGS.build.app + 'index.html', SETTINGS.build + 'templates/*.html', SETTINGS.build.css + '*css', SETTINGS.build.js + '*.js'], {
proxy: {
host: '127.0.0.1',
port: serverConfig.port
}
});
});