Skip to content

Instantly share code, notes, and snippets.

@niksudan
Last active August 29, 2015 14:23
Show Gist options
  • Save niksudan/2e925805157b4565a688 to your computer and use it in GitHub Desktop.
Save niksudan/2e925805157b4565a688 to your computer and use it in GitHub Desktop.
Gulpfile [WordPress]
/**
* gulp-wordpress
* Nik Sudan
*
* Commands:
* gulp Build the files once
* gulp watch Watch for file changes and perform the correct tasks
* gulp styles Compiles SASS and injects the new CSS into your webpage
* gulp scripts Concats and uglifies js/src into one file
* gulp images Compresses images
*/
// Project options
var options = {
'browser-sync': true,
'domain': 'test.dev',
'js-file': 'main.js'
}
// Dependencies
var gulp = require('gulp'),
sass = require('gulp-ruby-sass'),
autoprefixer = require('gulp-autoprefixer'),
minify = require('gulp-minify-css'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
imagemin = require('gulp-imagemin');
if (options['browser-sync']) {
var browserSync = require('browser-sync').create();
}
/**
* Run the tasks once
*/
gulp.task('default', ['styles', 'scripts', 'images']);
/**
* Watch for file changes and perform the correct tasks
*/
gulp.task('watch', function()
{
if (options['browser-sync']) {
browserSync.init({
proxy: options['domain']
});
console.log('Running browser-sync from proxy "' + options['domain'] + '"');
} else {
console.log('Not running browser-sync');
}
console.log('Compiling JS to file "' + options['js-file'] + '"');
gulp.watch('scss/**/*.scss', ['styles']);
if (options['browser-sync']) {
gulp.watch('js/src/**/*.js', ['scripts']).on('change', browserSync.reload);
gulp.watch('**/*.php').on('change', browserSync.reload);
gulp.watch('images/**/*', ['images']);
} else {
gulp.watch('js/src/**/*.js', ['scripts']);
gulp.watch('images/**/*', ['images']);
}
});
/**
* Compiles SASS and injects the new CSS into your webpage
*/
gulp.task('styles', function()
{
var process = sass('scss')
.pipe(autoprefixer())
.pipe(minify())
.pipe(gulp.dest('css'));
if (options['browser-sync']) {
process = process.pipe(browserSync.stream());
}
return process;
});
/**
* Concats and uglifies js/src into one file
*/
gulp.task('scripts', function()
{
return gulp.src('js/src/**/*.js')
.pipe(concat(options['js-file']))
.pipe(uglify())
.pipe(gulp.dest('js'));
});
/**
* Compresses images
*/
gulp.task('images', function()
{
var process = gulp.src('images/**/*')
.pipe(imagemin({
optimizationLevel: 7,
progressive: true,
interlaced: true
}))
.pipe(gulp.dest('images'));
if (options['browser-sync']) {
process = process.pipe(browserSync.stream());
}
return process;
});
{
"name": "gulp-wordpress",
"version": "1.0.0",
"description": "Gulp files required for use with WordPress. Requires gulp.",
"main": "gulpfile.js",
"devDependencies": {
"browser-sync": "^2.7.6",
"gulp-autoprefixer": "^2.3.0",
"gulp-concat": "^2.5.2",
"gulp-imagemin": "^2.2.1",
"gulp-minify-css": "^1.1.4",
"gulp-rename": "^1.2.2",
"gulp-ruby-sass": "^1.0.5",
"gulp-uglify": "^1.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment