-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
638 lines (609 loc) · 14.4 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
/**
* Gulpfile
*
* Use Gulp to build a WordPress widget.
*
* Implements:
* - BrowserSync (with watch task)
* - Sass compilation, autoprefixer, sourcemaps and minification (style,
* style-rtl, vendors styles)
* - Babel transpilation, concatenation and minification (main scripts and
* vendors scripts)
* - Images optimizations (JPG, PNG, GIF, SVG)
* - Pot file generation
* - Zip folder without development files
* - Copy `package.json` version to WP files
* - A one-time task to init your widget
*
* @author Armand Philippot <contact@armandphilippot.com>
*/
/**
* Load Gulp configuration and error handler.
*/
const config = require('./gulp/config');
const errorHandler = require('./gulp/error-handler');
/**
* Load Gulp plugins.
*/
/* CSS */
const { src, dest, watch, series, parallel, lastRun } = require('gulp');
const sass = require('gulp-sass');
const postcss = require('gulp-postcss');
const sorting = require('postcss-sorting');
const autoprefixer = require('autoprefixer');
const cleanCSS = require('gulp-clean-css');
const rtl = require('gulp-rtlcss');
/* JS */
const babel = require('gulp-babel');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
/* Images */
const imagemin = require('gulp-imagemin');
/* Translation */
const pot = require('gulp-wp-pot');
/* Live reload */
const browserSync = require('browser-sync');
const server = browserSync.create();
const reload = browserSync.reload;
/* Utilities */
const del = require('del');
const filter = require('gulp-filter');
const fs = require('fs');
const lec = require('gulp-line-ending-corrector');
const notify = require('gulp-notify');
const { pipeline } = require('stream');
const rename = require('gulp-rename');
const replace = require('gulp-replace');
const vinylPaths = require('vinyl-paths');
const zip = require('gulp-zip');
/**
* Load package.json data.
*/
const package = JSON.parse(fs.readFileSync('./package.json'));
const packageVersion = package.version;
/**
* Task: `serve`
*
* Init BrowserSync for live reloading.
*/
function serve(done) {
browserSync.init(config.browserSync);
done();
}
/**
* Task: `clean`
*
* Delete all generated assets.
*/
function clean(done) {
del.sync(config.clean.paths);
done();
}
/**
* Task: `publicStyles`
*
* Generate public style.css and style.min.css. SCSS to CSS compilation,
* properties sorting, autoprefixer, minification.
*/
function publicStyles() {
return pipeline(
[
src(config.styles.src.public, { sourcemaps: true }),
sass(config.styles.sassOptions),
postcss(
[sorting(config.styles.postCss.sortingOptions)],
[autoprefixer()]
),
lec(),
rename('style.css'),
dest(config.styles.dest.public),
filter('**/*.css'),
rename({ suffix: '.min' }),
cleanCSS(),
lec(),
dest(config.styles.dest.public, { sourcemaps: '.' }),
server.stream(),
notify({
title: 'publicStyles task complete',
message:
'style.css and style.min.css have been compiled and moved to the public folder.',
onLast: config.notify.onLastOption,
}),
],
errorHandler
);
}
/**
* Task: `publicRtlStyles`
*
* Generate public style-rtl.css and style-rtl.min.css. SCSS to CSS
* compilation, properties sorting, autoprefixer, minification.
*/
function publicRtlStyles() {
return pipeline(
[
src(config.styles.src.public, { sourcemaps: true }),
sass(config.styles.sassOptions),
postcss(
[sorting(config.styles.postCss.sortingOptions)],
[autoprefixer()]
),
lec(),
rename('style-rtl.css'),
rtl(),
dest(config.styles.dest.public),
filter('**/*.css'),
rename({ suffix: '.min' }),
cleanCSS(),
lec(),
dest(config.styles.dest.public, { sourcemaps: '.' }),
server.stream(),
notify({
title: 'publicRtlStyles task complete',
message:
'style-rtl.css and style-rtl.min.css have been compiled and moved to the public folder.',
onLast: config.notify.onLastOption,
}),
],
errorHandler
);
}
/**
* Task: `adminStyles`
*
* Generate admin style.css and style.min.css. SCSS to CSS compilation,
* properties sorting, autoprefixer, minification.
*/
function adminStyles() {
return pipeline(
[
src(config.styles.src.admin, { sourcemaps: true }),
sass(config.styles.sassOptions),
postcss(
[sorting(config.styles.postCss.sortingOptions)],
[autoprefixer()]
),
lec(),
rename('style.css'),
dest(config.styles.dest.admin),
filter('**/*.css'),
rename({ suffix: '.min' }),
cleanCSS(),
lec(),
dest(config.styles.dest.admin, { sourcemaps: '.' }),
server.stream(),
notify({
title: 'adminStyles task complete',
message:
'style.css and style.min.css have been compiled and moved to the admin folder.',
onLast: config.notify.onLastOption,
}),
],
errorHandler
);
}
/**
* Task: `adminRtlStyles`
*
* Generate admin style-rtl.css and style-rtl.min.css. SCSS to CSS compilation,
* properties sorting, autoprefixer, minification.
*/
function adminRtlStyles() {
return pipeline(
[
src(config.styles.src.admin, { sourcemaps: true }),
sass(config.styles.sassOptions),
postcss(
[sorting(config.styles.postCss.sortingOptions)],
[autoprefixer()]
),
lec(),
rename('style-rtl.css'),
rtl(),
dest(config.styles.dest.admin),
filter('**/*.css'),
rename({ suffix: '.min' }),
cleanCSS(),
lec(),
dest(config.styles.dest.admin, { sourcemaps: '.' }),
server.stream(),
notify({
title: 'adminRtlStyles task complete',
message:
'style-rtl.css and style-rtl.min.css have been compiled and moved to the admin folder.',
onLast: config.notify.onLastOption,
}),
],
errorHandler
);
}
/**
* Task: `publicScripts`
*
* Generate public scripts.js and scripts.min.js. JS concatenation and
* minification.
*/
function publicScripts() {
return pipeline(
[
src(config.scripts.src.public, { sourcemaps: true }),
babel(),
concat('scripts.js'),
lec(),
dest(config.scripts.dest.public),
rename({ suffix: '.min' }),
uglify(),
lec(),
dest(config.scripts.dest.public, { sourcemaps: '.' }),
server.stream(),
notify({
title: 'publicScripts task complete',
message:
'scripts.js and scripts.min.js have been compiled and moved to the public folder.',
onLast: config.notify.onLastOption,
}),
],
errorHandler
);
}
/**
* Task: `adminScripts`
*
* Generate admin scripts.js and scripts.min.js. JS concatenation and
* minification.
*/
function adminScripts() {
return pipeline(
[
src(config.scripts.src.admin, { sourcemaps: true }),
babel(),
concat('scripts.js'),
lec(),
dest(config.scripts.dest.admin),
rename({ suffix: '.min' }),
uglify(),
lec(),
dest(config.scripts.dest.admin, { sourcemaps: '.' }),
server.stream(),
notify({
title: 'adminScripts task complete',
message:
'scripts.js and scripts.min.js have been compiled and moved to the admin folder.',
onLast: config.notify.onLastOption,
}),
],
errorHandler
);
}
/**
* Task: `images`
*
* Compress images (png, jpg, gif, svg) and move them to the public folder.
*/
function images(done) {
return pipeline(
[
src(config.images.src, { since: lastRun(images) }),
imagemin([
imagemin.gifsicle(config.images.imageminOptions.gif),
imagemin.mozjpeg(config.images.imageminOptions.jpg),
imagemin.optipng(config.images.imageminOptions.png),
imagemin.svgo(config.images.imageminOptions.svg),
]),
dest(config.images.dest),
notify({
title: 'Images task complete',
message:
'Images have been compressed and moved to the public folder.',
onLast: config.notify.onLastOption,
}),
],
done()
);
}
/**
* Task: `moveFonts`
*
* Move fonts from src folder to public folder.
*/
function moveFonts(done) {
return pipeline(
[
src(config.fonts.src),
dest(config.fonts.dest),
notify({
title: 'moveFonts task complete',
message: 'Fonts have been moved to the public folder.',
}),
],
done()
);
}
/**
* Task: `compilePotFile`
*
* Generate a pot file for translation.
*/
function compilePotFile(done) {
return pipeline(
[
src(config.translation.src),
pot(config.translation.potOptions),
dest(config.translation.dest + '/' + config.translation.filename),
notify({
title: 'compilePotFile task complete',
message: 'POT file have been generated.',
}),
],
done()
);
}
/**
* Task: `zipWidget`
*
* Generate a zip version of the theme without development files.
*/
function zipWidget(done) {
return pipeline(
[
src(config.zip.src),
zip(config.zip.filename),
dest(config.zip.dest),
notify({
title: 'zipWidget task complete',
message: 'Widget have been zipped.',
}),
],
done()
);
}
/**
* Task: `bumpCSS`
*
* Copy package.json version in Sass variable and recompile CSS.
*/
function bumpCSS(done) {
return pipeline(
[
src(config.bump.styles.src),
replace(/widget_version: "(.{5})"/g, function () {
return 'widget_version: "' + packageVersion + '"';
}),
dest(config.bump.styles.dest),
],
done()
);
}
/**
* Task: `bumpPHP`
*
* Copy package.json version in functions.php.
*/
function bumpPHP(done) {
return pipeline(
[
src(config.bump.php.src),
replace(/Version: (.{5})/g, function () {
return 'Version: ' + packageVersion;
}),
replace(/_VERSION', '(.{5})'/g, function () {
return "_VERSION', '" + packageVersion + "'";
}),
dest(config.bump.php.dest),
],
done()
);
}
/**
* Task: `bumpTXT`
*
* Copy package.json version in readme.txt.
*/
function bumpTXT(done) {
return pipeline(
[
src(config.bump.txt.src),
replace(/Stable tag: (.{5})/g, function () {
return 'Stable tag: ' + packageVersion;
}),
dest(config.bump.txt.dest),
],
done()
);
}
/**
* Task: `renameMainFile`
*
* Init the widget folder by the main class file.
*/
function renameMainFile(done) {
return pipeline(
[
src(config.init.file.main, { base: './' }),
vinylPaths(del),
rename({
basename: 'class-' + config.init.packageName.toLowerCase(),
}),
dest(config.init.dest),
notify({
title: 'renameMainFile task complete',
message: 'Main class file has been renamed.',
onLast: config.notify.onLastOption,
}),
],
done()
);
}
/**
* Task: `renameAdminFile`
*
* Init the widget folder by the admin partial file.
*/
function renameAdminFile(done) {
return pipeline(
[
src(config.init.file.admin, { base: './' }),
vinylPaths(del),
rename({
basename:
config.init.packageName.toLowerCase() + '-admin-display',
}),
dest(config.init.dest),
notify({
title: 'renameAdminFile task complete',
message: 'Admin partial file has been renamed.',
onLast: config.notify.onLastOption,
}),
],
done()
);
}
/**
* Task: `renamePublicFile`
*
* Init the widget folder by the public partial file.
*/
function renamePublicFile(done) {
return pipeline(
[
src(config.init.file.public, { base: './' }),
vinylPaths(del),
rename({
basename:
config.init.packageName.toLowerCase() + '-public-display',
}),
dest(config.init.dest),
notify({
title: 'renamePublicFile task complete',
message: 'Public partial file has been renamed.',
onLast: config.notify.onLastOption,
}),
],
done()
);
}
/**
* Task: `replaceWidgetInfo`
*
* Init the widget folder by replacing some placeholders.
*/
function replaceWidgetInfo(done) {
return pipeline(
[
src(config.init.src, { base: './' }),
replace('Firstname Lastname', function () {
return config.init.authorName;
}),
replace('your@email.com', function () {
return config.init.authorEmail;
}),
replace('https://www.yourWebsite.com/', function () {
return config.init.authorUrl;
}),
replace('(WordPress.org usernames)', function () {
return config.init.contributors;
}),
replace('2020 Company Name', function () {
return config.init.copyright;
}),
replace('Your widget name', function () {
return config.init.name;
}),
replace('Your widget description.', function () {
return config.init.description;
}),
replace('Your-Package-Name', function () {
return config.init.packageName;
}),
replace('Your_Package_Name', function () {
return config.init.packageName.replace(/-/g, '_');
}),
replace('your-package-name', function () {
return config.init.packageName.toLowerCase();
}),
replace('your_package_name', function () {
return config.init.packageName.toLowerCase().replace(/-/g, '_');
}),
replace('YourPrefix', function () {
return config.init.prefix;
}),
replace('yourprefix', function () {
return config.init.prefix.toLowerCase();
}),
replace('YOURPREFIX', function () {
return config.init.prefix.toUpperCase();
}),
replace('https://github.com/your/repo', function () {
return config.init.repo;
}),
replace('yourTextDomain', function () {
return config.init.textDomain;
}),
replace('your-vendor-name', function () {
return config.init.vendorName;
}),
dest(config.init.dest),
notify({
title: 'replaceWidgetInfo task complete',
message:
'The placeholders have been replaced by your dotenv data.',
onLast: config.notify.onLastOption,
}),
],
done()
);
}
/**
* Task: `watchFiles`
*
* Reload tasks when files change.
*/
function watchFiles(done) {
watch(config.styles.watch.public).on(
'change',
series(parallel(publicStyles, publicRtlStyles), reload)
);
watch(config.styles.watch.admin).on(
'change',
series(parallel(adminStyles, adminRtlStyles), reload)
);
watch(config.scripts.watch.public).on(
'change',
series(publicScripts, reload)
);
watch(config.scripts.watch.admin).on(
'change',
series(adminScripts, reload)
);
watch(config.images.watch).on('change', series(images, reload));
watch(config.fonts.watch).on('change', series(moveFonts, reload));
watch(config.files.watch).on('change', series(compilePotFile, reload));
done();
}
/**
* Public tasks
*/
const build = parallel(
publicStyles,
publicRtlStyles,
adminStyles,
adminRtlStyles,
publicScripts,
adminScripts,
images,
moveFonts,
compilePotFile
);
exports.build = build;
exports.bump = parallel(bumpCSS, bumpPHP, bumpTXT);
exports.default = series(clean, build);
exports.initWidget = replaceWidgetInfo;
exports.renameFiles = parallel(
renameMainFile,
renameAdminFile,
renamePublicFile
);
exports.recompileCSS = parallel(publicStyles, publicRtlStyles);
exports.translate = compilePotFile;
exports.watch = parallel(serve, watchFiles);
exports.zipWidget = series(zipWidget);