Skip to content

Instantly share code, notes, and snippets.

@dabrattoli
Last active April 27, 2016 18:14
Show Gist options
  • Save dabrattoli/868109bc7b9d1ed6f074 to your computer and use it in GitHub Desktop.
Save dabrattoli/868109bc7b9d1ed6f074 to your computer and use it in GitHub Desktop.
var project = 'slilccc', // Project name, used for build zip.
scssfolder = './wp-content/themes/' + project + '/SCSS',
workingdir = './wp-content/themes/' + project + '/',
cssdir = './wp-content/themes/' + project + '/css/',
buildir = './wp-content/themes/' + project + '_build/',
url = 'lccc.dev', // Local Development URL for BrowserSync. Change as-needed.
gulp = require('gulp'),
browserSync = require('browser-sync'), // Asynchronous browser loading on .scss file changes
reload = browserSync.reload,
autoprefixer = require('gulp-autoprefixer'), // Autoprefixing magic
minifycss = require('gulp-uglifycss'),
filter = require('gulp-filter'),
imagemin = require('gulp-imagemin'),
newer = require('gulp-newer'),
uglify = require('gulp-uglify'),
rename = require('gulp-rename'),
concat = require('gulp-concat'),
notify = require('gulp-notify'),
cache = require('gulp-cache'),
rimraf = require('gulp-cache'),
header = require('gulp-header'),
cleanup = require('gulp-clean'),
zip = require('gulp-zip'), // Using to zip up our packaged theme into a tasty zip file that can be installed in WordPress!
cmq = require('gulp-combine-media-queries'),
sass = require('gulp-sass'),
plumber = require('gulp-plumber'), // Helps prevent stream crashing on errors
debug = require ('gulp-debug'),
sourcemaps = require('gulp-sourcemaps'),
changed = require('gulp-changed'),
buildStyles = [
// include common file types
'./wp-content/themes/' + project + '/**/*.css',
'./wp-content/themes/' + project + '/**/*.js',
'./wp-content/themes/' + project + '/**/*.svg',
'./wp-content/themes/' + project + '/**/*.ttf',
'./wp-content/themes/' + project + '/**/*.otf',
'./wp-content/themes/' + project + '/**/*.eot',
'./wp-content/themes/' + project + '/**/*.woff',
'./wp-content/themes/' + project + '/**/*.woff2',
// include specific files and folders
'screenshot.png',
// exclude files and folders
'!node_modules/**/*',
'!assets/bower_components/**/*',
'!style.css.map',
'!assets/js/custom/*',
'!assets/css/patrials/*'
];
buildStylescss = [
// include common file types
'./wp-content/themes/' + project + '/css/style.css',
// exclude files and folders
'!node_modules/**/*',
'!assets/bower_components/**/*',
'!style.css.map',
'!assets/js/custom/*',
'!assets/css/patrials/*'
];
buildPhp = [
// include common file types
'./wp-content/themes/' + project + '/**/*.php',
'./wp-content/themes/' + project + '/**/*.html',
// include specific files and folders
'screenshot.png',
// exclude files and folders
'!node_modules/**/*',
'!assets/bower_components/**/*',
'!style.css.map',
'!assets/js/custom/*',
'!assets/css/patrials/*'
];
var onError = function (err) {
gutil.beep();
console.log(err);
};
gulp.task('buildheader', function () {
var header = require('gulp-header');
gulp.src(buildir + 'style.css')
.pipe(plumber({
errorHandler: onError
}))
.pipe(header('/*!Theme Name: ${name}\n Theme URI: http://www.lorainccc.edu \n Description: Lorain County Community College Child Theme \n Author: LCCC Web Team \n Author URI: http://www.lorainccc.edu \n Template: lccc-framework \n Version:1.0.0 \n License: GNU General Public License v2 or later \n License URI: http://www.gnu.org/licenses/gpl-2.0.html \n Text Domain: ${name} */ \n ', { name : project+'_build'} ))
.pipe(gulp.dest(buildir))
.pipe(notify({ message: 'Build Header scripts task complete', onLast: true }));
});
gulp.task('workingheader', function () {
var header = require('gulp-header');
gulp.src(cssdir + 'style.css')
.pipe(plumber({
errorHandler: onError
}))
.pipe(header('/*!Theme Name: ${name}\n Theme URI: http://www.lorainccc.edu \n Description: Lorain County Community College Child Theme \n Author: LCCC Web Team \n Author URI: http://www.lorainccc.edu \n Template: lccc-framework \n Version:1.0.0 \n License: GNU General Public License v2 or later \n License URI: http://www.gnu.org/licenses/gpl-2.0.html \n Text Domain: ${name} */ \n ', { name : project } ))
.pipe(gulp.dest(workingdir))
.pipe(notify({ message: 'Working Header scripts task complete', onLast: true }));
});
/**
* Scripts: Javascripts
*
* Look at src/js and uglify those files, send them to buiild directory.
*/
gulp.task('Js', function() {
return gulp.src(['./wp-content/themes/' + project + '/**/*.js'])
/* .pipe(concat('vendors.js'))
.pipe(gulp.dest('./assets/js'))
.pipe(rename( {
basename: "vendors",
suffix: '.min'
}))*/
.pipe(uglify())
.pipe(newer(buildir))
.pipe(gulp.dest(buildir))
.pipe(notify({ message: 'Vendor scripts task complete', onLast: true }));
});
/**
* Build task that moves essential theme files for production-ready sites
*
* buildFiles copies all the files in buildInclude to build folder - check variable values at the top
* buildImages copies all the images from img folder in assets while ignoring images inside raw folder if any
*/
gulp.task('buildStyles', function() {
return gulp.src(buildStyles)
.pipe(newer(buildir))
.pipe(plumber({
errorHandler: onError
}))
.pipe(gulp.dest(buildir))
.pipe(notify({ message: 'Copy from buildStyles complete', onLast: true }));
});
gulp.task('maincCSS', function() {
return gulp.src(buildStylescss)
.pipe(newer(buildir))
.pipe(plumber({
errorHandler: onError
}))
.pipe(gulp.dest(buildir))
.pipe(notify({ message: 'Copy for mainCSS complete', onLast: true }));
});
gulp.task('workingStyle', function() {
return gulp.src(cssdir + 'style.css')
.pipe(gulp.dest(workingdir))
.pipe(notify({ message: 'Copy from workingStyles complete', onLast: true }));
});
gulp.task('buildPhp', function() {
return gulp.src(buildPhp)
.pipe(newer(buildir))
.pipe(plumber({
errorHandler: onError
}))
.pipe(gulp.dest(buildir))
.pipe(notify({ message: 'Copy from buildPhp complete', onLast: true }));
});
/**
* Styles
*
* Looking at src/sass and compiling the files into Expanded format, Autoprefixing and sending the files to the build folder
*
* Sass output styles: https://web-design-weekly.com/2014/06/15/different-sass-output-styles/
*/
gulp.task('styles', function () {
return gulp.src('./wp-content/themes/' + project + '/SCSS/style.scss')
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(sass({
errLogToConsole: true,
//outputStyle: 'compressed',
outputStyle: 'compact',
// outputStyle: 'nested',
// outputStyle: 'expanded',
precision: 10
}))
.pipe(sourcemaps.write({includeContent: false}))
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(autoprefixer('last 2 version', '> 1%', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(sourcemaps.write('.'))
.pipe(plumber.stop())
.pipe(gulp.dest(cssdir))
.pipe(filter('**/*.css')) // Filtering stream to only css files
.pipe(cmq()) // Combines Media Queries
.pipe(reload({stream:true})) // Inject Styles when style file is created
// .pipe(rename({ suffix: '.min' }))
.pipe(minifycss({
maxLineLen: 80
}))
.pipe(gulp.dest(cssdir))
.pipe(reload({stream:true})) // Inject Styles when min style file is created
.pipe(notify({ message: 'Styles task complete', onLast: true }))
});
/**
* Images
*
* Look at src/images, optimize the images and send them to the appropriate place
*/
gulp.task('images', function() {
// Add the newer pipe to pass through newer images only
return gulp.src(['./wp-content/themes/' + project + '/**/*.{png,jpg,gif}'])
.pipe(newer('./wp-content/themes/' + project + '_build/images'))
// .pipe(rimraf({ force: true }))
.pipe(imagemin({ optimizationLevel: 10, progressive: true, interlaced: true }))
.pipe(gulp.dest('./wp-content/themes/' + project + '_build/'))
.pipe( notify( { message: 'Images task complete', onLast: true } ) );
});
/**
* Clean gulp cache
*/
gulp.task('clear', function () {
cache.clearAll();
});
/**
* Clean tasks for zip
*
* Being a little overzealous, but we're cleaning out the build folder, codekit-cache directory and annoying DS_Store files and Also
* clearing out unoptimized image files in zip as those will have been moved and optimized
*/
gulp.task('cleanup', function() {
return gulp.src(['./assets/bower_components', '**/.sass-cache','**/.DS_Store'], { read: false }) // much faster
.pipe(ignore('node_modules/**')) //Example of a directory to ignore
.pipe(rimraf({ force: true }))
.pipe(notify({ message: 'Clean task complete', onLast: true }));
});
/**
* Zipping build directory for distribution
*
* Taking the build folder, which has been cleaned, containing optimized files and zipping it up to send out as an installable theme
*/
gulp.task('buildZip', function () {
// return gulp.src([build+'/**/', './.jshintrc','./.bowerrc','./.gitignore' ])
return gulp.src(buildir+'/**/')
.pipe(zip(project+'.zip'))
.pipe(gulp.dest('./finished_themes/'))
.pipe(notify({ message: 'Zip task complete', onLast: true }));
});
/**
* Watch Task
**/
gulp.task('watch', function () {
gulp.watch('./wp-content/themes/' + project + '/**/*.scss', ['styles','workingStyle']);
gulp.watch('./wp-content/themes/' + project + '/style.css', ['workingheader'])
gulp.watch('./wp-content/themes/' + project + '/**/style.css', ['maincCSS']);
gulp.watch('./wp-content/themes/' + project + '_build/style.css', ['buildheader'])
gulp.watch('./wp-content/themes/' + project + '/**/*.php', ['buildPhp']);
gulp.watch('./wp-content/themes/' + project + '/images/**/*.jpg', ['images']);
gulp.watch('./wp-content/themes/' + project + '/images/**/*.png', ['images']);
gulp.watch('./wp-content/themes/' + project + '/images/**/*.gif', ['images']);
gulp.watch('./wp-content/themes/' + project + '/**/*.js', ['Js']);
});
gulp.task('default', ['watch']);
{
"name": "lccc_gulpdev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"browser-sync": "^2.10.0",
"gulp": "^3.9.0",
"gulp-autoprefixer": "^3.1.0",
"gulp-cache": "^0.4.0",
"gulp-changed": "^1.3.0",
"gulp-clean": "^0.3.1",
"gulp-combine-media-queries": "^0.2.0",
"gulp-concat": "^2.6.0",
"gulp-debug": "^2.1.2",
"gulp-filter": "^3.0.1",
"gulp-header": "^1.7.1",
"gulp-iconfont": "^5.0.0",
"gulp-image-optimization": "^0.1.3",
"gulp-imagemin": "^2.4.0",
"gulp-newer": "^1.0.0",
"gulp-notify": "^2.2.0",
"gulp-plumber": "^1.0.1",
"gulp-rename": "^1.2.2",
"gulp-sass": "^2.1.0",
"gulp-sourcemaps": "^1.6.0",
"gulp-uglify": "^1.4.2",
"gulp-uglifycss": "^1.0.4",
"gulp-zip": "^3.0.2"
},
"dependencies": {
"gulp-header": "^1.7.1",
"gulp-uglifycss": "^1.0.4",
"imagemin": "^3.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment