Setting Up Your Gulpfile - A Simple Frontend Development Workflow 
Tags: beginners, css, tutorial
Published: 2020-08-16 05:23:25

If you're also working on [Frontend Mentor](https://www.frontendmentor.io/)’s amazing exercises, this would help your dev process tremendously.

This is a modification from [this article](https://themesberg.com/blog/tutorial/gulp-4-bootstrap-sass-browsersync). Follow the steps detailed there and check out my mods below.

Great for:
- Watching SCSS subfolders and every file in them
- Reloading and compiling whenever you make changes to your SCSS and HTML files
- Simple file systems

## Before you begin

You can refer to these tutorials ([Themesburg](https://themesberg.com/blog/tutorial/gulp-4-bootstrap-sass-browsersync), [CSS-Tricks](https://css-tricks.com/gulp-for-beginners/#globbing-in-node)) to install NPM, Gulp, and all the other goodies you’ll need to make this work.

## My project structure

![Project file structure](https://dev-to-uploads.s3.amazonaws.com/i/p7hiotzngu1znsay26vh.png)
The gulpfile in the [tutorial](https://themesberg.com/blog/tutorial/gulp-4-bootstrap-sass-browsersync) I linked above works great for simpler structures without sub-folders, but since I’m interested in making my code more modular, I’ve followed the [simple scss file structure](https://vanseodesign.com/css/sass-directory-structures/) illustrated here.

You can learn more about the why and how behind this method [here](http://thesassway.com/beginner/how-to-structure-a-sass-project) (TheSassWay.com).

## Issues

Since the gulpfile is originally set up to only watch .scss files that are in the scss folder, the files in the partials and variables subfolders will not be watched, and changes won’t trigger a reload or compilation.

Another thing about the tutorial’s gulpfile is that it doesn’t reload the browser whenever we make changes to our scss files. It simply watches and compiles, but will only reload if you make changes to the .html files.

## Making it work

In order to make BrowserSync and Gulp work for our scenario, I’ll need to make some minor tweaks. Take a look below. The changes are marked in bold and colored blue. You can copy this file further below in the article, where I’ve linked a gist.

Take note of lines 8, 23, and 24.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2vgj0a6jl1puetjn0njx.png)

## How does it work?

### Defining the source files properly

On lines 8 and 23, I’ve added extra syntax to allow for subfolder support. Instead of…

```
app/scss/*.scss
```

… which only makes gulp watch .scss files that are direct children of the scss folder, we’re putting in an extra layer, like so …

```
app/scss/**/*.scss
```

… which would then take subfolders and its contents in consideration. This (to my delight) is called Globbing, and you can read more about it [here](https://css-tricks.com/gulp-for-beginners/#globbing-in-node) (CSS-Tricks.com).

This solves our subfolder structure issue!

### Setting up browser reload on scss change

On line 24, I added the function^ below to the gulp watch task on lines 22–23.

```
.on("change", browserSync.reload);
```

The whole task would like like this:

```
gulp.watch("app/scss/**/*.scss", gulp.series("sass"))
.on("change", browserSync.reload);
```

This would force a reload whenever there are changes in any scss files.

_^I’m not entirely sure if that’s what we call that. Let me know what the proper name is, please?_

## Your turn!

Have a look at the whole copy-able gist file below and give it a try. Run `gulp-server` and make changes to your .scss partials and see what happens.


I like to test by changing my font color to a really obnoxious color just to see if it works :)

{% gist https://gist.github.com/beatricebock/00dcbceccb6b798dce06b93e891c7827 %}

That’s it! I hope you found this useful. If you’ve got a better solution, let me know! I’d love to learn from you.

## Resources

- [Frontend Mentor](https://www.frontendmentor.io/) — Build your portfolio with briefs, style guides, and even a pixel checker.
- [How to set up Gulp 4, Bootstrap, Sass and BrowserSync](https://themesberg.com/blog/tutorial/gulp-4-bootstrap-sass-browsersync) for a simple workflow ([Zoltán Szőgyényi](https://twitter.com/zolidev), Themesburg.com)
- [Gulp for Beginners](https://css-tricks.com/gulp-for-beginners) ([Zell Liew](https://css-tricks.com/author/zellwk/), CSS-Tricks.com)
- Create and share [beautiful images of your source code](https://carbon.now.sh/) (Carbon.now.sh) I’m using the Duotone color scheme and Fira Code font.