Skip to content

Commit 8de6f55

Browse files
author
Jan Lehtinen
committed
add gulp4, refactor gulpfile for es6
1 parent 5ad818d commit 8de6f55

File tree

5 files changed

+608
-302
lines changed

5 files changed

+608
-302
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*.bak
99
*.swp
1010
*.tmp
11+
.vscode
1112

1213
bower_components/*
1314
!bower_components/.gitkeep

assets/last-edited.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"js": 1554373157703,
3-
"css": 1554443013364,
3+
"css": 1557934496204,
44
"svg": 1554364154832
55
}

gulpfile.js

100755100644
+107-88
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,39 @@
55
/**
66
* Site config
77
*/
8-
var manifest = require('./assets/manifest.js');
9-
var timestamps = require('./assets/last-edited.json');
8+
const manifest = require('./assets/manifest.js');
9+
const timestamps = require('./assets/last-edited.json');
1010

1111
/**
1212
* Global modules
1313
*/
14-
var argv = require('minimist')(process.argv.slice(2));
15-
var autoprefixer = require('gulp-autoprefixer');
16-
var beeper = require('beeper');
17-
var browsersync = require('browser-sync').create();
18-
var concat = require('gulp-concat');
19-
var flatten = require('gulp-flatten');
20-
var gulp = require('gulp');
21-
var gulpif = require('gulp-if');
22-
var imagemin = require('gulp-imagemin');
23-
var jshint = require('gulp-jshint');
24-
var lazypipe = require('lazypipe');
25-
var merge = require('merge-stream');
26-
var cssnano = require('gulp-cssnano');
27-
var plumber = require('gulp-plumber');
28-
var runsequence = require('run-sequence');
29-
var sass = require('gulp-sass');
30-
var sourcemaps = require('gulp-sourcemaps');
31-
var uglify = require('gulp-uglify');
32-
var rename = require('gulp-rename');
33-
var svgmin = require('gulp-svgmin');
34-
var svgstore = require('gulp-svgstore');
35-
var file = require('gulp-file');
14+
const argv = require('minimist')(process.argv.slice(2));
15+
const autoprefixer = require('gulp-autoprefixer');
16+
const beeper = require('beeper');
17+
const browsersync = require('browser-sync').create();
18+
const concat = require('gulp-concat');
19+
const flatten = require('gulp-flatten');
20+
const { series, paralell } = require('gulp');
21+
const gulpif = require('gulp-if');
22+
const imagemin = require('gulp-imagemin');
23+
const jshint = require('gulp-jshint');
24+
const lazypipe = require('lazypipe');
25+
const merge = require('merge-stream');
26+
const cssnano = require('gulp-cssnano');
27+
const plumber = require('gulp-plumber');
28+
const runsequence = require('run-sequence');
29+
const sass = require('gulp-sass');
30+
const sourcemaps = require('gulp-sourcemaps');
31+
const uglify = require('gulp-uglify');
32+
const rename = require('gulp-rename');
33+
const svgmin = require('gulp-svgmin');
34+
const svgstore = require('gulp-svgstore');
35+
const file = require('gulp-file');
3636

3737
/**
3838
* Asset paths
3939
*/
40-
var path = {
40+
const path = {
4141
"base" : {
4242
"source": "assets/",
4343
"dist": "dist/",
@@ -71,7 +71,7 @@ var path = {
7171
/**
7272
* Disable or enable features
7373
*/
74-
var enabled = {
74+
const enabled = {
7575

7676
// disable source maps when `--production`
7777
maps: !argv.production,
@@ -92,7 +92,7 @@ var enabled = {
9292
*
9393
* Update asset class timestamp to last-edited.json
9494
*/
95-
var updateTimestamp = function updateTimestamp(stamp) {
95+
const updateTimestamp = (stamp) => {
9696

9797
timestamps[stamp] = Date.now();
9898

@@ -114,10 +114,10 @@ var updateTimestamp = function updateTimestamp(stamp) {
114114
* 'globs': 'assets/main.scss,assets/print.scss'
115115
* }
116116
*/
117-
var buildAssets = function(buildFiles) {
117+
const buildAssets = (buildFiles) => {
118118

119-
var result = [];
120-
for (var buildFile in buildFiles) {
119+
let result = [];
120+
for (let buildFile in buildFiles) {
121121

122122
// set correct asset paths
123123
for (i = 0; i < buildFiles[buildFile].length; i++) {
@@ -133,8 +133,8 @@ var buildAssets = function(buildFiles) {
133133

134134
};
135135

136-
var jsAssets = buildAssets(manifest.js());
137-
var cssAssets = buildAssets(manifest.css());
136+
const jsAssets = buildAssets(manifest.js());
137+
const cssAssets = buildAssets(manifest.css());
138138

139139
/**
140140
* Process: CSS
@@ -145,7 +145,7 @@ var cssAssets = buildAssets(manifest.css());
145145
* .pipe(cssTasks('main.css')
146146
* .pipe(gulp.dest(path.base.dist + 'styles'))
147147
*/
148-
var cssTasks = function(filename) {
148+
const cssTasks = (filename) => {
149149

150150
return lazypipe()
151151

@@ -204,7 +204,7 @@ var cssTasks = function(filename) {
204204
* .pipe(gulp.dest(path.base.dist + 'scripts'))
205205
* ```
206206
*/
207-
var jsTasks = function(filename) {
207+
const jsTasks = filename => {
208208

209209
updateTimestamp('js');
210210

@@ -241,18 +241,18 @@ var jsTasks = function(filename) {
241241
* By default this task will only log a warning if a precompiler error is
242242
* raised. If the `--production` flag is set: this task will fail outright.
243243
*/
244-
gulp.task('styles', [], function() {
244+
function styles() {
245245

246-
var merged = merge();
246+
const merged = merge();
247247

248248
// update last-edited.json
249249
updateTimestamp('css');
250250

251251
// process all assets
252252
for (i = 0; i < cssAssets.length; i++) {
253253

254-
var asset = cssAssets[i];
255-
var cssTasksInstance = cssTasks(asset.name);
254+
let asset = cssAssets[i];
255+
const cssTasksInstance = cssTasks(asset.name);
256256

257257
// handle possible errors
258258
if (!enabled.failStyleTask) {
@@ -275,24 +275,24 @@ gulp.task('styles', [], function() {
275275
.pipe(gulp.dest(path.styles.dist))
276276
.pipe(browsersync.stream({match: '**/*.css'}));
277277

278-
});
278+
};
279279

280+
exports.styles = styles;
280281

281282
/**
282283
* Task: Scripts
283284
*
284285
* `gulp scripts` - Runs JSHint then compiles, combines, and optimizes JS.
285286
*/
286-
gulp.task('scripts', ['jshint'], function() {
287-
288-
var merged = merge();
287+
function scripts() {
288+
const merged = merge();
289289

290290
// process all assets
291291
for (i = 0; i < jsAssets.length; i++) {
292292

293-
var asset = jsAssets[i];
293+
let asset = jsAssets[i];
294294

295-
var jsTasksInstance = jsTasks(asset.name);
295+
const jsTasksInstance = jsTasks(asset.name);
296296

297297
merged.add(
298298
gulp.src(asset.globs, {base: 'scripts'})
@@ -309,16 +309,17 @@ gulp.task('scripts', ['jshint'], function() {
309309
.pipe(gulp.dest(path.scripts.dist))
310310
.pipe(browsersync.stream({match: '**/*.js'}));
311311

312-
});
312+
};
313+
314+
exports.scripts = scripts;
313315

314316
/**
315317
* Task: Fonts
316318
*
317319
* `gulp fonts` - Grabs all the fonts and outputs them in a flattened directory
318320
* structure. See: https://github.com/armed/gulp-flatten
319321
*/
320-
gulp.task('fonts', function() {
321-
322+
function fonts() {
322323
return gulp.src([path.fonts.source + '**/*'])
323324

324325
// flatten directory structures
@@ -330,14 +331,15 @@ gulp.task('fonts', function() {
330331
// browsersync result
331332
.pipe(browsersync.stream());
332333

333-
});
334+
};
335+
exports.fonts = fonts;
334336

335337
/**
336338
* Task: Images
337339
*
338340
* `gulp images` - Run lossless compression on all the images.
339341
*/
340-
gulp.task('images', function() {
342+
function images() {
341343

342344
return gulp.src([path.images.source + '**/*'])
343345

@@ -354,14 +356,16 @@ gulp.task('images', function() {
354356
// browsersync result
355357
.pipe(browsersync.stream());
356358

357-
});
359+
};
360+
361+
exports.images = images;
358362

359363
/**
360364
* Task: Svgstore
361365
*
362366
* Create a single sprite.svg file from files in /assets/sprite/.
363367
*/
364-
gulp.task('svgstore', function () {
368+
function svgstore() {
365369

366370
updateTimestamp('svg');
367371

@@ -380,18 +384,21 @@ gulp.task('svgstore', function () {
380384
// browsersync result
381385
.pipe(browsersync.stream());
382386

383-
});
387+
};
388+
389+
exports.svgstore = svgstore;
384390

385391
/**
386392
* Task: JSHint
387393
*
388394
* `gulp jshint` - Lints configuration JSON and project JS.
389395
*/
390-
gulp.task('jshint', function() {
391396

392-
var allJS = [];
397+
function jshint() {
398+
399+
let allJS = [];
393400
for (i = 0; i < jsAssets.length; i++) {
394-
var globsArray = jsAssets[i].globs;
401+
let globsArray = jsAssets[i].globs;
395402
for (j = 0; j < globsArray.length; j++) {
396403
allJS.push(globsArray[j]);
397404
}
@@ -404,14 +411,16 @@ gulp.task('jshint', function() {
404411
.pipe(jshint.reporter('jshint-stylish'))
405412
.pipe(gulpif(enabled.failJSHint, jshint.reporter('fail')));
406413

407-
});
414+
};
415+
416+
exports.jshint = jshint;
408417

409418
/**
410419
* Task: Favicon
411420
*
412421
* `gulp favicon` - Run lossless compression for favicons.
413422
*/
414-
gulp.task('favicon', function() {
423+
function favicon {
415424

416425
return gulp.src([path.favicon.source + '**/*'])
417426

@@ -429,14 +438,20 @@ gulp.task('favicon', function() {
429438
// send to /dist/favicon
430439
.pipe(gulp.dest(path.favicon.dist));
431440

432-
});
441+
};
442+
443+
exports.favicon = favicon;
433444

434445
/**
435446
* Task: Clean
436447
*
437448
* `gulp clean` - Deletes the build folder entirely.
438449
*/
439-
gulp.task('clean', require('del').bind(null, [path.base.dist]));
450+
function clean() {
451+
require('del').bind(null, [path.base.dist]));
452+
}
453+
454+
exports.clean = clean;
440455

441456
/**
442457
* Task: Watch
@@ -447,35 +462,39 @@ gulp.task('clean', require('del').bind(null, [path.base.dist]));
447462
* build step for that asset and inject the changes into the page.
448463
* See: http://www.browsersync.io
449464
*/
450-
gulp.task('watch', function() {
451-
var new_tab = 'local';
452-
if(argv.q) {
453-
new_tab = false;
454-
}
455-
// browsersync changes
456-
browsersync.init({
457-
files: [
458-
'{inc,partials,template-tags}/**/*.php',
459-
'*.php'
460-
],
461-
proxy: manifest.devUrl(),
462-
snippetOptions: {
463-
whitelist: ['/wp-admin/admin-ajax.php'],
464-
blacklist: ['/wp-admin/**']
465-
},
466-
open: new_tab
467-
});
468-
469-
// watch these files
470-
gulp.watch([path.styles.source + '**/*'], ['styles']);
471-
gulp.watch([path.scripts.source + '**/*'], ['jshint', 'scripts']);
472-
gulp.watch([path.fonts.source + '**/*'], ['fonts']);
473-
gulp.watch([path.images.source + '**/*'], ['images']);
474-
gulp.watch([path.sprite.source + '*'], ['svgstore']);
475-
gulp.watch([path.favicon.source + '*'], ['favicon']);
476-
gulp.watch(['assets/manifest.js'], ['build']);
477-
478-
});
465+
// function watch() {
466+
// const new_tab = 'local';
467+
// if(argv.q) {
468+
// new_tab = false;
469+
// }
470+
// // browsersync changes
471+
// browsersync.init({
472+
// files: [
473+
// '{inc,partials,template-tags}/**/*.php',
474+
// '*.php'
475+
// ],
476+
// proxy: manifest.devUrl(),
477+
// snippetOptions: {
478+
// whitelist: ['/wp-admin/admin-ajax.php'],
479+
// blacklist: ['/wp-admin/**']
480+
// },
481+
// open: new_tab
482+
// });
483+
484+
// // watch these files
485+
watch([path.styles.source + '**/*'], ['styles']);
486+
// gulp.watch([path.scripts.source + '**/*'], ['jshint', 'scripts']);
487+
// gulp.watch([path.fonts.source + '**/*'], ['fonts']);
488+
// gulp.watch([path.images.source + '**/*'], ['images']);
489+
// gulp.watch([path.sprite.source + '*'], ['svgstore']);
490+
// gulp.watch([path.favicon.source + '*'], ['favicon']);
491+
// gulp.watch(['assets/manifest.js'], ['build']);
492+
493+
// });
494+
495+
exports.build = series(clean, parallel(styles, scripts));
496+
497+
// exports.watch = watch;
479498

480499
/**
481500
* Task: Build

0 commit comments

Comments
 (0)