Skip to content

Instantly share code, notes, and snippets.

@AndreKelling
Created October 12, 2018 07:16
Show Gist options
  • Save AndreKelling/0a3402b564c41eb468bc70dc6427e4c3 to your computer and use it in GitHub Desktop.
Save AndreKelling/0a3402b564c41eb468bc70dc6427e4c3 to your computer and use it in GitHub Desktop.
A gulp setup for wordpress
/* set theme name and paths */
const themeName = 'theme',
wpDir = './wp/',
themeDir = wpDir+'wp-content/themes/'+themeName+'/',
jsDir = themeDir+'js/',
blocksDir = themeDir+'blocks/',
scssDir = themeDir+'scss/',
imagesOrigDir = themeDir+'images-original/',
imagesDir = themeDir+'images/',
iconsOrigDir = themeDir+'icons/svg/',
iconsDir = themeDir+'icons/'
;
/* global module, require */
const gulp = require('gulp'),
$ = require('gulp-load-plugins')({
pattern: ['gulp-*']
});
const imageminJpegRecompress = require('imagemin-jpeg-recompress');
const imageminPngquant = require('imagemin-pngquant');
const runSequence = require('run-sequence');
/** serve **/
gulp.task('serve', ['build'], function() {
gulp.watch(scssDir+'**/*.scss', ['scss', 'stylelint']);
gulp.watch([jsDir+'index.js', blocksDir+'**/index.js'], ['eslint']);
gulp.watch(imagesOrigDir+'**/*', ['images']);
gulp.watch(iconsOrigDir+'**/*', ['icons']);
});
/** styles **/
gulp.task('scss', function () {
gulp.src([
scssDir+'style.scss'
])
.pipe($.sourcemaps.init())
.pipe($.sass().on('error', $.sass.logError))
.pipe($.sourcemaps.write(themeDir))
.pipe(gulp.dest(themeDir));
});
gulp.task('stylelint', function () {
gulp.src([
scssDir+'**/*.scss'
])
.pipe($.stylelint({
failAfterError: false,
reporters: [
{formatter: 'string', console: true}
]
}))
});
/** scripts **/
gulp.task('eslint', function () {
gulp.src([
jsDir+'index.js',
blocksDir+'**/index.js'
])
.pipe($.eslint())
.pipe($.eslint.format());
});
/** images **/
gulp.task('images', function() {
'use strict';
return gulp.src(imagesOrigDir+'**/*')
.pipe($.cache($.imagemin([
imageminJpegRecompress({
loops:4,
min: 50,
max: 95,
quality:'high'
}),
imageminPngquant()
])))
.pipe(gulp.dest(imagesDir));
});
/** icons **/
gulp.task('icons', function() {
return gulp
.src(iconsOrigDir+'*.svg')
.pipe($.svgSymbols({
svgAttrs: {
class: 'svg-icon-lib',
'aria-hidden': 'true',
'data-enabled': true,
}
}))
.pipe(gulp.dest(iconsDir))
});
/** default - runs serve for development work (serve calls build initially) **/
gulp.task('default', ['serve']);
/** build - prepares and updates all assets **/
gulp.task('prepare', ['eslint', 'images', 'icons', 'scss']);
// no need for run sequence, but will let stay / no need for prepare also left over from service-worker stuff
gulp.task('build', function() {
runSequence('prepare');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment