Fork me on GitHub

Using Gulp

You can use Gulp to automatically minify and compile your assets. This includes turning SASS to CSS or CoffeeScript to JavaScript. Gulp can also optimize your images and multiple other things. With Browsersync it can even reload your browser windows across multiple devices automatically.

Default Gulp settings require adaptation to watch for your theme

The Seravo template contains example gulpfile.js. Our example compiles sass files from twentyfifteen theme. It also has Browsersync already configured for theme twentyfifteen so you can use it as base for your own projects.

Browsersync can be started by running:

$ cd /data/wordpress/ && gulp watch

When it’s started the internal web server (Nginx) automatically reroutes all traffic through Browsersync so that you can have nice live reload effect while developing.

Example file

/*
 * This is default barebone for gulp, add your own modifications here
 * It also serves as an example of how to use browser-sync in this environment
 */
var gulp        = require('gulp');
var browserSync = require('browser-sync');
var sass        = require('gulp-sass');
var reload      = browserSync.reload;

/*
 * src array contains the files which the gulp will be watching
 * Choose your theme over here (twentyfifteen is provided as an example)
 */
var src = {
    scss: 'htdocs/wp-content/themes/twentyfifteen/scss/*.scss',
    css:  'htdocs/wp-content/themes/twentyfifteen/css/',
    php: [
        'htdocs/wp-content/themes/*/*.php',
        'htdocs/wp-content/plugins/*/*.php',
        'htdocs/wp-content/mu-plugins/*/*.php'
    ]
};

// Serve all files through browser-sync
gulp.task('serve', ['sass'], function() {

    // Initialize browsersync
    // Nginx is configured to use any service in port 1337
    // as middleware to WordPress in vagrant environment
    browserSync.init({
        // browsersync with a php server
        proxy: "http://localhost:8080",
        port: 1337,
        ui: {
            port: 1338
        },
        notify: true
    });

    // Watch sass files and compile them
    gulp.watch(src.scss, ['sass']);

    // Update the page automatically if php files change
    gulp.watch(src.php).on('change', reload);
});

// Give another name for serve
gulp.task('watch',['serve'], function() {
});

// Compile sass into CSS
gulp.task('sass', function() {
    return gulp.src(src.scss)
        .pipe(sass())
        .pipe(gulp.dest(src.css))
        .pipe(reload({stream: true}));
});

// Default task just compiles everything
// This is run when site is deployed!
gulp.task('default', ['sass'], function() {});