Setting up a CSS build process with Gulp

Luca Spezzano
Jul 11, 2019 · 5 min read
Image for post
Image for post

What is a build process?

It’s essentially just a sequence of tasks that are performed automatically and generate the files ready to be deployed.
We are going to implement a CSS build process that will compile Sass, prefix and compress our CSS.

Why Sass?

CSS is getting more powerful day by day, there are many CSS methodologies (I wrote an about CSS methodologies if you want to go deep in the argument) that help us to write more maintainable and scalable CSS code, but these methodologies alone aren’t enough to solve the problem of maintaining large CSS codebases.
That’s where Sass comes into play. Sass provides us features that regular CSS doesn’t have, using Sass we can write more readable, maintainable and reusable CSS code.

Sass has become the most used CSS pre-processor in the front-end universe. Also some of the best front-end frameworks like Bootstrap and Foundation have been developed with Sass.
Sass lets you use many interesting functionalities like variables, nested syntax, mathematical operations, mixins, loops, functions, imports and more.

The packages

To develop our process we need to use some packages, let’s see together which one we will use and how they work.

gulp: is the core of our process and as they write on their website “Gulp is a toolkit for automating painful or time-consuming tasks in your development workflow, so you can stop messing around and build something”. Gulp has a lot of very useful functionalities, in this case we will use only few:

  • gulp.src(): usually it is a string that is the path of the specific file to read;
  • gulp.dest(): usually it is a string that would be the destination path of the file once it will be processed;
  • gulp.watch(): it allows watching files and running the functions when a change occurs;
  • gulp.parallel(): it will execute the functions simultaneously;

gulp-sass: Sass is a preprocessor and to run in the browsers it needs to be compiled into css, that’s why we need gulp-sass, this gulp plugin will compile the Scss files into CSS.

gulp-autoprefixer: this plugin is very useful because it allows us not to think about the anymore.

gulp-clean-css: this plugin is essential before deploying your website, because it will take your CSS file and will compress it in .min.css file, reducing extremely size of the file.

gulp-purgecss: this is one of the most important because it will reduce the size of your css file removing all the unused styles.

gulp-rename: this gulp plugin is useful if we want to change the extension file names.

BrowserSync: is an automation tool that makes web development faster, it has many useful functionalities, in our case we will use:

  • server that will run a static server;
  • browserSync.stream()is very useful for the css because it will change the CSS LIVE without to refresh the page;
  • browserSync.reload() that will reload the page automatically.

Let’s build the process

Image for post
Image for post
building our process

At first be sure to have installed node, node-sass and npm, after open your terminal and install gulp-cli globally running

npm i -g gulp-cli

Now go wherever you want to create the folder and run

mkdir your-folder-name

and after go inside it

cd your-folder-name

Before to initialize our folder let’s create a simple structure for our folder, our structure should look like that. (You can see it on )

 src
| |
| assets
| | |
| | css
| | |
| | files
| | | |
| | | reset.scss
| | | |
| | | vendors.scss
| | | |
| | | element1.scss
| | |
| | |
| | main.scss
| |
| index.html
|
gulpfile.js

Once we created our files and have our structure we are free to initialize our folder

yarn init

and hit enter until the end.

Now it’s time to install our modules.

yarn add gulp gulp-sass browser-sync gulp-autoprefixer gulp-clean-css gulp-rename gulp-purgecss --dev

let’s install also Bootstrap, I would like to show you how to import the vendors in our css (you can see it on ).

yarn add bootstrap

Now open your text editor and let’s populate our gulpfile.js, at first we need to require our packages so let’s write in your gulpfile.js this:

var gulp = require('gulp'),
sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer'),
cleanCSS = require('gulp-clean-css'),
rename = require('gulp-rename'),
purgecss = require('gulp-purgecss'),
browserSync = require('browser-sync').create();

After the declaration we can start to write our first function

function css() {
return gulp.src('./src/assets/css/main.scss')
.pipe(sass().on('error', sass.logError))
.pipe(autoprefixer())
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(rename(function(path) {
path.extname = ".min.css";
}))
.pipe(
purgecss({
content: ['public/**/*.html']
})
)
.pipe(gulp.dest('./public/assets/css/'))
.pipe(browserSync.stream());
})

In this function we have all our CSS process, we compile the Scss file, we add the prefixers, we minify the files and add the extension .min.

Well done so far 😬

So the core of our process is done, now let’s create our second function

function html() {
return gulp.src('./src/**/*.html')
.pipe(gulp.dest('./public/'));
})

This function will bring simply our html files from our src folder to the public folder.

Last function is to run our static server

function serve() {
browserSync.init({
server: {
baseDir: './public'
}
})
})

In this case it will serve the public folder.

Now it’s time to create our watch, they are important to be faster during the development, they reload our pages automatically when we do a change in our files.

In this case we need only 2, 1 for scss files and 1 for html files

gulp.watch('./src/assets/css/**/*.scss', css);
gulp.watch('./src/*.html', html).on('change', browserSync.reload);

And at the end let’s create a sequence of functions that will run all our process

exports.default = gulp.parallel(html, css, serve);

Here we go, now run

gulp 

aaaaand

Instead if you want to clone directly the folder feel free to do it from , once you have cloned it just run

yarn

to install all our packages and after

gulp 

to run our process.

Here the link of the Github repo if you want to clone it to play around 😃

NotOnlyCSS

This publication includes original articles and tips about…

Sign up for NotOnlyCSS

By NotOnlyCSS

Articles and tips for frontend developers 

By signing up, you will create a Medium account if you don’t already have one. Review our for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Luca Spezzano

Written by

Frontend developer focused on CSS architecture of scalable and maintainable large scale projects and the development of amazing user interfaces.

NotOnlyCSS

This publication includes original articles and tips about frontend technologies.

Luca Spezzano

Written by

Frontend developer focused on CSS architecture of scalable and maintainable large scale projects and the development of amazing user interfaces.

NotOnlyCSS

This publication includes original articles and tips about frontend technologies.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface.

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox.

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store