Let's Talk

Contact UsLet's Talk Solution

    Software Development, Web Development

    Using Browserlist with Gulp and Autoprefixer: A Beginner’s Guide

    Using Browserlist with Gulp and Autoprefixer: A Beginner’s Guide

    Do you use Gulp to compile your Sass files or build your HTML or JS assets? If yes, then you also need to know about Browser-sync and Browserlist. Both of them are tools that are used as an extension of the Chrome browser and help you to test websites in various different browsers without having the actual Chrome browser installed on your machine.

    Gulp is a task-based build system and automation tool for code. It automates tasks like compiling Sass, minification, unit testing, etc. It is developed by Eric Schoffstall and it has become very popular among front-end developers due to its ease of use, flexible API, and integration possibilities.

    In this blog post, we will cover how we integrated Gulp with Browserlist tool to automate browser testing across the local development environment, staging server, Staging server with Capistrano, and CI servers like Jenkins, etc which helps us in faster feedback cycle during the development phase and more stable end product at production phase too.

    What is Browserlist?

    Browserslist is a tool that allows specifying which browsers should be supported in your frontend app by specifying “queries” in a config file. It’s used by frameworks/libraries such as React, Angular, and Vue, but it’s not limited to them.

    There are currently a few different servers that allow you to sync your local development environment with the browsers you’re testing on, but Browserlist is probably the best option right now. There are a few benefits of using Browserlist. First of all, it allows you to sync your local development environment with the browsers you’re testing on, which means that you can see any errors that occur in your code while you’re developing. By default, it will also sync your custom preferences like custom CSS, JavaScript, or fonts that you’ve added to your project.

    Install Browserlist manually in your local machine and register it with CI servers

    You can install Browserlist on your local machine by following these steps. First, you need to download the extension of Chrome and then you can access the settings of your browser and add “http://localhost:8000” as a trusted source. After that, open your local machine and add “localhost” to the end of the “Local hostname or IP” which is usually “127.0.0.1” in Linux/Mac OS X Terminal so that you can open the browser locally to sync it with the browsers you’re testing on. Now you can register your local machine with any CI servers like Heroku, Azure, etc by pasting your local machine’s address in their webhook settings. Make sure that you set up your Heroku/Azure account accordingly so that the webhook doesn’t get rejected.

    Integrate Gulp with Browserlist

    Since we are using Gulp to automate our build system, we can easily integrate Gulp with Browserlist and use them together to automate browser tests. We will install a plugin in our Gulpfile to start Browserlist as soon as our Gulpfile is loaded. We will use different plugins to set up different tasks like running a task with a set of options, running tasks in a certain order, etc. You can find all the plugins used in this blog post in our Github repository. We will first install the Gulp-Browserlist plugin in our Gulpfile to start Browserlist as soon as Gulpfile is loaded using the following command in the Terminal.

    Running Gulp task with Browserlist

    Let’s now start a simple Gulp task with Browserlist by changing our gulpfile.js file. This Gulp task will run a gulp task with a set of options that contains the name of our target, the path of our source file, a set of options to define our build process, etc. Now let’s run our Browserlist task from our Terminal to sync our local machine with the browsers we are testing on. We will see a bunch of logs in our Terminal which will contain the name of the server that logged in, which browser you are testing on, the URL, and the time when the sync started. If you are having trouble setting up the browserlist, you can also open the logs and look at the browserlist.log file which is created in your working directory when you ran the browserlist command in your terminal.

    Installing the required tools for Browserlist integration

    Now let you need to set up your local machine with all the required tools to sync your browser with the testing browsers. You can install them on your local machine and then start Browserlist there.

    Node.js – It is a server-side JavaScript runtime environment that can be used to build Single Page Applications.

    PhantomJS – It is a headless WebKit used for running browser tests.

    Gulp – is a task-based build system and automation tool for code. It automates tasks like compiling Sass, minification, unit testing, etc.

    Using Autoprefixer with Gulp

    Now let’s turn our attention toward our Autoprefixer plugin for Gulp. In our Browserlist task, we need to tell Autoprefixer which CSS rules we want to add to our web page. We can pass all the rules in the options object or we can use the CSS object to define each CSS rule one by one. The CSS object has three properties that define the selector, the property, and the value of each CSS rule. To set these rules, we first need to import the Autoprefixer plugin into our gulpfile.js file. Next, we need to define our Autoprefixer task in our gulpfile.js file. Now let’s run our Autoprefixer task from our Gulpfile.

    Using Browserlist with Autoprefixer and PostCSS

    Now let’s add the final layer of Autoprefixer and Browserlist integration. In our gulpfile.js file, we need to import the PostCSS plugin to transform our CSS files using PostCSS. We will use PostCSS to do all the transformations and produce suitable output files for Browserlist and Autoprefixer. We will add a Gulp task to transform our CSS file using PostCSS. We will use CSS Modules and the Autoprefixer plugins to import our CSS modules and add the required CSS rules. We will also add the PostCSS plugin so that we can transform our CSS files using PostCSS and write the transformed CSS to our style.css file.

    Now let’s run our CSS transformation task using Gulp. This completes our Browserlist integration for Gulp. Now when you run your tests, you will be able to view the logs in your browser, and also you will be able to see the diff between your changes in real-time. You can also see the results of your tests and view the changes in your code.

    Summary

    In order to use browserlist with gulp and autoprefixer, you need to first install the gulp-autoprefixer module. Once that is installed, you can add it to your gulpfile.js file like this

    var autoprefixer = require(‘gulp-autoprefixer’);
    gulp.task(‘styles’, function() {
    return gulp.src(‘css/style.css’)
    .pipe(autoprefixer({
    browsers: [‘last 2 versions’],
    cascade: false
    }))
    .pipe(gulp.dest(‘dist’));
    });

    Once that is done, you can then run the gulp styles task and it will automatically prefix your CSS properties with the correct vendor prefixes.
    You can also use browserlist with other tools such as Babel and ESLint. For more information on how to do that, please refer to the Browserlist documentation.

    Written by Aayush
    Writer, editor, and marketing professional with 10 years of experience, Aayush Singh is a digital nomad. With a focus on engaging digital content and SEO campaigns for SMB, and enterprise clients, he is the content creator & manager at SERP WIZARD.