Yogesh Chauhan's Blog

How to compile and watch Sass using Gulp in WordPress?

in SCSS & WordPress on July 17, 2021

Follow first 3 steps from this post and install gulp and required packages.

How to install Gulp with WordPress?

Step 4: Sass Packages

We can use gulp-sass plugin to compile Sass files and gulp-clean-css plugin to minify the compiled files.

npm install sass gulp-sass gulp-clean-css gulp-sourcemaps gulp-postcss autoprefixer --save-dev

autoprefixer and gulp-postcss will help with vendor prefixing and gulp-sourcemaps… you know why we need that!

Step 5: Compile function

5.1: Import packages

Let’s import the installed packages to gulpfile.babel.js:

import { src, dest } from 'gulp';
import yargs from 'yargs';
import cleanCss from 'gulp-clean-css';
import gulpif from 'gulp-if';
import postcss from 'gulp-postcss';
import sourcemaps from 'gulp-sourcemaps';
import autoprefixer from 'autoprefixer';

const PRODUCTION = yargs.argv.prod;
var sass = require("gulp-sass")(require("sass"));


5.2: Write a function

Let’s write a function to compile Sass files and save it to a specific folder.

export const compileSass = () => {
  return src('assets/sass/main.scss')
    .pipe(gulpif(!PRODUCTION, sourcemaps.init()))
    .pipe(sass().on('error', sass.logError))
    .pipe(gulpif(PRODUCTION, postcss([ autoprefixer ])))
    .pipe(gulpif(PRODUCTION, cleanCss({compatibility:'ie8'})))
    .pipe(gulpif(!PRODUCTION, sourcemaps.write()))

Using the code above, we read the file using src and using dest we create the compiled css file. We’re also inserting sourcemap info for dev mode.

Using gulpif, we’re only minifying the css file, but only for the production mode.

This is how you can pass the production mode:

gulp compileSass --prod

Without –prod, it’ll just use dev mode by default.

5.1: Multiple Source Files

You can pass multiple files as an array like this:

  return src(['assets/sass/main.scss', 'assets/sass/projects.scss'])

Step 6: Watch Sass

We need to import watch in gulpfile.babel.js and write a function:

import { src, dest, watch } from 'gulp';
export const watchSass = () => {
  watch('assets/sass/**/*.scss', styles);

To call the function, use this command:

gulp watchSass

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to add a ribbon on top of a container using CSS?CSSRendering Elements in ReactReact2 Ways We Can Write Multiple Line Commands in PHPPHPinclude, include_once, require, require_once in PHPPHPWordPress: How to display slider repeater fields in ACF?WordPressLearn to make a responsive gallery using CSS Grid and without media queriesCSS