A Match Made in Heaven for Front-end Development

SASS and Gulp

Benjamin
12 min readJan 18, 2023

Introduction

  • What is SASS and why to use it: SASS (Syntactically Awesome Style Sheets) is a CSS preprocessor that allows you to use variables, functions, and other programming constructs in your CSS. It extends the functionality of CSS and makes it more powerful and easier to maintain. One of the main benefits of using SASS is that it allows you to write DRY (Don’t Repeat Yourself) code, which means you can reuse variables and other values across your stylesheets, making it easier to make changes and maintain your codebase. Additionally, SASS also provides features like nested selectors, mixins, and control directives, which make it easier to write complex CSS. Overall, SASS helps to make your CSS more organized, reusable, and maintainable.
  • What is Gulp and why to use it: Gulp is a JavaScript task runner that automates repetitive tasks such as minification, compilation, testing, and more. It uses a simple syntax to define tasks, making it easy to learn and use. One of the main benefits of using Gulp is that it can help to streamline your development workflow and save you a lot of time. For example, you can use Gulp to automatically compile your SASS files to CSS, minify your JavaScript, and even run tests on your code. Additionally, Gulp has a large ecosystem of plugins that can be used to perform a wide range of tasks. Overall, Gulp can help to automate repetitive tasks, improve your development workflow, and save you time.
Photo by Zany Jadraque on Unsplash

Setting up your project:

Installing Node.js and npm: To use Gulp, you will first need to install Node.js and npm (Node Package Manager) on your computer. Node.js is a JavaScript runtime that allows you to run JavaScript code on your computer, while npm is a package manager that allows you to easily install and manage JavaScript packages.

To install Node.js, go to the official website (https://nodejs.org/) and download the installer for your operating system. Once the installation is complete, you can open a command prompt or terminal and type “node -v” to confirm that Node.js is properly installed. Similarly, npm will also be installed when you install Node.js. You can check the version of npm by running “npm -v” in your command prompt or terminal.

After this, you can start installing the packages you need for your project by running “npm install <package_name>” in your command prompt or terminal.

Photo by Gabriel Heinzer on Unsplash

Creating a package.json file: A package.json file is a JSON file that contains information about your project, such as its name, version, and the packages it depends on. This file is used by npm to manage the packages that are installed for your project.

To create a package.json file, open a command prompt or terminal at the root of your project and run the following command:

npm init

This will start an interactive process that will prompt you for information about your project, such as its name, version, and description. You can accept the default values by pressing enter, or you can enter your values. Once you have completed the process, a package.json file will be created in your project’s root directory.

You can also create a package.json file by running npm init --yes which will use default values for all the fields and create package.json

It’s also important to note that when you will run npm install <package_name> it will also add that package in the dependencies section of the package.json file. This will help in the future when someone else will work on the project and they can easily install the packages the project needs by running npm install which will install all the packages mentioned in the dependencies section of package.json

Installing Gulp and other necessary packages: Once you have Node.js and npm set up, you can start installing the packages you need for your project. To use Gulp, you will need to install the gulp package as a global package and the gulp-sass package as a devDependency.

To install gulp as a global package, open a command prompt or terminal and run the following command:

npm install -g gulp

To install gulp-sass as a devDependency package, open a command prompt or terminal and navigate to your project’s root directory, then run the following command:

npm install --save-dev gulp-sass

This will install the gulp-sass package and also add it as a devDependency in the package.json file.

You may also need to install other necessary packages like gulp-concat, gulp-uglify, gulp-clean-css, gulp-sourcemaps, and other packages depending on your needs.

It’s important to note that, the gulp and gulp-sass packages are just examples, you might have to install different packages depending on your specific requirements.

Configuring Gulp for SASS

Setting up a gulpfile.js: A gulpfile.js is a JavaScript file that defines the tasks that Gulp will run. This file is typically located in the root of your project and should be named gulpfile.js.

To set up a gulpfile.js, you can create a new file in the root of your project with that name. Then you’ll need to require gulp and gulp-sass packages at the top of your gulpfile.js.

var gulp = require('gulp');
var sass = require('gulp-sass');

Creating a task to compile SASS to CSS: To create a task that compiles your SASS files to CSS. You can do this by creating a function and using the gulp.src and gulp-sass to perform the compilation. For example:

gulp.task('sass', function () {
return gulp.src('./sass/**/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest('./css'));
});

This task will take all the .scss files in the sass directory and its sub-directories and convert them to .css files in the CSS directory.

In this task, gulp.src method is used to specify which files to compile, in this case, all the .scss files in the sass directory and its sub-directories. Then, the sass() method is used to perform the compilation and the gulp.dest method is used to specify where to save the compiled CSS files.

You can also specify the options for the sass() method, like outputStyle, sourceComments, and other options.

gulp.task('sass', function () {
return gulp.src('./sass/**/*.scss')
.pipe(sass({outputStyle: 'compressed', sourceComments: 'map'}).on('error', sass.logError))
.pipe(gulp.dest('./css'));
});

In this case, the outputStyle is set to ‘compressed’ which will minify the CSS, and sourceComments is set to ‘map’ which will generate source maps for the CSS.

Once you’ve defined this task in your gulpfile.js, you can run it by typing gulp sass in your command prompt or terminal.

Adding a watch task to automatically compile SASS changes: To add a watch task that will automatically compile your SASS files whenever they change, you can use the gulp.watch method in your gulpfile.js.

For example:

gulp.task('watch', function () {
gulp.watch('./sass/**/*.scss', ['sass']);
});

This task will watch all the .scss files in the sass directory and its sub-directories, and when any of them change, it will run the ‘sass’ task defined earlier.

In this task, gulp.watch method is used to specify which files to watch, in this case, all the .scss files in the sass directory and its sub-directories. The second argument is the task to run when the files change.

You can also add other tasks to watch other file types and run them on change.

gulp.task('watch', function () {
gulp.watch('./sass/**/*.scss', ['sass']);
gulp.watch('./js/**/*.js', ['javascript']);
});

In this case, it will also watch all the .js files in the js directory and its sub-directories and when any of them change, it will run the ‘javascript’ task.

Once you’ve defined this task in your gulpfile.js, you can run it by typing gulp watch in your command prompt or terminal. This will start the watch task and it will keep running in the background, monitoring the specified files and running the specified task on change.

Photo by Mohammad Rahmani on Unsplash

Using SASS in your project

To use SASS in your project, you will first need to create some SASS files. These files should have a .scss file extension and can be organized in any directory structure you like.

For example, you could have a directory called “sass” in the root of your project, and within that directory, you could have subdirectories for different parts of your project, such as “base”, “components”, “layout”, etc.

You can create a new .scss file by creating a new file with the .scss extension, or you can rename an existing .css file to .scss

Once you have created your SASS files, you will need to import them into your CSS. This is done using the @import directive at the top of your main SASS file, which is typically called main.scss or style.scss.

For example, if you have a file called _variables.scss in the “sass” directory, you would import it in your main.scss file with:

@import "variables";

You can also use a relative path to import the file:

@import "base/variables";

You can also use wildcard (*) to import all the files in a directory

@import "base/*";

This will import all the .scss files into the base directory.

It’s important to note that the import statement should not include the file extension.

Once you’ve imported your SASS files into your main file, you can use the variables, mixins, and functions defined in those files in your CSS.

When you run the ‘sass’ task, it will take the main.scss file and process all the @import statements and combine them into a single CSS file, which you can link to in your HTML file.

Using SASS variables, mixins, and functions: SASS provides several features that allow you to write more powerful and maintainable CSS, such as variables, mixins, and functions.

  • Variables: Variables in SASS allow you to store a value and reuse it throughout your stylesheet. For example, you can use a variable to store a color value and then use that variable throughout your stylesheet. Variables are defined using the “$” symbol, followed by the variable name, and then the value.
$main-color: #ff0000;

In this case, $main-color is the variable name and #ff0000 is the value.

  • Mixins: Mixins are like functions for CSS. They allow you to define a set of CSS styles and then reuse them throughout your stylesheet. Mixins are defined using the “@mixin” keyword, followed by the mixin name, and then the styles.
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
border-radius: $radius;
}

In this case, border-radius is the mixin name and $radius is a parameter that can be passed to the mixin.

  • Functions: SASS provides several built-in functions, such as darken(), lighten(), and rgba(), that can be used to perform calculations on values. Functions are called by using their name followed by the parameter and are written with the #{} syntax.
background-color: darken(#ff0000, 10%);

In this case, darken is the function name, #ff0000 is the color value and 10% is the parameter.

Once you’ve defined your variables, mixins, and functions, you can use them throughout your stylesheet by referencing the variable or calling the mixin or function.

.header {
background-color: $main-color;
@include border-radius(5px);
}

In this case, the background-color of the header class will be #ff0000 and it will have a border-radius of 5px by calling the mixin border-radius.

You can also create your custom functions and use them in SASS, which can be very useful when you need to perform complex calculations or transformations on your values.

It’s important to note that, SASS variables and Mixins are scoped to the file where they are defined, you need to import the file that contains the mixins and variables in other files where you want to use them.

Troubleshooting common issues: Here are some common issues you may encounter when working with SASS and Gulp, and some possible solutions:

  • “Cannot find module ‘gulp-sass’” error: This error occurs when the gulp-sass package is not installed, or when it’s not listed as a dependency in your package.json file. To fix this, make sure that you have run npm install --save-dev gulp-sass to install the package and check if it's listed under devDependencies in your package.json file.
  • “SASS not compiling”: This can happen when the gulp-sass package is not being loaded correctly, or when there is an error in your gulpfile.js. Make sure that you’ve required the gulp-sass package at the top of your gulpfile.js and that you’ve defined the correct file paths in your gulp.src method. Also, make sure you’re running the correct task by running gulp sass.
  • “Gulp is not recognized as an internal or external command”: This error occurs when the gulp command is not recognized by the command prompt or terminal. To fix this, make sure that you have installed gulp globally by running npm install -g gulp.
  • “Gulp task is not running”: This can happen if you have not defined any tasks in your gulpfile.js, or if the task name you are running does not match the task name you’ve defined in your gulpfile.js. Make sure that you have defined the task and that the task name you’re running matches the one in your gulpfile.js
  • “Error: Invalid CSS after “…’: expected expression (e.g. 1px, bold), was “…”: This error occurs when you have a syntax error in your SASS code. This can be caused by a missing semicolon, a mismatched brace, or other issues with your SASS code. To fix this, make sure to check your SASS code for any syntax errors and correct them. You can also use a linter tool to help catch any issues with your code.
  • “Source maps not working”: This can happen when there is an issue with the gulp-sourcemaps package or with the configuration of the source maps in your gulpfile.js. To fix this, make sure that the gulp-sourcemaps package is correctly installed and required in your gulpfile.js, and that the source maps are correctly configured in your gulp task.
  • “CSS not updating after changing SASS files”: This can happen when the gulp watch task is not running or when there is an issue with the file paths in the gulp.watch method. To fix this, make sure that the gulp watch task is running and that the file paths are in the gulp.watch method matches the location of your SASS files.

These are just a few examples of the issues you may encounter when working with SASS and Gulp. If you are facing any issues not mentioned above, it’s best to check the documentation of the package or library that you are using, check the gulpfile.js and check the version of the packages that you are using, and search for solutions online.

Photo by Lewis Kang'ethe Ngugi on Unsplash

Conclusion

The benefits of using SASS and Gulp in your workflow: There are several benefits to using SASS and Gulp in your workflow:

  • Improved code organization: SASS allows you to use variables, mixins, and functions to organize your CSS code, making it easier to maintain and scale your stylesheets. With SASS, you can also use partials and imports to split your CSS code into smaller, more manageable files.
  • Improved performance: Using Gulp, you can automate repetitive tasks such as minifying and concatenating your CSS and JavaScript files, which can improve the performance of your website.
  • Improved development experience: With Gulp, you can set up a watch task that automatically compiles your SASS files whenever they change, which can save time and improve your development experience.
  • Cross-browser compatibility: With SASS, you can use mixins to add browser prefixes to your CSS, which can improve cross-browser compatibility.
  • Source maps: You can use the gulp-sourcemaps package to generate source maps for your CSS, which can help you to debug your code and understand the relationship between the SASS and CSS files.
  • Customizable: You can easily customize and configure your gulpfile.js to suit your specific needs.
  • Reusability: By using mixins and functions, you can create reusable code snippets, which can save you time and reduce the amount of code you need to write.
  • Productivity: With the automation provided by gulp and the organization provided by SASS, your productivity will increase and you’ll be able to focus on the most important tasks.

Overall, using SASS and Gulp in your workflow can improve the maintainability, scalability, and performance of your code, and make your development experience more efficient.

Photo by Emile Perron on Unsplash

Additional resources for learning more about SASS and Gulp: There are many resources available for learning more about SASS and Gulp, including:

These resources will help you to learn more about SASS and Gulp and understand how to use them effectively in your workflow.

--

--