To automate routine, repetitive processes, we use gulp.js. We use the following features in our template:
compiling scss in css, pug in html
automatic tagging of vendor prefixes
image optimization
script concatenation
script and style compression
creation of icon fonts and svg sprites
html validation check
These are basic features and there are a few more trivialities, but we’ll speak about them later. You can find the template of our team on the github (https://github.com/glivera-team/glivera-team-template)
Installation
Gulp is the npm package for node.js, that’s why we install node.js (https://nodejs.org/en/). Install as a simple program.
If you have Windows, you need to install 2 versions of python (https://www.python.org/downloads/release/python-2710/) and Microsoft Visual Studio (https://www.microsoft.com/en-gb/ download / details.aspx? id = 44914). They are necessary to work with some packages, most often installation problems appear because of their absence.
After installation, go to the project folder and enter in the console:
You will see various messages and you can answer all affirmatively. This initialization procedure is required to install packages. After its completion, enter into the console:
After executing this command, you will see the node_modules folder in your project. This is the folder for storing npm packages. In this case, you installed the package locally, i.e. all gulp files are in the project. As you can see, it takes some time. In order not to install a bunch of modules locally every time, install packages globally, and then make a link to them in the project. How to do this:
We have installed gulp globally and then made a link to the project. All the commands are run from the project folder.
Next, let’s install the remaining packages and link them together:
At first, I install plugins for development and then for building. If you have problems installing a plugin, try to install it separately.
Writing gulpfile
Create gulpfile.js at the root of your project. Gulp will not work without this file. It describes all the tasks listed at the beginning of the article.
At first, it is necessary to write dependencies for the project.
gulpfile.js
Usually there is information on how to prescribe a dependency in the plugin repository.
Next, declare variables for the project folders:
We will use them in the tasks. Let’s write the first task - to compile pug
Now, to compile pug, just enter into the console:
Do the same for sass
Step by step, you will get used to the syntax. Usually there are examples of tasks in the gulp module repository. Pay attention to the line
If a compilation error occurs, the gulp will “fly out”, and you will need to restart it. But with the plumber plugin this will not happen. In order to restart the browser after each task pass, use the command:
Use the following task to concatenate scripts:
It will connect all the tasks from the js/all folder into one script. Usually these are different plugins.
Tasks for synchronizing project folders between each other:
In order to track changes in the project and run tasks, we automatically use gulp.watch.
We use browser-sync to update the browser and local host.
Now, to start all this stuff we will write the default task gulp
Here we indicate which tasks should be run together. Thanks to the watch tusk, the gulp will not finish, but will wait for changes.
To run the default task, enter
A browser will open and everything will be compiled and updated automatically.