Skip to content

Instantly share code, notes, and snippets.

@jaclyntan
Created December 27, 2016 20:13
Show Gist options
  • Save jaclyntan/10241f2b0a282bc8bda928f043777af5 to your computer and use it in GitHub Desktop.
Save jaclyntan/10241f2b0a282bc8bda928f043777af5 to your computer and use it in GitHub Desktop.
One gulpfile to rule them all: Wordpress edition
"use strict";
// . . . .
// ,`,`,`,`,
//. . . . `\`\`\`\;
//`\`\`\`\`, ~|;!;!;\!
// ~\;\;\;\|\ (--,!!!~`! .
//(--,\\\===~\ (--,|||~`! ./
// (--,\\\===~\ `,-,~,=,:. _,//
// (--,\\\==~`\ ~-=~-.---|\;/J,
// (--,\\\((```==. ~'`~/ a |
// (-,.\\('('(`\\. ~'=~| \_. \
// (,--(,(,(,'\\. ~'=| \\_;> -- Say you have a folder containing subfolders of wordpress installs.
// (,-( ,(,(,;\\ ~=/ \ Installing node modules for each project is a pain in the butt.
// (,-/ (.(.(,;\\,/ ) Hence we run gulp from the parent directory like so, making sure to specify the subdirectory's folder name:
// (,--/,;,;,;,\\ ./------. gulp --project=__SUBDIRECTORYNAME__
// (==,-;-'`;' /_,----`. \
// ,.--_,__.-' `--. ` \
// (='~-_,--/ , ,!,___--. \ \_)
// (-/~( | \ ,_- | ) /_|
// (~/((\ )\._, |-' _,/ /
// \\)))) / ./~. | \_\;
// ,__///// / / ) /
// '===~' | | (, <.
// / / \. \
// _/ / \_\
// /_!/ >_\
var gulp = require('gulp'),
browserSync = require('browser-sync'),
plumber = require('gulp-plumber'),
gutil = require('gulp-util'),
rename = require('gulp-rename'),
imagemin = require('gulp-imagemin'),
sass = require('gulp-sass'),
cleanCSS = require('gulp-clean-css'),
autoprefixer = require('gulp-autoprefixer'),
sourcemaps = require('gulp-sourcemaps'),
csscomb = require('gulp-csscomb'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
bower = require('main-bower-files'),
// bowerNormalizer = require('gulp-bower-normalize'),
shell = require('gulp-shell'),
args = require('yargs').argv,
//set cwd
setcwd = args.project ? args.project : './',
url = "localhost:8888/" + setcwd, //change proxy according to computer settings
cwd = setcwd + "/wp-content/themes/jt"; //change theme according to project settings...or add as another arg
//set up the bs
gulp.task('browser-sync', function () {
browserSync({
proxy: url
});
gutil.log(gutil.colors.blue.bold('� � Welcome to the wonderful bs'),gutil.colors.magenta('world of unicorns ( ˘ ³˘)� � '));
});
//set up the bs reloads
gulp.task('bs-reload', function () {
browserSync.reload();
gutil.log(gutil.colors.yellow.bgBlue('⭐ Did you know unicorns love a refreshing waterfall? ⭐'));
});
////install, update, prune npm packages
//in the event a gulpfile dependency is deleted from package.json, run npm install so this actually works again
gulp.task('npm', function () {
return gulp.src('./package.json')
.pipe(shell(['npm install && npm update && npm prune']))
.on('end', function() {
gutil.log(gutil.colors.black('� All unicorns in the darklands agree - the more inception the better � '));
})
});
gulp.task('bowerinstall', function () {
return gulp.src(cwd + "/bower.json")
.pipe(shell(['cd ' + cwd + '&& bower install && bower update && bower prune']))
.on('end', function() {
gutil.log(gutil.colors.red('� Bower birds are every unicorn\'s annoying best friend � '));
})
});
//optimise images
gulp.task('images', function () {
return gulp.src([cwd + '/images/dump/**/*'])
.pipe(imagemin({
optimizationLevel: 5,
progressive: true,
interlaced: true,
svgoPlugins: [ {removeViewBox:false}, {removeUselessStrokeAndFill:false}],
verbose: true
}))
.pipe(gulp.dest(cwd + '/images/'))
.on('end', function() {
gutil.log(gutil.colors.blue('� Unicorn tears create the highest quality jpegs �'));
})
});
//process scss folder
gulp.task('scss', function () {
return gulp.src([cwd + '/scss/**/*.scss'])
.pipe(plumber({
errorHandler: function (error) {
gutil.log(gutil.colors.red('� Every time this appears a unicorn\'s horn falls off �'));
gutil.log(gutil.colors.red.bold(error.message));
this.emit('end');error.message
}
}))
.pipe(sourcemaps.init())
.pipe(sass({ outputStyle: 'expanded' }))
.pipe(autoprefixer())
.pipe(csscomb())
.pipe(sourcemaps.write(cwd))
.pipe(gulp.dest(cwd))
.pipe(browserSync.reload({
stream: true
}))
})
gulp.task('styles',['scss'], function () {
return gulp.src([cwd + '/style.css'])
//future note: add concat to process any straight css files
.pipe(rename({ suffix: '.min' }))
.pipe(cleanCSS({ debug: true }, function (details) {
gutil.log(gutil.colors.red(details.name + '(original file size): ' + details.stats.originalSize));
gutil.log(gutil.colors.green(details.name + '(minified file size): ' + details.stats.minifiedSize));
}))
.pipe(gulp.dest(cwd))
.on('end', function() {
gutil.log(gutil.colors.white('� Unicorns need them'),gutil.colors.blue('s'),gutil.colors.yellow('t'),gutil.colors.green('y'),gutil.colors.gray('l'),gutil.colors.red('e'),gutil.colors.cyan('s'),gutil.colors.white('to turn into dragons �'));
})
})
//process scripts
//get the js bower files and do a � in main js folder
//remember to remove unnecessary scripts in js/vendor when you get a chance
gulp.task('bower',['bowerinstall'], function() {
return gulp.src(bower({includeDev: true,filter:['**/*.js', '!**/*.min.js', '!**/*/bootstrap.js' , '!**/*/jquery.js'], paths: cwd }))
// .pipe(bowerNormalizer({bowerJson: 'bower.json'}))
.pipe(gulp.dest(cwd + '/js/vendor'))
})
//process js folder
gulp.task('scripts', function () {
return gulp.src([cwd + '/js/**/*.js'])
.pipe(plumber({
errorHandler: function (error) {
gutil.log(gutil.colors.red('� Every time this appears a unicorn\'s horn falls off �'));
gutil.log(gutil.colors.red.bold(error.message));
this.emit('end');
}
}))
.pipe(concat('script.js'))
.pipe(gulp.dest(cwd))
.pipe(rename({
suffix: '.min'
}))
.pipe(uglify())
.pipe(gulp.dest(cwd))
.pipe(browserSync.reload({
stream: true
}))
.on('end', function() {
gutil.log(gutil.colors.green('� A unicorn\'s mane can only flow majestically if it shampoos at least twice a day �'));
})
});
//magic happens
gulp.task('default', ['browser-sync'], function () {
console.log(cwd + '/bower.json');
gulp.watch(cwd + "/bower.json", ['bower']);
gulp.watch('./package.json', ['npm']);
gulp.watch(cwd + "/images/dump/**/*", ['images']);
gulp.watch(cwd + "/scss/**/*.scss", ['styles']);
gulp.watch(cwd + "/js/**/*.js", ['scripts']);
gulp.watch(cwd + "/**/*.html", ['bs-reload']);
gulp.watch(cwd + "/**/*.php", ['bs-reload']);
gulp.watch(cwd + "/**.js", ['bs-reload']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment