Skip to content

Instantly share code, notes, and snippets.

@mschmidty
Last active December 3, 2021 23:24
Show Gist options
  • Save mschmidty/f639c6565f3de4aa6af579125c4a0585 to your computer and use it in GitHub Desktop.
Save mschmidty/f639c6565f3de4aa6af579125c4a0585 to your computer and use it in GitHub Desktop.
My gulp setup for wordpress sites with Browsersync and svgstore
const { dest, src, series, watch, parallel } = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const rename = require('gulp-rename');
const autoprefixer = require('autoprefixer')
const sourcemaps = require('gulp-sourcemaps')
const postcss = require('gulp-postcss')
const cssnano = require('cssnano');
const babel = require('gulp-babel');
const concat = require('gulp-concat');
const svgstore = require('gulp-svgstore');
const svgmin = require('gulp-svgmin');
const path = require('path');
const imagemin = require('gulp-imagemin');
const browserSync = require('browser-sync');
//cssnano plugins
var plugins = [
autoprefixer(),
cssnano()
];
function styles() {
return src('./src/sass/**/*.sass')
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(postcss(plugins))
.pipe(rename('style.css'))
.pipe(sourcemaps.write('.'))
.pipe(dest('./'))
.pipe(browserSync.stream());
};
function js() {
return src('./src/js/*.js', { sourcemaps: true })
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(concat('main.min.js'))
.pipe(dest('./js', { sourcemaps: true }))
.pipe(browserSync.stream());
}
function svgStore() {
return src('src/svg/*.svg')
.pipe(svgmin(function (file) {
var prefix = path.basename(file.relative, path.extname(file.relative));
return {
plugins: [{
cleanupIDs: {
prefix: prefix + '-',
minify: true
}
}]
}
}))
.pipe(svgstore({ inlineSvg: true }))
.pipe(rename('svg.php'))
.pipe(dest('template-parts'))
.pipe(browserSync.stream());
}
function images(){
return src('src/images/**/**')
.pipe(imagemin([
imagemin.mozjpeg({quality:75, progressive:true}),
imagemin.optipng({optimizationLevel: 5})
]))
.pipe(dest('./images'))
}
function watchFiles(){
watch('./src/sass/**/*', styles);
watch('./src/svg/*', svgStore);
watch('./src/js/*', js);
watch('*.php').on('change', browserSync.reload);
}
function browser() {
browserSync.init({
proxy: 'localhost:8000',
port: 5000
});
}
function reload(){
browserSync.reload();
done();
}
exports.styles = styles;
exports.js = js;
exports.svgStore = svgStore;
exports.images = images;
exports.default = series(styles, js, svgStore, parallel(watchFiles, browser));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment