-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
573 lines (476 loc) · 13.8 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
// Config object
const CONFIG = require('./.projectconfig.js');
// Util
const gulp = require('gulp');
const browsersync = require('browser-sync');
const yargs = require('yargs').argv;
const fs = require('fs');
const log = require('fancy-log');
const flatmap = require('gulp-flatmap');
const sourcemaps = require('gulp-sourcemaps');
const gulpIf = require('gulp-if');
const colors = require('colors');
const using = require('gulp-using');
// const del = require('del');
// Sass
const sass = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const cssnano = require('gulp-cssnano');
const mediaQuery = require('gulp-group-css-media-queries');
const sassGlob = require('gulp-sass-glob');
const gulpStylelint = require('gulp-stylelint');
const postcss = require('gulp-postcss');
const objectFit = require('postcss-object-fit-images');
const easingGradients = require('postcss-easing-gradients');
// Rollup and Scripts
const rollup = require('rollup');
const rollupEach = require('gulp-rollup-each');
const rollupResolve = require('@rollup/plugin-node-resolve');
const rollupBabel = require('@rollup/plugin-babel');
const rollupCommon = require('@rollup/plugin-commonjs');
const rollupESLint = require('@rollup/plugin-eslint');
const uglify = require('gulp-uglify');
const gulpEsbuild = require('gulp-esbuild')
// Images
const imagemin = require('gulp-imagemin');
// GitHub Pages
const ghPages = require('gulp-gh-pages');
// Favicon
const realFavicon = require('gulp-real-favicon');
// Definitions
const localConfig = '.local-gulpconfig.json';
const localConfigDefault = '.ex-local-gulpconfig.json';
let production = yargs.production ? yargs.production : false;
let watchJsPath = yargs.watchjs ? yargs.watchjs : false;
let watchThemeOnly = watchJsPath ? true : false;
// ----------------------------------------------------------------------------
// GULP TASK FUNCTIONS
// ----------------------------------------------------------------------------
//
// Reload Browsersync
//
const reload = done => {
browsersync.reload();
done();
};
//
// Check to see if a .gulp-config.json file exists, if
// not, creates one from .ex-gulp-config.json
//
const checkGulpConfig = done => {
if (!CONFIG.useProxy) {
return false;
}
fs.access(localConfig, fs.constants.F_OK, err => {
if (err) {
let source = fs.createReadStream(localConfigDefault);
let dest = fs.createWriteStream(localConfig);
source.pipe(dest);
source.on('end', () => {
log(
`Edit the proxy value in ${localConfig} to match your virtual host. \n`
.underline.red
);
process.exit(1);
});
source.on('error', err => {
log(
`Copy ${localConfigDefault} to ${localConfig} and edit the proxy value to match your virtual host. \n`
.underline.red
);
process.exit(1);
});
}
});
done();
};
//
// Check to see if a .gulp-config.json file exists, if
// not, creates one from .ex-gulp-config.json
//
const setProductionTrue = done => {
production = true;
done();
};
//
// Compile Sass
// Tasks for each sass file
//
const compileSass = (stream, css_dest_path) => {
return (
stream
.pipe(using({ prefix: 'Processing' }))
.pipe(gulpIf(!production, sourcemaps.init()))
.pipe(
gulpStylelint({
// fix: true,
reporters: [{ formatter: 'string', console: true }],
failAfterError: false,
debug: true,
})
)
.pipe(sassGlob())
.pipe(
sass({
includePaths: ['node_modules'],
}).on('error', sass.logError)
)
.pipe(postcss([easingGradients]))
.pipe(postcss([objectFit]))
.pipe(
autoprefixer({
grid: true,
})
)
.pipe(gulpIf(production, mediaQuery()))
.pipe(gulpIf(production, cssnano()))
.pipe(gulpIf(!production, sourcemaps.write('.')))
.pipe(gulp.dest(css_dest_path))
.pipe(browsersync.stream({ match: '**/*.css' }))
);
};
//
// Compile JS with Rollup
//
const compileJS = (src_path, dest_path) => {
return gulp
.src([src_path])
.pipe(using({ prefix: 'Processing' }))
.pipe(gulpIf(!production, sourcemaps.init()))
.pipe(
rollupEach(
{
// external: [],
plugins: [
rollupCommon(),
rollupBabel.babel({
babelHelpers: 'bundled',
exclude: [/\/core-js\//],
}),
rollupResolve.nodeResolve(),
rollupESLint(),
],
},
{
format: 'es',
// globals: {}
},
rollup
)
)
.pipe(gulpIf(!production, sourcemaps.write('.')))
.pipe(gulpIf(production, uglify()))
.pipe(gulp.dest(dest_path));
};
//
// Compile Images
//
const compileImages = (src_path, dest_path) => {
return gulp
.src(src_path)
.pipe(
imagemin([
imagemin.gifsicle({ interlaced: true }),
imagemin.mozjpeg({ progressive: true }),
imagemin.optipng({ optimizationLevel: 5 }),
imagemin.svgo({
plugins: [
{ removeViewBox: true },
{ cleanupIDs: false },
{ cleanupNumericValues: { floatPrecision: 2 } },
],
}),
])
)
.pipe(gulp.dest(dest_path));
};
let watch_manifest = {},
sass_task_manifest = [],
js_task_manifest = [],
image_task_manifest = [];
//
// Task Builder
// ------------
// Create tasks based on inputs
//
// @param task_name {string} Name of task to be used to suffix task types
// @param base_path {string} Directory path for the task to do its thang
//
function task_builder(task_name, base_path) {
// Build task based watch files
let misc_watchers = [];
CONFIG.paths.watch_files.forEach(watch_path => {
misc_watchers.push(base_path + watch_path);
});
watch_manifest[task_name] = {
sass: base_path + CONFIG.paths.sass.watch,
js: base_path + CONFIG.paths.js.watch,
images: base_path + CONFIG.paths.images.main,
misc: misc_watchers,
};
// Setup task names
const sass_task = 'sass:' + task_name;
const js_task = 'js:' + task_name;
const image_task = 'images:' + task_name;
// Push task names into task manifest arrays
sass_task_manifest.push(sass_task);
js_task_manifest.push(js_task);
image_task_manifest.push(image_task);
//
// SASS TASK
// ---------
// Can be run via `sass:TASK_NAME`
//
gulp.task(sass_task, () => {
return gulp
.src(base_path + CONFIG.paths.sass.main)
.pipe(
flatmap(stream => {
return compileSass(stream, base_path + CONFIG.paths.sass.dest);
})
);
});
//
// JS TASK
// -------
// Can be run via `js:TASK_NAME`
//
gulp.task(js_task, (done) => {
let js_dest_path = base_path + CONFIG.paths.js.dest;
if ( !production && watchJsPath ) {
const onlyFiles = watchJsPath.split(',');
const onlyFilePaths = onlyFiles.map(filename => base_path + CONFIG.paths.js.src + '/' + filename);
onlyFilePaths.forEach(function(srcPath, idx) {
return gulp.src(srcPath)
.pipe(gulpEsbuild({
outfile: onlyFiles[idx],
bundle: true,
sourcemap: true,
}))
.pipe(gulp.dest(js_dest_path));
});
return done();
}
return compileJS(
base_path + CONFIG.paths.js.main,
js_dest_path
);
});
//
// MODULE IMAGES TASK
// ------------------
// Can be run via `images:TASK_NAME`
//
gulp.task(image_task, () => {
return compileImages(
base_path + CONFIG.paths.images.main,
base_path + CONFIG.paths.images.dest
);
});
}
// ----------------------------------------------------------------------------
// BUILD MODULE & THEME TASKS
// ----------------------------------------------------------------------------
if ( !watchThemeOnly ) {
CONFIG.project.modules.forEach(mod_dir => {
task_builder('module--' + mod_dir, CONFIG.modules_path + mod_dir + '/');
});
}
CONFIG.project.themes.forEach(theme_dir => {
task_builder('theme--' + theme_dir, CONFIG.themes_path + theme_dir + '/');
});
// ----------------------------------------------------------------------------
// GULP TASKS
// ----------------------------------------------------------------------------
//
// Default Task
//
gulp.task(
'default',
gulp.series(
gulp.parallel(sass_task_manifest, js_task_manifest, image_task_manifest)
)
);
//
// Build Task
//
gulp.task(
'build',
gulp.series(
setProductionTrue, // Runs with production set to true
'default'
)
);
//
// Watch Task
//
gulp.task('watch', () => {
Object.keys(watch_manifest).forEach(function(task) {
const manifest = watch_manifest[task];
gulp.watch(manifest.sass, gulp.series('sass:' + task));
gulp.watch(manifest.js, gulp.series('js:' + task, reload));
gulp.watch(manifest.images, gulp.series('images:' + task));
gulp.watch(manifest.misc, reload);
});
// Fractal
if (CONFIG.fractal.use) {
const fractalSass = CONFIG.fractal.components + '/**/*.scss';
const fractalTwig = CONFIG.fractal.components + '/**/*.twig';
const fractalJS = CONFIG.fractal.components + '/**/*.js';
const fractalJSON = CONFIG.fractal.components + '/**/*.json';
gulp.watch(fractalSass, gulp.series('sass:theme--' + CONFIG.base_theme));
gulp.watch([fractalTwig, fractalJS, fractalJSON], reload);
}
});
gulp.task('watch-args', () => {
if ( watchJsPath ) {
const onlyFiles = watchJsPath.split(',');
const onlyPaths = onlyFiles.map(filename => CONFIG.paths.js.src + '/' + filename);
console.log(onlyPaths);
// console.log(base_path + CONFIG.paths.js.src);
}
})
// ----------------------------------------------------------------------------
// GULP SERVE TASKS
// ----------------------------------------------------------------------------
//
// Init CMS Browsersync Server
//
gulp.task('startsync:cms', cb => {
if (CONFIG.useProxy) {
const g_config = JSON.parse(fs.readFileSync(localConfig));
if (g_config.proxy == null) {
log(`Edit the proxy value in ${localConfig} \n`.underline.red);
process.exit(1);
}
CONFIG.browsersyncOpts['proxy'] = g_config.proxy;
}
browsersync.init(CONFIG.browsersyncOpts, cb);
});
//
// Serve CMS in Browsersync
//
gulp.task(
'serve',
gulp.series(checkGulpConfig, 'default', 'startsync:cms', 'watch')
);
// ----------------------------------------------------------------------------
// FAVICON GENERATOR
// ----------------------------------------------------------------------------
//
// Generate Icons
//
gulp.task('favicon:generate', function(done) {
if (CONFIG.faviconData) {
let funcsCount = CONFIG.faviconData.length;
CONFIG.faviconData.forEach(element => {
realFavicon.generateFavicon(element, function () {
--funcsCount;
if (funcsCount <= 0) {
done();
}
});
})
} else {
done();
}
});
//
// Generate Icon HTML
//
gulp.task('favicon:markup', function(done) {
if (CONFIG.faviconData) {
CONFIG.faviconData.forEach(element => {
gulp
.src([element.html_file])
.pipe(
realFavicon.injectFaviconMarkups(
JSON.parse(fs.readFileSync(element.markupFile)).favicon.html_code
)
)
.pipe(gulp.dest(element.dest));
});
}
done();
});
//
// Run Favicon tasks together
//
gulp.task('favicon', gulp.series('favicon:generate', 'favicon:markup'));
// ----------------------------------------------------------------------------
// FRACTAL
// ----------------------------------------------------------------------------
let fractal;
let logger;
let fractalServer;
if (CONFIG.fractal.use) {
// Init Fractal
fractal = require('./fractal.config.js');
logger = fractal.cli.console;
// Set Fractal Browsersync Options
fractal.web.set('server.syncOptions', {
open: true,
notify: true,
});
// Set Fractal Browsersync Options
fractalServer = fractal.web.server({
sync: true,
});
}
//
// Init Fractal Browsersync Server
//
gulp.task('startsync:fractal', function(done) {
if (!CONFIG.fractal.use) {
return done();
}
fractalServer.on('error', err => {
logger.error(err.message);
// return process.exit(); // Uncomment to stop watch on fail
});
return fractalServer.start().then(() => {
logger.success(`Fractal server is now running at ${fractalServer.url}`);
done();
});
});
//
// Serve Fractal
//
gulp.task(
'fractal',
gulp.series(checkGulpConfig, 'default', 'startsync:fractal', 'watch')
);
//
// Build Fractal
//
gulp.task('fractal:build', function(done) {
if (CONFIG.fractal.use) {
const builder = fractal.web.builder();
builder.on('progress', (completed, total) =>
logger.update(`Exported ${completed} of ${total} items`, 'info')
);
builder.on('error', err => logger.error(err.message));
return builder.build().then(() => {
logger.success('Fractal build completed!');
});
} else {
done();
}
});
// Copy Fractal Build to Netlify Branch
gulp.task('fractal:build:branch', function(done) {
if (CONFIG.fractal.use) {
return gulp.src('./fractal-build/**/*').pipe(
ghPages({
branch: 'netlify',
})
);
} else {
done();
}
});
// Deploy Fractal to Netlify Branch
gulp.task(
'fractal:deploy',
gulp.series('build', 'fractal:build', 'fractal:build:branch')
);