-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathgulpfile.js
813 lines (633 loc) · 20.6 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
var gulp = require('gulp'),
gutil = require('gulp-util'), //=> Gulp utilities (--dev, noop(), ...)
runSequence = require('gulp-sequence'), //=> Run tasks synchronously
del = require('del'), //=> Remove files with patterns
concat = require('gulp-concat'),
rename = require("gulp-rename"),
sourcemaps = require('gulp-sourcemaps'),
browserify = require('gulp-browserify'), //=> Compile JavaScript files with their dependencies
uglify = require('gulp-uglify'),
sass = require('gulp-sass'),
combineMq = require('gulp-combine-media-queries'),
prefix = require('gulp-autoprefixer'),
cssmin = require('gulp-cssmin'),
iconfont = require('gulp-iconfont'),
iconfontcss = require('gulp-iconfont-css'),
phpspec = require('gulp-phpspec'),
imagemin = require('gulp-imagemin'),
newer = require('gulp-newer'),
browserSync = require('browser-sync'),
notifier = require('node-notifier'), //=> For notifications not via .pipe()
notify = require('gulp-notify'); //=> For notifications via .pipe()
// ====================================================================================
// ~~~ CONFIGURATION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ====================================================================================
var dev = !! gutil.env.dev;
var production = ! dev;
var config = {};
/**
* CSS Configuration
*/
config.css = {
sourceMaps: dev,
combineMediaQueries: production,
// Auto-Prefix Settings
autoPrefix: {
enabled: true,
browsers: ['last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4']
},
// SASS Settings
sass: {
minify: false,
src: 'assets/sass/',
dest: 'public/css/',
clearCompiled: false //=> This will delete all *.css files from the dest folder when all tasks are done!
},
// Concatenation Settings
concat: {
minify: false,
files: [],
outputFilename: 'styles.css',
dest: 'public/css/'
},
// Minify Files Settings
minify: {
enabled: true,
files: [
'public/css/*.css',
'!public/css/*.min.css' //=> Not already minified files
],
suffix: '.min',
dest: 'public/css/'
}
};
/**
* JS Configuration
*/
config.js = {
sourceMaps: dev,
// Browserify Settings
browserify: {
uglify: false,
src: 'assets/js/',
mainFilename: 'main.js',
dest: 'compiled/',
clearCompiled: production //=> This will delete all *.js files from the dest folder when all tasks are done!
},
// Concatenation Settings
concat: {
uglify: false,
files: [],
outputFilename: 'scripts.js',
dest: 'public/js/'
},
// Minify Files Settings
uglify: {
enabled: true,
files: [
'assets/js/*.js',
'!assets/js/*.min.js' //=> Not already minified files
],
suffix: '.min',
dest: 'public/js/'
}
};
/**
* Copy Files Configuration
*/
config.copy = {
enabled: true,
files: {
// To avoid task naming conflicts, always start
// your files object name with "Copy":
// CopyJS, CopyCSS, CopyWhatever
CopyJS: {
src: [
"bower_components/rem-unit-polyfill/js/rem.min.js",
"bower_components/html5shiv/dist/html5shiv.min.js"
//'assets/js/*.js'
],
dest: "public/js/"
},
CopyCSS: {
src: [
"bower_components/normalize.css/normalize.css"
],
dest: "public/css/"
}
}
};
/**
* Icon Font Configuration
*/
config.iconFont = {
name : 'icon-font',
src: 'assets/icon-font/',
dest: 'public/fonts/',
css : {
template: 'assets/icon-font-template.scss',
dest: config.css.sass.src + '_icon-font.scss',
fontPath: '../fonts/' //=> Relative path from the CSS file to the font
}
};
/**
* Image Optimalization Configuration
*/
config.images = {
src: 'assets/images/',
dest: 'public/images/'
};
/**
* PHPSpec Configuration
*/
// Make sure you also set the correct
// mappings in phpspec.yml
config.phpspec = {
spec: 'spec/',
php: 'src/'
};
/**
* Browser Sync Configuration
*/
config.sync = {
root: 'public/',
proxy: null, //=> Don't start a server but use an existing host (ex.: "app.dev")
open: "external" //=> Open browser automatically (false|"local"|"external")
};
// ====================================================================================
// ~~~ DEFAULT TASK ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ====================================================================================
if (dev) {
gulp.task('default', function (cb) {
runSequence('copy', 'images', 'icon-font', 'css', 'js', 'watch', cb);
});
} else {
gulp.task('default', function (cb) {
runSequence('copy', 'images', 'icon-font', 'css', 'js', cb);
});
}
gulp.task('watch', ['serve', 'watch-copy', 'watch-images', 'watch-icon-font', 'watch-css', 'watch-js']);
// ====================================================================================
// ~~~ BROWSER SYNC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ====================================================================================
/**
* Sync Browsers
*/
var bsOptions = {
// Watch these files for changes
files: [
config.sync.root + "**/*.css",
config.sync.root + "**/*.html",
config.sync.root + "**/*.php",
config.sync.root + "**/*.js"
],
// Don't show any notifications in the browser
notify: false,
// Open browser automatically
open: config.sync.open
};
if (config.sync.proxy) {
bsOptions.proxy = config.sync.proxy
} else {
bsOptions.server = {
baseDir: config.sync.root
}
}
gulp.task('serve', function () {
gutil.log(gutil.colors.green('Starting BrowserSync...'));
browserSync(bsOptions);
});
// ====================================================================================
// ~~~ CSS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ====================================================================================
/**
* Compile SASS:
* - Compile SASS with source maps (dev)
* - Compile SASS without source maps (production)
* - Combine media queries (production) (source maps won't work well with this)
* - Add browser prefixes
* - Minify CSS
*/
gulp.task('compile-sass', function () {
// Fetch SASS files
return gulp.src(config.css.sass.src + '**/*.scss')
// Start SASS source map
.pipe(config.css.sourceMaps ? sourcemaps.init() : gutil.noop())
// Compile SASS
.pipe(sass())
.on('error', notifySASSError)
// Save SASS source map
.pipe(config.css.sourceMaps ? sourcemaps.write() : gutil.noop())
// Start new source map and load SASS source map
.pipe(config.css.sourceMaps ? sourcemaps.init({loadMaps: true}) : gutil.noop())
// Combine media queries (source maps won't work well with this)
.pipe(config.css.combineMediaQueries ? combineMq() : gutil.noop())
// Add browser prefixes
.pipe(config.css.autoPrefix.enabled ? prefix(config.css.autoPrefix.browsers) : gutil.noop())
// Minify CSS
.pipe(config.css.sass.minify ? cssmin({keepSpecialComments: 0}) : gutil.noop())
// Save source map
.pipe(config.css.sourceMaps ? sourcemaps.write() : gutil.noop())
// Save compiled CSS file
.pipe(gulp.dest(config.css.sass.dest));
});
/**
* SASS Task with notification
*/
gulp.task('sass', function (cb) {
runSequence('compile-sass', 'css-notification', cb);
});
/**
* SASS Watcher
*/
gulp.task('watch-sass', function () {
gulp.watch(config.css.sass.src + '**/*.scss', ['sass']);
});
/**
* Concatenate CSS:
* - Concatenate CSS files with source maps (dev)
* - Concatenate CSS files without source maps (production)
* - Combine media queries (production) (source maps won't work well with this)
* - Add browser prefixes
* - Minify CSS
*/
gulp.task('concat-css', function () {
// Fetch CSS files
return gulp.src(config.css.concat.files)
// Start source map
.pipe(config.css.sourceMaps ? sourcemaps.init({loadMaps: true}) : gutil.noop())
// Concatenate files
.pipe(concat(config.css.concat.outputFilename))
// Combine media queries (source maps won't work well with this)
.pipe(config.css.combineMediaQueries ? combineMq() : gutil.noop())
// Add browser prefixes
.pipe(config.css.autoPrefix.enabled ? prefix(config.css.autoPrefix.browsers) : gutil.noop())
// Minify CSS
.pipe(config.css.concat.minify ? cssmin({keepSpecialComments: 0}) : gutil.noop())
// Save source map
.pipe(config.css.sourceMaps ? sourcemaps.write() : gutil.noop())
// Save output to destination folder
.pipe(gulp.dest(config.css.concat.dest));
});
/**
* Minify CSS Files
*/
gulp.task('minify-css', function (cb) {
// Only run if minify is enabled
if (config.css.minify.enabled) {
// Fetch CSS files
return gulp.src(config.css.minify.files)
// Start source map
.pipe(config.css.sourceMaps ? sourcemaps.init({loadMaps: true}) : gutil.noop())
// Minify CSS
.pipe(cssmin({keepSpecialComments: 0}))
// Add file suffix
.pipe(rename({suffix: config.css.minify.suffix}))
// Save source map
.pipe(config.css.sourceMaps ? sourcemaps.write() : gutil.noop())
// Save files to destination folder
.pipe(gulp.dest(config.css.minify.dest));
}
cb();
});
/**
* Clean Up CSS Output (production)
*/
gulp.task('cleanup-css', function (cb) {
config.css.sass.clearCompiled
? del([config.css.sass.dest + '*.css'], cb)
: cb();
});
/**
* Default CSS Task:
* - Compile SASS
* - Concatenate
* - Minify
* - Clean Up Output
*/
gulp.task('css', function (cb) {
runSequence('compile-sass', 'concat-css', 'minify-css', 'cleanup-css', 'css-notification', cb);
});
/**
* CSS Watcher
*/
gulp.task('watch-css', function () {
gulp.watch(config.css.sass.src + '**/*.scss', ['css']);
});
/**
* Show CSS Success Notification
*/
gulp.task('css-notification', function () {
notifySuccess('CSS Compiled Successfully!');
});
// ====================================================================================
// ~~~ JAVASCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ====================================================================================
/**
* Browserify JS:
* - Compile the main JS file with it's dependencies/modules
* - Use source maps (dev)
* - Uglify JS
*/
gulp.task('browserify-js', function () {
// Fetch JS files
return gulp.src(config.js.browserify.src + config.js.browserify.mainFilename)
// Compile main JS file with dependencies/modules
.pipe(browserify({debug: config.js.sourceMaps}))
.on('error', notifyJSError)
// Start source map
.pipe(config.js.sourceMaps ? sourcemaps.init({loadMaps: true}) : gutil.noop())
// Uglify JS (production)
.pipe(config.js.browserify.uglify ? uglify() : gutil.noop())
// Save source map
.pipe(config.js.sourceMaps ? sourcemaps.write() : gutil.noop())
// Save output to destination folder
.pipe(gulp.dest(config.js.browserify.dest));
});
/**
* Browserify JS Task with notification
*/
gulp.task('browserify', function (cb) {
runSequence('browserify-js', 'js-notification', cb);
});
/**
* Browserify JS Watcher
*/
gulp.task('watch-browserify', function () {
gulp.watch(config.js.browserify.src + '**/*.js', ['browserify']);
});
/**
* Concatenate JS:
* - Concatenate JS files with source maps (dev)
* - Concatenate JS files without source maps (production)
* - Uglify JS
*/
gulp.task('concat-js', function () {
// Fetch CSS files
return gulp.src(config.js.concat.files)
// Start source map
.pipe(config.js.sourceMaps ? sourcemaps.init({loadMaps: true}) : gutil.noop())
// Concatenate files
.pipe(concat(config.js.concat.outputFilename))
// Uglify JS (production)
.pipe(config.js.concat.uglify ? uglify() : gutil.noop())
// Save source map
.pipe(config.js.sourceMaps ? sourcemaps.write() : gutil.noop())
// Save output to destination folder
.pipe(gulp.dest(config.js.concat.dest));
});
/**
* Uglify JS:
*/
gulp.task('uglify-js', function () {
// Fetch CSS files
return gulp.src(config.js.uglify.files)
// Start source map
.pipe(config.js.sourceMaps ? sourcemaps.init({loadMaps: true}) : gutil.noop())
// Uglify JS
.pipe(uglify())
// Add file suffix
.pipe(rename({suffix: config.js.uglify.suffix}))
// Save source map
.pipe(config.js.sourceMaps ? sourcemaps.write() : gutil.noop())
// Save output to destination folder
.pipe(gulp.dest(config.js.uglify.dest));
});
/**
* Clean Up JS Output (production)
*/
gulp.task('cleanup-js', function (cb) {
config.js.browserify.clearCompiled
? del([config.js.browserify.dest + '*.js'], cb)
: cb();
});
/**
* Show JS Success Notification
*/
gulp.task('js-notification', function () {
notifySuccess('JS Compiled Successfully!');
});
/**
* Default JS Task:
* - Browserify
* - Concatenate
* - Uglify
* - Clean Up Output
*/
gulp.task('js', function (cb) {
runSequence('browserify-js', 'concat-js', 'uglify-js', 'cleanup-js', 'js-notification', cb);
});
/**
* JS Watcher
*/
gulp.task('watch-js', function () {
gulp.watch(config.js.browserify.src + '**/*.js', ['js']);
});
// ====================================================================================
// ~~~ COPY FILES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ====================================================================================
/**
* Generate Tasks:
* - Create a task for each object in config.copy.files
*/
// Get the key names of the objects in config.copy.files
var copyTasks = Object.keys(config.copy.files);
if (config.copy.enabled) {
// Loop through the objects in config.copy.files
copyTasks.forEach(function (taskName) {
// Fetch the src files array from the current object
var files = config.copy.files[taskName];
// Create task
return gulp.task(taskName, function () {
// Load files
var stream = gulp.src(files.src)
// Only process the ones that changed
.pipe(newer(files.dest))
// Save files in destination folder
.pipe(gulp.dest(files.dest));
notifySuccess(taskName + ': Files Copied Successfully!');
return stream;
});
});
}
/**
* Run Generated Tasks
*/
gulp.task('copy', function (cb) {
config.copy.enabled
? runSequence(copyTasks, cb)
: cb();
});
/**
* Copy Files Watcher
*/
gulp.task('watch-copy', function (cb) {
config.copy.enabled
? addCopyFileWatchers()
: cb();
});
function addCopyFileWatchers() {
// For some reason, if you run the copy and watch-copy tasks in a row (default gulp task),
// then "copyTasks" is at this point an array of empty objects instead of an array of strings. Weird....?
// If you run the tasks separately, the "copyTasks" var stays intact...
// For now let's just redefine the array...
copyTasks = Object.keys(config.copy.files);
copyTasks.forEach(function (taskName) {
gulp.watch(config.copy.files[taskName].src, [taskName]);
});
}
// ====================================================================================
// ~~~ ICON FONT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ====================================================================================
/**
* Compile Icon Font
*/
gulp.task('icon-font', function () {
// Fetch SVG files
var stream = gulp.src([config.iconFont.src + '*.svg'], {base: '.'})
// Create font CSS
.pipe(iconfontcss({
fontName: config.iconFont.name,
path: config.iconFont.css.template,
targetPath: getRoot(config.iconFont.dest) + config.iconFont.css.dest,
fontPath: config.iconFont.css.fontPath
}))
// Create font
.pipe(iconfont({fontName: config.iconFont.name, normalize: true}))
// Save font files
.pipe(gulp.dest(config.iconFont.dest));
notifySuccess('Icon Font Compiled Successfully!');
return stream;
});
/**
* Icon Font Watcher
*/
gulp.task('watch-icon-font', function () {
gulp.watch(config.iconFont.src + '*.svg', ['icon-font']);
});
// ====================================================================================
// ~~~ IMAGE OPTIMIZATION ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ====================================================================================
/**
* Optimize Images
*/
gulp.task('images', function () {
// Fetch original images
var stream = gulp.src(config.images.src + '**')
// Only process the ones that changed
.pipe(newer(config.images.dest))
// Optimize images
.pipe(imagemin({
progressive: true,
svgoPlugins: [{removeViewBox: false}]
}))
// Save optimized images
.pipe(gulp.dest(config.images.dest));
notifySuccess('All Images Optimized!');
return stream;
});
/**
* Image Optimization Watcher
*/
gulp.task('watch-images', function () {
gulp.watch([config.images.src + '**'], ['images']);
});
// ====================================================================================
// ~~~ PHPSPEC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ====================================================================================
/**
* Run PHPSpec Tests
*/
gulp.task('run-phpspec', function () {
// Fetch tests
var stream = gulp.src(config.phpspec.spec + '**/*.php')
// Run tests
.pipe(phpspec('', {notify: true}))
.on('error', notifyPHPSpecError);
notifySuccess('PHPSpec: All Tests Successful!');
return stream;
});
/**
* PHPSpec Watcher
*/
gulp.task('phpspec', ['phpspec-once'], function () {
gulp.watch([config.phpspec.spec + '**/*.php', config.phpspec.php + '**/*.php'], ['run-phpspec']);
});
// ====================================================================================
// ~~~ HELPER METHODS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ====================================================================================
/**
* Get Project Root
*/
function getRoot(path) {
var backPath = '',
depth = (path.match(/\//g) || []).length;
for (var i = 0; i < depth; i++) {
backPath += '../';
}
return backPath;
}
/**
* Suppress Plugin Console Output
*/
var glog = gutil.log;
gutil.log = function () {
var args = Array.prototype.slice.call(arguments);
if (args.length) {
if (/^.*gulp-imagemin|gulp-svgicons2svgfont.*$/.test(args[0])){
return;
}
}
return glog.apply(console, args);
};
/**
* Error Notification
*/
// Keep track of errors for
// success notification...
var hasErrors = false;
function notifyError(title, message, error) {
hasErrors = true;
notify.onError({
title: title,
message: message,
sound: 'Sosumi'
//icon: __dirname + '/fail.png'
})(error);
console.log(error.toString());
gutil.log(gutil.colors.red(message));
}
/**
* Success Notification
*/
function notifySuccess(message) {
if (hasErrors == false) {
notifier.notify({
title: 'Yaaay!',
message: message
});
gutil.log(gutil.colors.green(message));
}
hasErrors = false;
}
/**
* Error Handlers
*/
function notifySASSError(error) {
var title = 'Ooops...',
message = 'Error Compiling SASS!';
notifyError(title, message, error);
this.emit('end');
}
function notifyJSError(error) {
var title = 'Ooops...',
message = 'Error Compiling JS!';
notifyError(title, message, error);
this.emit('end');
}
function notifyPHPSpecError(error) {
var title = 'Ooops...',
message = 'PHPSpec Tests Failed!';
notifyError(title, message, error);
this.emit('end');
}