{"id":151967,"date":"2016-02-15T11:00:37","date_gmt":"2016-02-15T16:00:37","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=151967"},"modified":"2016-02-15T05:44:03","modified_gmt":"2016-02-15T10:44:03","slug":"gulp-for-wordpress","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/gulp-for-wordpress\/","title":{"rendered":"Using Gulp to Speed Up WordPress Development"},"content":{"rendered":"<p>Build tools let developers focus on efficient development rather than the nitty-gritty details that take away half your life but don&#8217;t add much to the project on their own. One such build tool is <a href=\"http:\/\/gulpjs.com\/\" target=\"_blank\">Gulp<\/a>.<\/p>\n<p>Gulp optimizes your theme&#8217;s images, concatenate your JS files, and processes your Sass\/LESS code automatically.<\/p>\n<p>In this article, I&#8217;ll show you how to get started and how you can use Gulp to speed up your development process.<\/p>\n<h2>What is Gulp?<\/h2>\n<p>Gulp is a JavaScript-based build tool that uses Node to automate mundane tasks. The basic idea is to create triggers that perform a specific action.<\/p>\n<p>For example, any time you save a Sass file it is compiled into a CSS file you specify. Or any time you save a JS file all your JavaScript files are concatenated into a single file and that file is then minified.<\/p>\n<h2>Sounds Good, How Do I Get Started?<\/h2>\n<p>You&#8217;ll need Node to run Gulp. This should be easy, just visit <a href=\"https:\/\/nodejs.org\/\" target=\"_blank\">nodejs.org<\/a> and download and run the installer. Node will be installed, along with npm (node package manager), which you will use to grab node packages, such as Gulp.<\/p>\n<p>The next step is to install Gulp globally. You can do this by opening the terminal or command prompt in Windows (I will call both of them terminal from now on) and issuing the following command:<\/p>\n<div class=\"gist\" data-gist=\"b694e334c67640509212900e2a243268\" data-gist-file=\"gulp-install.sh\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/b694e334c67640509212900e2a243268.js?file=gulp-install.sh\">Loading gist b694e334c67640509212900e2a243268<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This installs the Gulp command line interface so you can issue Gulp commands anywhere. You <em>also<\/em>\u00a0need to install Gulp for your specific project. This is because each project will have different requirements. You won&#8217;t be minifying the same files, you won&#8217;t be using the same structure, you may not even use WordPress or PHP for all of your projects!<\/p>\n<p>But before we do that, let&#8217;s talk WordPress for a moment.<\/p>\n<h2>Using Build Tools With WordPress<\/h2>\n<p>Most of our efforts will do one of two things: modify existing files or create new files based on existing ones. Many of the files we use for development will <em>not<\/em> be used in the final product. If we build a theme using Sass for the stylesheet, the final CSS file is what will be shipped as part of the theme, because the original Sass files aren&#8217;t needed.<\/p>\n<p>There are two approaches you can take:<\/p>\n<ul>\n<li>Supply development files within your project<\/li>\n<li>Omit all development-related files<\/li>\n<\/ul>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735 size-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/02\/structure.jpg\" alt=\"Project Structures\" width=\"735\" height=\"553\" \/><figcaption class=\"wp-caption-text\">Project Structures<\/figcaption><\/figure>\n<h3>Supplying Development Files<\/h3>\n<p>The upside of this is that you&#8217;ll make developers happy and they will be able to extend your work with more exactitude and with a higher degree of freedom. The downside is that the presence of these files may confuse some and you may need to offer support for them just the same. For open source projects, I would consider supplying all the development files, in which case they can reside within the main project.<\/p>\n<h3>Omitting Development Files<\/h3>\n<p>The upside of omitting these files is that they remove confusion and make downloads smaller. Development files for coding don&#8217;t usually add a huge overhead, but if you work with plenty of images or large text files this could be an issue. The downside is that only you will be able to work with the original files. In some projects this would be considered an upside so whichever works for you is what you should choose. Here the files reside outside of the final deliverable folder.<\/p>\n<h2>Installing Gulp For Our Project<\/h2>\n<p>Our test project will be a simple theme and we&#8217;ll be providing our development files so we&#8217;ll be using the structure in the left of the image above. Install Gulp in the <strong>main theme directory<\/strong>\u00a0of your site by navigating to the directory in the terminal and issuing the following command:<\/p>\n<div class=\"gist\" data-gist=\"7e689458c90e4a809ceadf233ca35bde\" data-gist-file=\"gulp-local.sh\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/7e689458c90e4a809ceadf233ca35bde.js?file=gulp-local.sh\">Loading gist 7e689458c90e4a809ceadf233ca35bde<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The next step is to create a Gulpfile. This is a JavaScript file that tells Gulp what to do and when to do it. Create <code>Gulpfile.js<\/code> in your main theme folder and paste the following contents into it:<\/p>\n<div class=\"gist\" data-gist=\"f0ef990c4305132371d1a4be1a699212\" data-gist-file=\"gulpfile.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/f0ef990c4305132371d1a4be1a699212.js?file=gulpfile.js\">Loading gist f0ef990c4305132371d1a4be1a699212<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>The initial line states that we are requiring Gulp itself. The next section defines a Gulp task. This could be anything from minification to image optimization, for now I&#8217;ve left this blank.<\/p>\n<p>Once you&#8217;ve saved this file go back to the terminal and issue a single command: <code>gulp<\/code>. if nothing else is specified the default task defined in the Gulpfile will be run. We have defined this task but it has no content so it just starts and finishes, you should see something like this in your terminal:<\/p>\n<p>&nbsp;<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-735x735 size-735x735\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2016\/02\/Screen-Shot-2016-02-11-at-12.50.50.png\" alt=\"Running an empty Gulp task\" width=\"735\" height=\"67\" \/><figcaption class=\"wp-caption-text\">Running an empty Gulp task<\/figcaption><\/figure>\n<h2>Creating Tasks<\/h2>\n<p>Most tasks will start with a source files (or a single file), manipulate them and output the result into a different file. Sass\/LESS might be the most obvious example so let&#8217;s start by processing a Sass file.To perform an action you&#8217;ll need to find the right Gulp module. This is pretty easy, write &#8220;gulp sass&#8221; into Google and you&#8217;ll find what you need, a package named<\/p>\n<p>To perform an action you&#8217;ll need to find the right Gulp module. This is pretty easy, write &#8220;gulp sass&#8221; into Google and you&#8217;ll find what you need, a package named <a href=\"https:\/\/www.npmjs.com\/package\/gulp-sass\" target=\"_blank\">gulp-sass<\/a>.<\/p>\n<p>Each package&#8217;s npm page has instructions on installation and usage. Apart from the specific commands for each module the steps are almost always the same:<\/p>\n<ul>\n<li>Install the package<\/li>\n<li>Include the package<\/li>\n<li>Use it in the Gulpfile<\/li>\n<\/ul>\n<p>You can install gulp-sass by issuing the following command in the terminal:<\/p>\n<div class=\"gist\" data-gist=\"f8d9904d58acdc5b3c5027f20cd00627\" data-gist-file=\"gulp-sass.sh\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/f8d9904d58acdc5b3c5027f20cd00627.js?file=gulp-sass.sh\">Loading gist f8d9904d58acdc5b3c5027f20cd00627<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Next, create a variable at the top of the Gulpfile that we&#8217;ll use later on:<\/p>\n<div class=\"gist\" data-gist=\"c63e3d98e2f36384cce2d9ec83c11314\" data-gist-file=\"include-gulp-sass.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/c63e3d98e2f36384cce2d9ec83c11314.js?file=include-gulp-sass.js\">Loading gist c63e3d98e2f36384cce2d9ec83c11314<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>Now we&#8217;re ready to create a task! Let me show you the full code and add the explanation below:<\/p>\n<div class=\"gist\" data-gist=\"35f5df1ba0505de04b56f2576c1b2830\" data-gist-file=\"sass-task.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/35f5df1ba0505de04b56f2576c1b2830.js?file=sass-task.js\">Loading gist 35f5df1ba0505de04b56f2576c1b2830<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>I started by defining a task named &#8220;sass.&#8221; This task takes a source file (the style.sass file within development\/styles), processes it and writes it to the style.css file in the main folder. Most of this is accomplished using the <code>pipe()<\/code> function. Piping is the backbone of Gulp, it transports data between the different functions.<\/p>\n<p>In this flow we start out with the contents of a source file. The contents are piped to the <code>sass()<\/code> function. This takes care of all the sass processing and spits out something (the final CSS). This content is further piped to the <code>dest()<\/code> function which defines where the file is written to. It will take on the name of the original file so we&#8217;ll end up with <code>style.css<\/code> as our final product. To run this task run <code>gulp sass<\/code> in the terminal.<\/p>\n<h3>Minifying Files<\/h3>\n<p>Anther common task is the minification of files. Once we&#8217;ve created our style.css we could make it a lot smaller, making our theme more efficient.<\/p>\n<p>Let&#8217;s follow the same process as before, starting with Googling &#8220;gulp minify css&#8221;. You should find <a href=\"https:\/\/www.npmjs.com\/package\/gulp-minify-css\" target=\"_blank\">gulp-minify-css<\/a>. Install it using <code>npm install --save-dev gulp-minify-css<\/code> and include it at the top of your Gulpfile using <code>var minifyCss = require('gulp-minify-css');<\/code><\/p>\n<p>Once everything is set up we can add it to our <code>sass<\/code> task. We simply need to pipe our content to the minification function after the sass module has done its thing, here&#8217;s the full task:<\/p>\n<div class=\"gist\" data-gist=\"5d0e050a4835976d76e992fdda5ac8de\" data-gist-file=\"minify-task.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/5d0e050a4835976d76e992fdda5ac8de.js?file=minify-task.js\">Loading gist 5d0e050a4835976d76e992fdda5ac8de<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>One trick I love to use is adding the <code>keepSpecialComments<\/code> options. By default all comments will be stripped out but our theme is not an actual theme unless it has the header comment in the <code>style.css<\/code> file. These cases are exactly why the special comments option exists. Append a exclamation mark after your comment opener (<code>\/*<\/code>) to make a comment special, here&#8217;s a full theme header for you:<\/p>\n<div class=\"gist\" data-gist=\"389e997847a57b0b92c5a2c8f79a3761\" data-gist-file=\"theme-header.scss\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/389e997847a57b0b92c5a2c8f79a3761.js?file=theme-header.scss\">Loading gist 389e997847a57b0b92c5a2c8f79a3761<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<h3>Concatenating Files<\/h3>\n<p>Let&#8217;s turn our attention to concatenation which allows us to merge multiple files into one. This technique is usually used for Javascript files. I like to use <a href=\"https:\/\/www.npmjs.com\/package\/gulp-include\" target=\"_blank\">gulp-include<\/a> because it gives me more control. By now you should be able to install and include it, let&#8217;s go straight to usage.<\/p>\n<p>Instead of concatenating a bunch of files by giving a source destination and a output location I&#8217;ll use one central JS file as an intermediary. Within this file I&#8217;ll be able to define which files I want to concatenate and also add regular JS code. In the development\/scripts folder I would create a <code>scripts.js<\/code> file ,which would look something like this:<\/p>\n<div class=\"gist\" data-gist=\"ccba4f24d275444123fc315a756bfb9a\" data-gist-file=\"scripts.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/ccba4f24d275444123fc315a756bfb9a.js?file=scripts.js\">Loading gist ccba4f24d275444123fc315a756bfb9a<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>gulp-include allows me to use the include statements you see at the beginning of the file to pull in the code found within those files. The first 6 are from <a href=\"http:\/\/foundation.zurb.com\/\" target=\"_blank\">Foundation<\/a>, they set up the Javascript needs of the Foundation framework. The final include is a code for sticky menu bars. Finally, I&#8217;ve added some regular Javascript code which will initialize Foundation. Let&#8217;s create a Gulp task that would make this all work:<\/p>\n<div class=\"gist\" data-gist=\"75acfd234488f2dc348003845f4818d9\" data-gist-file=\"scripts-task.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/75acfd234488f2dc348003845f4818d9.js?file=scripts-task.js\">Loading gist 75acfd234488f2dc348003845f4818d9<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>I&#8217;ve targeted the <code>development\/scripts\/scripts.js<\/code> file and piped it to the include function which will take care of the includes. The destination folder is the root folder, a scripts.js file will be created there. You can run this task by typing <code>gulp scripts<\/code> into the terminal.<\/p>\n<h3>Running Multiple Tasks<\/h3>\n<p>So far so good, but right now only have partial automation and we have to run everything separately. Let&#8217;s solve the second issue by filling out our default task. Our default task will run our already existing scripts and styles tasks. This is a simple matter. Here&#8217;s how our final default task will look:<\/p>\n<div class=\"gist\" data-gist=\"b6e55f27b7653c41bbf45aeb7db3ef58\" data-gist-file=\"default-task.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/b6e55f27b7653c41bbf45aeb7db3ef58.js?file=default-task.js\">Loading gist b6e55f27b7653c41bbf45aeb7db3ef58<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>That&#8217;s it, just keep on adding tasks as needed. Run the default task using the <code>gulp command<\/code>.<\/p>\n<h3>Watching Files<\/h3>\n<p>For our final trick we&#8217;ll make all of this run automatically as we make changes. The goal is to get Gulp to watch our scripts and styles and run the required tasks when they change. Install and include <a href=\"https:\/\/www.npmjs.com\/package\/gulp-watch\" target=\"_blank\">gulp-watch<\/a> on your own and then create a new task with the following code:<\/p>\n<div class=\"gist\" data-gist=\"bdc6c126e5187feee5bebbce5953723a\" data-gist-file=\"watch-task.js\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/bdc6c126e5187feee5bebbce5953723a.js?file=watch-task.js\">Loading gist bdc6c126e5187feee5bebbce5953723a<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This task contains a function that defines two sets of watched files. The first set will watch all files with the <code>.scss<\/code> extension within the development\/styles folder. When any of them change the <code>styles<\/code> task will be run. The second set will watch all files with the <code>.js<\/code> extension within the development\/scripts folder. When any of them change the <code>scripts<\/code> task will be run.<\/p>\n<p>If you run <code>gulp watch<\/code> from the terminal you should see that the task starts and finishes, but it doesn&#8217;t exit out and allow you to type another command. This is because the files are being watched. If you save a file you&#8217;ll see the task start and finish again. This will keep on happening until you close the terminal window or press control+c to exit out.<\/p>\n<h2>Saving Time with Build Tools<\/h2>\n<p>Build tools like Gulp are immensely powerful and we&#8217;ve barely just scratched the surface here. Whatever boring menial task you can come up with, you can automate it with Gulp.The<\/p>\n<p>The <a href=\"http:\/\/gulpjs.com\/plugins\/\" target=\"_blank\">Gulp Plugins<\/a> page is full of packages for ideas and you&#8217;ll find a bunch on Github as well. I use it for image optimization, sourcemaps, prefixing, creating local servers, refreshing the browser automatically and more!<\/p>\n<p><strong>Do you use Gulp or a different build tool? How has it altered your workflow? What are the biggest benefits that you see? Let us know in the comments below.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Build tools let developers focus on efficient development rather than the nitty-gritty details that take away half your life but don&#8217;t add much to the project on their own. One such build tool is Gulp, which can optimize your theme&#8217;s images, concatenate your JS files, and process your Sass\/LESS code automatically. Here&#8217;s how to get started.<\/p>\n","protected":false},"author":344049,"featured_media":152091,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[557],"tags":[10412,10411],"tutorials_categories":[],"class_list":["post-151967","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-build-tools","tag-gulp"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/151967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/344049"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=151967"}],"version-history":[{"count":10,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/151967\/revisions"}],"predecessor-version":[{"id":218997,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/151967\/revisions\/218997"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/152091"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=151967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=151967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=151967"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=151967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}