-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
581 lines (532 loc) · 17.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
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
/**************************************************************************
* Copyleft joserprieto - Some Rights Reserved
*
* joserprieto
*
*
* @file gulpfile.js
* @author joserprieto
* @date 24/05/16 20:18
* @project Arte et Marte Layout
* @projectCode AEM-LYT
* @site http://preview.arteetmarte.org
* @repo [email protected]:joserprieto/aem-lyt.git
*
* @type javascript
*/
/**
* Based on various excellent tutorials:
* @see https://www.mikestreety.co.uk/blog/advanced-gulp-file
* @see https://markgoodyear.com/2014/01/getting-started-with-gulp/
* @see
* @see
*/
// Plugins:
/**
* Plugins:
* -------
*/
var gulp = require('gulp'),
eventStream = require('event-stream'),
gutil = require('gulp-util'),
livereloadwebserver = require('gulp-server-livereload'),
plugins = require("gulp-load-plugins")({
pattern: ['gulp-*', 'gulp.*'],
replaceString: /\bgulp[\-.]/
});
/**
* Development Variables and Functions:
* -----------------------------------
*/
// Development Variables and Functions:
// Allows gulp --env dev or gulp --env prod to be run for a more verbose output
gutil.log('Init gulpfile.js; the defined enviroment is (remember, use gulp --env {option}): ' + gutil.env.env );
var isProduction = (gutil.env.env === 'prod')?true:false;
var sassStyle = isProduction ? 'compressed': 'expanded',
useSourceMap = isProduction?false:true,
autoprefixOptions =
[
'last 2 version',
'safari 5',
'ie 8',
'ie 9',
'opera 12.1',
'ios 6',
'android 4'
],
imageMinOptions =
{
optimizationLevel : 3,
progressive : true,
interlaced : true
};
// changeEvent function - something which gets fired whenever a file changes.
// This outputs what file it was and what happened to it
var changeEvent = function(evt) {
gutil.log(
'File',
gutil.colors.cyan(
evt.path.replace(new RegExp('/.*(?=/' + basePaths.src + ')/'), '')
),
'was',
gutil.colors.magenta(evt.type)
);
};
// File paths
var basePaths =
{
html:
{
src : 'src/static/',
dest : 'web/'
},
assets:
{
src : 'src/static/theme/ArteEtMarte/assets/',
dest : 'web/theme/ArteEtMarte/assets/',
bower : 'bower_components/',
node : 'node_modules/'
},
thirdParty : 'third-party/'
},
paths =
{
html:
{
src : basePaths.html.src,
dest : basePaths.html.dest
},
images:
{
src : basePaths.assets.src + 'img/',
dest : basePaths.assets.dest + 'img/'
},
fonts:
{
src : basePaths.assets.src + 'fonts/',
dest : basePaths.assets.dest + 'fonts/'
},
icons:
{
src : basePaths.assets.src + 'icons/',
dest : basePaths.assets.dest + 'icons/'
},
jSscripts:
{
src : basePaths.assets.src + 'js/',
dest : basePaths.assets.dest + 'js/'
},
sassStyles:
{
src : basePaths.assets.src + 'sass/',
dest : basePaths.assets.dest + 'css/'
},
cssStyles:
{
src : basePaths.assets.src + 'css/',
dest : basePaths.assets.dest + 'css/'
},
vendor:
{
src : basePaths.assets.src + 'vendor/',
dest : basePaths.assets.dest + 'vendor/'
}
},
appFiles =
{
html : paths.html.src + '**/*.+(html|htm)',
sassStyles : paths.sassStyles.src + '**/*.+(scss|sass)',
cssStyles :
[
paths.cssStyles.src + '**/*.css'
],
jSscripts :
[
paths.jSscripts.src + '**/*.js'
]
},
// Materialize:
jsMaterialize =
basePaths.assets.bower + 'Materialize/dist/js/' +
(isProduction?'materialize.min.js':'materialize.js'),
// jQuery:
jsJQuery =
basePaths.assets.bower + 'jquery/dist/' +
(isProduction?'jquery.min.js':'jquery.js'),
// Swipe
cssSwiper =
basePaths.assets.bower + 'Swiper/dist/css/' +
(isProduction?'swiper.min.css':'swiper.css'),
jsSwiper =
basePaths.assets.bower + 'Swiper/dist/js/' +
(isProduction?'swiper.min.js':'swiper.js'),
// Unslider
cssUnslider=
basePaths.assets.bower + 'unslider/dist/css/**/*.css' +
(isProduction?'swipper.min.css':'swipper.css'),
jsUnslider =
(isProduction?
(basePaths.assets.bower + 'unslider/dist/js/**/*.js'):
(basePaths.assets.bower + 'unslider/src/js/**/*.js')),
jsUnslider =
(isProduction?
(basePaths.assets.bower + 'unslider/dist/js/**/*.js'):
(basePaths.assets.bower + 'unslider/src/js/**/*.js')),
// OwlCarousel
cssOwlCarouselBase =
basePaths.assets.bower + 'owl.carousel/dist/assets/' +
(isProduction?'owl.carousel.min.css':'owl.carousel.css'),
cssOwlCarouselTheme =
basePaths.assets.bower + 'owl.carousel/dist/assets/' +
(isProduction?'owl.theme.default.min.css':'own.theme.default.css'),
jsOwlCarousel =
basePaths.assets.bower + 'owl.carousel/dist/' +
(isProduction?'owl.carousel.min.js':'owl.carousel.js'),
// OwnCarousel2
cssOwlCarousel2Base =
basePaths.thirdParty + 'OwlCarousel2/dist/assets/' +
(isProduction?'owl.carousel.min.css':'owl.carousel.css'),
cssOwlCarousel2Theme =
basePaths.thirdParty + 'OwlCarousel2/dist/assets/' +
(isProduction?'owl.theme.default.min.css':'own.theme.default.css'),
jsOwlCarousel2 =
basePaths.thirdParty + 'OwlCarousel2/dist/' +
(isProduction?'owl.carousel.min.js':'owl.carousel.js'),
vendorFiles =
{
cssStyles:
[
cssSwiper,
cssOwlCarousel2Base
],
jSscripts:
[
jsMaterialize,
jsSwiper,
jsOwlCarousel2
]
};
// Variables dependent of --dev
var cssDestFileName = isProduction?'styles.css':'styles.css',
jsFDestFileName = isProduction?'scripts.js':'scripts.js';
/**
* Clean Tasks:
* ---------
*/
/**
* @task clean-dest
* @description clean the html and assets files of destiny paths
*/
gulp.task('clean-dest', function() {
var clean = plugins.rimraf,
gulpif = plugins.if,
debug = plugins.debug;
// Clean files on destiny:
gulp.src(
[
paths.html.dest + '/**/*.+(html|htm)', // HTML files
paths.jSscripts.dest + '/**/*.*', // jS Files
paths.cssStyles.dest + '/**/*.*', // CSS Files
paths.images.dest + '/**/*.*', // Image Files
basePaths.assets.dest + '/ico/**', // ICO Files
basePaths.assets.dest + '/fonts/**' // Font Files
],
{
read: false
}
)
.pipe(gulpif(!isProduction, debug({title: 'clean-dest:'})))
.pipe(
clean(
{
force: true
}
)
);
})
/**
* Copy plain files Tasks:
* -----------------------
*/
/**
* @task copy-fonts
* @description copy assets fonts files that don't need any operation
*/
gulp.task('copy-fonts', function() {
var gulpif = plugins.if,
debug = plugins.debug;
// copy any assets files in source path to public path (web root)
gulp.src(
[
paths.fonts.src + '/**' // Font Files
]
)
.pipe(gulpif(!isProduction, debug({title: 'copy-fonts:'})))
.pipe(
gulp.dest(paths.fonts.dest)
);
});
/**
* @task copy-icons
* @description copy assets icons files that don't need any operation
*/
gulp.task('copy-icons', function() {
var gulpif = plugins.if,
debug = plugins.debug;
// copy any assets files in source path to public path (web root)
gulp.src(
[
paths.icons.src + '/**' // Icon Files
]
)
.pipe(gulpif(!isProduction, debug({title: 'copy-icons:'})))
.pipe(
gulp.dest(paths.icons.dest)
);
});
/**
* @task copy-plain-html
* @description copy html files that don't need any operation
*/
gulp.task('copy-plain-html', function() {
var gulpif = plugins.if,
debug = plugins.debug;
// copy any html files in source path to public path (web root)
gulp.src(
[
appFiles.html
]
)
.pipe(gulpif(!isProduction, debug({title: 'copy-plain-html:'})))
.pipe(
gulp.dest(paths.html.dest)
);
});
/**
* Image tasks:
* ------------
*/
/**
* @task copy-images
* @description copy image files and minimice these files
*/
gulp.task('copy-images', function() {
var imagemin = plugins.imagemin,
gulpif = plugins.if,
debug = plugins.debug;
// copy any img files in source path to public path (web root)
gulp.src(
[
basePaths.assets.src + '/**/*.+(jpg|jpeg|gif|png|svg)' // Image Files
]
)
.pipe(gulpif(!isProduction, debug({title: 'copy-images:'})))
.pipe(
imagemin(imageMinOptions)
)
.pipe(
gulp.dest(basePaths.assets.dest)
);
});
/**
* jS Tasks:
* ---------
*/
/**
* @task jshint
* @description Lint our javascript (check for errors) using jshint and we’ll also set it up to run this task
* each time we save a javascript file
*/
gulp.task('jshint', function() {
var jshint = plugins.jshint,
gulpif = plugins.if,
debug = plugins.debug;
return gulp.src(appFiles.jSscripts)
.pipe(gulpif(!isProduction, debug({title: 'jshint:'})))
.pipe(jshint())
.pipe(jshint.reporter('jshinTaskt-stylish'));
});
/**
* @task build-js
* @description task to concat all jS needed for our web app, and, run it through uglify also to get
* a much smaller filesize.
*/
gulp.task('build-js', function() {
var sourcemaps = plugins.sourcemaps,
concat = plugins.concat,
gulpif = plugins.if,
uglify = plugins.uglify,
debug = plugins.debug,
jsSources = new Array();
jsSources = vendorFiles.jSscripts.concat(appFiles.jSscripts);
gulp.src(jsSources)
.pipe(gulpif(!isProduction, debug({title: 'build-js:'})))
.pipe(gulpif(!isProduction, sourcemaps.init())) // Process the original sources
.pipe(concat(jsFDestFileName))
//only uglify if gulp is ran with '--env prod'
.pipe(gulpif(isProduction, uglify()))
.pipe(gulpif(!isProduction, sourcemaps.write())) // Add the map to modified source
.pipe(gulp.dest(paths.jSscripts.dest));
});
/**
* CSS Tasks:
* ---------
*/
/**
* @task build-css
* @description Concatenate css files and copy to the web directory
*/
gulp.task('build-css', function() {
var sourcemaps = plugins.sourcemaps,
concat = plugins.concat,
gulpif = plugins.if,
autoprefixer = plugins.autoprefixer,
size = plugins.size,
debug = plugins.debug,
sass = plugins.sass,
combineMediaQueries = plugins.combineMediaQueries,
notify = plugins.notify,
cssmin = plugins.cssmin,
cssSources = appFiles.cssStyles.concat(vendorFiles.cssStyles),
sassSources = appFiles.sassStyles,
sassResult;
sassResult = gulp.src(sassSources)
.pipe(gulpif(!isProduction, debug({title: 'build-css; building sass:'})))
.pipe(gulpif(!isProduction, sourcemaps.init())) // Process the original sources
.pipe(
sass({ style: sassStyle})
.on('error', sass.logError)
)
.pipe(gulpif(!isProduction, sourcemaps.write())) // Add the map to modified source
.pipe(autoprefixer(autoprefixOptions))
.pipe(notify({ message: 'Sass build complete' }));
return eventStream.concat(gulp.src(cssSources), sassResult)
.pipe(gulpif(!isProduction, debug({title: 'build-css; building css:'})))
.pipe(concat(cssDestFileName))
.pipe(autoprefixer(autoprefixOptions))
.pipe(gulpif(isProduction, cssmin()))
.pipe(size())
.pipe(gulp.dest(paths.cssStyles.dest));
//.pipe(
// gulpif(isProduction,
// combineMediaQueries({
// log: true
// })
// )
//)
});
/**
* @task build-sass
* @description Build sass files and concatenate with another css files
* @note this task is only for dev / testing purpose; the build-css task build SASS files and concatenate the
* result with the css files needed
*/
gulp.task('build-sass', function() {
var sass = plugins.sass,
sourcemaps = plugins.sourcemaps,
autoprefixer = plugins.autoprefixer,
concat = plugins.concat,
debug = plugins.debug,
notify = plugins.notify,
rename = plugins.rename,
gulpif = plugins.if;
gulp.src(appFiles.sassStyles)
.pipe(gulpif(!isProduction, debug({title: 'build-sass:'})))
.pipe(gulpif(!isProduction, sourcemaps.init())) // Process the original sources
.pipe(sass({ style: sassStyle}).on('error', sass.logError))
.pipe(gulpif(!isProduction, sourcemaps.write())) // Add the map to modified source
.pipe(autoprefixer('last 2 version'))
.pipe(rename({
dirname: '',
basename: 'sass-style',
extname: ".css"
}))
.pipe(gulp.dest(paths.sassStyles.dest))
.pipe(notify({ message: 'Scripts task complete' }));
});
/**
* Watch Tasks:
* ---------
*/
/**
* @task watch
* @description configure which files to watch and what tasks to use on file changes
*/
gulp.task('watch', function() {
var livereload = plugins.livereload;
gulp.watch(appFiles.jSscripts, ['jshint']);
gulp.watch(appFiles.html, ['copy-plain-html']);
gulp.watch(paths.fonts.src, ['copy-fonts']);
gulp.watch(paths.icons.src, ['copy-icons']);
gulp.watch(
[
basePaths.assets.src + '/**/*.+(jpg|jpeg|gif|png|svg)' // Image Files
],
['copy-images']);
gulp.watch(appFiles.cssStyles, ['build-css']);
gulp.watch(appFiles.sassStyles, ['build-css']);
gulp.watch(appFiles.jSscripts, ['build-js']);
// Create LiveReload server
livereload.listen();
// Watch any files in dist/, reload on change
gulp.watch(
[
basePaths.html.dest + '/**',
basePaths.assets.dest + '/**'
]
)
.on('change', livereload.changed);
/*
Remember to place this snippet on the html for listen to the livereload server:
<script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"></' + 'script>')</script>
If your web site is running on another server, you need to specify the IP address of your local computer instead:
<script src="http://192.168.0.1:35729/livereload.js?snipver=1"></script>
*/
});
/**
* @task webserver
* @description Gulp task to run the web server and live reload the changes in browser
* @usage
* $ gulp webserver
* @see https://www.npmjs.com/package/gulp-server-livereload
* @see https://www.npmjs.com/package/livereload
*
* Isolated task; with a
* $ gulp
* The default tasks works fine
*
* default host: localhost
* default port: 8000
* default livereload port (socket.io): 35729
*/
gulp.task('webserver', function() {
gulp.src('web')
.pipe(
livereloadwebserver(
{
livereload: true,
directoryListing: true,
defaultFile: 'index.html',
open: true,
log: 'debug',
clientConsole: true
}
)
);
});
/**
* Default Tasks:
* ---------
*/
/**
* @task default
* @description define the default task and add the watch task to it, and the other task, before
*/
gulp.task('default', ['clean-dest',
'copy-fonts',
'copy-icons',
'copy-plain-html',
'copy-images',
'build-css',
'build-js',
'watch']);