dots Created with Sketch.
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';

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

#Jul 22 Is there a CSS parent selector? #Jul 22 Difference between :where and :is in CSS #Jul 22 Does :is() pseudo selector hint at CSS preprocessing in the future? #Jul 22 Control Scrolling with CSS Scroll Snap #Jul 21 Control rendering using CSS content-visibility property #Jul 21 How to use @supports rule in CSS?
You might also like these
How to read Standard Input in Swift?SwiftIN and BETWEEN Operators in SQLSQL/MySQLWordPress: How to get field values in Advanced Custom Fields?WordPressDROP DATABASE (remove a database) from PostgresPostgresWhat’s Interpolation in SCSS (Sass)?SCSSRIGHT JOIN in PostgresPostgres