{"id":280453,"date":"2018-12-26T08:04:32","date_gmt":"2018-12-26T15:04:32","guid":{"rendered":"http:\/\/css-tricks.com\/?p=280453"},"modified":"2019-08-02T11:46:46","modified_gmt":"2019-08-02T18:46:46","slug":"gulp-for-wordpress-initial-setup","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/gulp-for-wordpress-initial-setup\/","title":{"rendered":"Gulp for WordPress: Initial Setup"},"content":{"rendered":"

This is the first part of a two-part series on creating a Gulp workflow for WordPress theme development. This first part covers a lot of ground for the initial setup, including Gulp installation and an outline of the tasks we want it to run. If you’re interested in how the tasks are created, then stay tuned for part two.<\/p>\n

Earlier this year, I created a course for building premium WordPress themes<\/a>. During the process, I wanted to use a task runner to concatenate and minify JavaScript and CSS files. I ended up using that task runner to automate a lot of other tasks that made the theme much more efficient and scalable.<\/p>\n

The two most popular task runners powered by Node are Gulp<\/a> and Grunt<\/a>. I went with Gulp after a good amount of research, it appeared to have an intuitive way to write tasks. It uses Node streams to manipulate files and JavaScript functions to write the tasks, whereas Grunt uses a configuration object to define tasks \u2014 which might be fine for some, but is something that made me a little uncomfortable. Also, Gulp is a bit faster<\/a> than Grunt because of those Node streams and faster is always a good thing to me!<\/p>\n

<\/p>\n

So, we’re going to set Gulp up to do a lot of the heavy lifting for WordPress theme development. We’ll cover the initial setup for now, but then go super in-depth on the tasks themselves in another post.<\/p>\n

\n

Article Series:<\/h4>\n
    \n
  1. \n Initial Setup (This Post)<\/em>\n <\/li>\n
  2. \n Creating the Tasks<\/a>\n <\/li>\n<\/ol>\n<\/div>\n

    Initial theme setup<\/h3>\n

    So, how can we use Gulp to power the tasks for a WordPress theme? First off, let\u2019s assume our theme only contains the two files that WordPress requires for any theme: index.php<\/code> and styles.css<\/code>. Sure, most themes are likely to include many more files that this, but that\u2019s not important right now.<\/p>\n

    Secondly, let\u2019s assume that our primary goal is to create tasks that help manage our assets, like minify our CSS and JavaScript files, compile Sass to CSS, and transpile modern JavaScript syntax (e.g. ES6, ES7, etc..) into ES5 in order to support older browsers. <\/p>\n

    Our theme folder structure will look like this:<\/p>\n

    themefolder\/\r\n\u251c\u2500\u2500 index.php\r\n\u251c\u2500\u2500 style.css\r\n\u2514\u2500\u2500 src\/\r\n    \u251c\u2500\u2500 images\/\r\n    \u2502   \u2514\u2500\u2500 cat.jpg\r\n    \u251c\u2500\u2500 js\/\r\n    \u2502   \u251c\u2500\u2500 components\/\r\n    \u2502   \u2502   \u2514\u2500\u2500 slider.js\r\n    \u2502   \u2514\u2500\u2500 bundle.js\r\n    \u2514\u2500\u2500 scss\/\r\n        \u251c\u2500\u2500 components\/\r\n        \u2502   \u2514\u2500\u2500 slider.scss\r\n        \u2514\u2500\u2500 bundle.scss<\/code><\/pre>\n

    The only thing we\u2019ve added on top of the two required files is a src<\/code> directory where our original un-compiled assets will live.<\/p>\n

    Inside of that src<\/code> directory, we have an images<\/code> subdirectory as well as others for our JavaScript and Sass files. And from, there, the JavaScript and Sass subdirectories are organized into components that will be called from their respective bundle<\/code> file. So, for example, bundle.js<\/code> will import and include slider.js<\/code> when our JavaScript tasks run so all our code is concatenated into a single file.<\/p>\n

    Identifying Gulp tasks<\/h3>\n

    OK, next we want Gulp tasks to a create a new dist<\/code> directory where all of our compiled, minified and concatenated versions of our assets will be distributed after the tasks have completed. Even though we\u2019re calling this directory dist<\/code> in this post because it is short for “distribution,” it could really be called anything, as long as Gulp knows what it is called. Regardless, these are the assets that will be distributed to end users. The src<\/code> folder will only contain the files that we edit directly during development.<\/p>\n

    Identifying which Gulp tasks are the best fit for a project will depend on the project\u2019s specific needs. Some tasks will be super helpful on some projects but completely irrelevant on others. I\u2019ve identified the following for us to cover in this post. You\u2019ll see that one or two are more useful in a WordPress context (e.g. the POT task) than others. Yet, most of these are broad enough that you\u2019re likely to see them in many projects that use Gulp to process Sass, JavaScript and image assets.<\/p>\n