Gulp tutorial for beginners

In a sentence, Gulp is a task runner.

Gulp makes our life easier. In a development workflow, we need to do some tasks frequently and gulp can do some tasks automatically. Don’t think gulp is a replacement of grunt, but it is being improved very very fast.

Gulp is a task/build runner for development. It allows you to do a lot of stuff within your development workflow. You can compile sass files, uglify and compress js files and much more. The kicker for gulp is that its a streaming build system which doesn’t write temp files.

In this article, I will show you to create a simple WordPress plugin taking advantage of gulp. I am using MAC OS (OS X Yosemite) and MAMP as server.

We will compile sass to css, minify it, concat all the css files into one file, uglify js, concat all js files into one file, run these on changes automatically, create po file for i18n and create the release package as zip.

Let’s start!

First, look at my directory structure:

In here, index.php is our main plugin file. There are three sass file in scss folder, which will be converted into css where gulp.main.css is the css file and gulp.main.min.css is the minified version. Exactly same for js script as well, gulp.main.min.js is the minified/uglified version of gulp.main.js which is a combination of ajax.js and public.js files. In the lang folder, we will generate out po file. node_modules folder will be auto generated, so is package.json file (we will see), the packages will be in there. We will use the releases folder to store the plugin package as zip. gulpfile.js file is our gulp configuration file.

Install Gulp

You must need Node JS and Node Package Manager (NPM) in your host to use Gulp. If you don’t have yet, please download from their site.

If you have those, let’s install gulp globally that will help us to run the gulp command anywhere in your system.

I assume you are already in root of the plugin directory in terminal.

Initializing gulp

Run the following command:

This command will create a package.json file and it will look like:

The command will ask you some question, you can leave to default just pressing enter or you can configure.

Now, we have to install dependencies. Of course, the first dependency is gulp itself:

The –save-dev flag asks to install the dependencies locally and you will see the updated package.json file with this dependency.

You may also read:  Limit one blog per user in a wordpress multisite

Now, let’s create gulpfile.js file manually, the file name is strict.

Hello World(?) in Gulp

Open the new created gulpfile.js and start configuring. We have just installed a dependency and to use every dependency, we need to use require function.

As we know, gulp is a task runner, so we will define some tasks, and gulp will run those tasks. The format of a task declaration is simple:

So, create our first task:

And run just “gulp mytask” in the terminal. Please note, in the command example first gulp is my plugin folder name, please do not get confused, sorry for that.

The “gulp mytask” will show the output like:

If we want to use more dependencies (of course we will use), then the default format should be:

Here, src function is used to get the source file, pipe function is used to bind with the dependency functions and dest function refers to the destination folder.

Let’s compile SASS

We have learnt about gulp, now let’s do a real task. We will compile sass file and for that we will need to install a dependency:

Add in gulp js file:

Write the task:

Write some sass file in about.scss file like:

Now run “gulp sass” in the terminal and you will see the generated css file:

Please check, in the task, the task name is sass, so we called “gulp sass”, gave the source file in src function, then used pipe function to run sass dependency function, and then sent to the destination.

This is done for one file, what if we have multiple files? We can do is very easily:

Now, if you have 3 sass files, it will generate 3 css files. But we want to combine all those. So, let’s install contact dependency:

And add code in gulp js file:

See, in pipe function, I used concat funtion and gave a file name. All the css files will be combined and get name gulp.main.css and then sent to the destination folder.

Our next job is to minify the generated css, so install minify module:

Add code in gulp file:

This is all similar, here I have also mentioned to make compatible to IE8, that’s totally optional.

Let’s do some auto-prefixing job. Like, if you use “display: flex” in your sass file, it will create all the prefixer for that like – -webkit-flex, -ms-flex etc, cool – heh?

You may also read:  WordPress admin user search by name

So, install auto-prefixer:

Add in gulp file:

Another interesting thing, we are going to add – sourcemap. This is very interesting. To debug css, we always use the browser developer toolbar. But when you use sass to write your css, then in browser inspector, you will see reference to css not sass file. Sourcemap will help you to see the reference of sass file, like the screenshot below:


Install sourcemap then:

Configure in gulp file:

We are almost done. Just one thing, after doing minify we need to rename the file to with some suffix, like .min. We will use rename module:

And now configure:

Please note, in a step I renamed the task name to “build-css”. So, now run in the terminal “gulp build-css” and see the magic! The minified version and combined version will be generated in the css folder.

As we are familiar with the process now, I will go little faster, if any confusion, please comment below.

Let’s play with JS now

For JS files, we will do almost similar things, except we will uglify instead of minify. It’s same but uglify minifies the variable name and do other compression too.

Install uglify then:

And in gulp file:

Now, run “gulp build-js” in terminal and see the output files in js folder.

Do the above jobs on the fly!

Till now, what we have done, if we make any changes we will need to run the command and check in the browser. How’s about doing it more automatic? Like we will make changes, save and right then the above task will be completed. We will ask gulp to watch for any changes, and if any changes found, run the above two tasks.

We will use “watch” command to ask gulp to watch changes.

Check at the very bottom, I have defined the watch task which will watch all css and js files for changes and run the corresponding task.

Create po file

In WordPress, we need to provide po file to the users, so that they can translate the strings in their language. We normally use poedit or glotpress to do this job. But now, we will run a php shell command using gulp.

You may also read:  Assign a membership to a registered member automatically - Membership 2 (WPMU)

To do that, we will use WordPress’ makepot tool. So, download the wp-dev using svn:

And the gulp task will be:

Here, /Users/ashokkumarnath/Desktop/pot/wp-dev/tools/i18n/makepot.php is the path to makepot.php file which will create the po file. Please replcae the path with your file path.

wp-plugin is used for plugins, if you are developing a theme, use “wp-theme”.

Then the folder name “lang”, in my case, and the text-domain gtd.po. Change gtd with your text-domain.

Now run “gulp i18n” and see the po file is generated in lang folder.

Final touch and release the plugin!

Now, we will copy the files we need for the plugin and paste to releases folder, under the version name:

The created task name is “build” here. We have asked to copy index.php and recurrsive assets folder into releases/VERSION_NO/ folder.

In last step, we will create zip of the plugin, so we will need zip module:

And create the zip task:

You will see at the bottom, I have created a default task for gulp. The default task will run when you command just “gulp” in the terminal. So, when “gulp” command is run in terminal, I have set ‘build-css’, ‘build-js’, ‘build’, ‘zip’ – all four tasks to be run.

Now, if you just run “gulp”, it will compile the sass, create css, combine, create minify version, combine js files, create minified js file, create po file, copy the plugin files and create the releasable zip.

I have done some more polishing in my final gulpfile.js file:

In line 65, I asked to run build task only when build-css and build-js tasks are done, not before that. In line 73, the zip task will run, when the build task is done. This is called task dependency.

So, when you will work on css and js, before start working run “gulp watch” in the terminal, do not break/cancel it so that it can run the build-css and build-js when you make some changes. When you are done, just run “gulp”.

This is the final version of package.json:

This is the final version of main plugin file:

It may look like little complex, but once you practice 2-3 times, it will be very very easy.

So, be automatic! 🙂

You may also like...

4 Responses

  1. Md. Mrinal Haque says:

    Inspired me to learn Gulp. Thanks

  2. Apu says:

    great boss….keep educate us……….

Leave a Reply

%d bloggers like this: