<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Gulp - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Gulp - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 30 May 2026 22:26:23 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/gulp/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Using Gulp 4 in your workflow for Sass and JS files ]]>
                </title>
                <description>
                    <![CDATA[ By Jessica Chan This post was originally published on Coder-Coder.com. This tutorial will explain, step by step, how to set up Gulp 4 in your workflow, as well as how to migrate from Gulp 3 to Gulp 4 syntax. Need to just quickly get your Gulp 3 gul... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/gulp-4-walk-through-with-example-code-c3c018eab306/</link>
                <guid isPermaLink="false">66d46150d14641365a050975</guid>
                
                    <category>
                        <![CDATA[ Gulp ]]>
                    </category>
                
                    <category>
                        <![CDATA[ learning to code ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 26 Mar 2019 03:27:19 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2019/06/Gulp-4-workflow-walkthrough.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Jessica Chan</p>
<p><em>This post was originally published on <a href="https://coder-coder.com/gulp-4-walk-through/">Coder-Coder.com</a>.</em></p>

<p>This tutorial will explain, step by step, how to set up Gulp 4 in your workflow, as well as how to migrate from Gulp 3 to Gulp 4 syntax.</p>
<blockquote>
<p><em>Need to just quickly get your Gulp 3 gulpfile.js working with Gulp 4? <a class="post-section-overview" href="#migrating">Click here</a> to go right to that part of the post.</em></p>
</blockquote>
<ol>
<li>Install the gulp-cli on your command line by running <code>npm install gulp-cli -g</code>.</li>
<li>Install Gulp by running <code>npm install gulp</code>.</li>
<li>Install other npm packages for your Gulp workflow.</li>
<li>Create a <code>gulpfile.js</code> file in your project root.</li>
<li>Import your npm packages as modules in your gulpfile.</li>
<li>Add your tasks to the gulpfile to compile your SCSS/JS files and run a watch task.</li>
<li>Run the <code>gulp</code> command to run all your tasks.</li>
</ol>
<h3 id="heading-what-is-gulp-and-what-does-it-do">What is Gulp and what does it do?</h3>
<p>Gulp is a tool that will run various tasks for you in your web development workflow. It might be called a bundler, build tool, or a task runner. They all mean roughly the same thing.</p>
<p>Similar tools are Webpack and Grunt (although Grunt is quickly becoming obsolete).</p>
<p>Here’s what we’re going to have Gulp do for us:</p>
<ol>
<li>Compile our Sass files to CSS</li>
<li>Add vendor prefixes to our CSS</li>
<li>Minify our final CSS file</li>
<li>Concatenate (i.e. combine) our JS files</li>
<li>Uglify our final JS file</li>
<li>Move the final CSS/JS files into our <code>/dist</code> folder.</li>
</ol>
<p>Pretty cool, right?!</p>
<p>So the way it works is, all your settings and tasks are stored in a <code>gulpfile.js</code> file. And you run Gulp on your command line.</p>
<p>The great part about that is that once you have your gulpfile all set up, you can easily reuse it for other projects. So it’s a huge time-saver!</p>
<p>Let’s move on to installing and setting up Gulp on your computer.</p>
<h3 id="heading-installing-gulp-using-a-working-demo-project">Installing Gulp, using a working demo project</h3>
<p>Before you can run Gulp, you will need to install a couple of things:</p>
<ul>
<li>Install <a target="_blank" href="https://nodejs.org/en/">Node.js</a> if you don’t have it yet.</li>
<li>Install the <a target="_blank" href="https://www.npmjs.com/package/gulp-cli">Gulp Command Line Utility</a> by running <code>npm install --global gulp-cli</code>.</li>
</ul>
<p>Once you have Gulp working, check out a demo project I built that uses Gulp! It’s a front-end boilerplate project that’s meant to be a quick way for me to get started with any simple front-end website.</p>
<p>(I also have plenty of code snippets in the rest of this tutorial, so you can just refer to those as well!)</p>
<p><strong>To set the front-end boilerplate project up:</strong></p>
<ul>
<li>Clone or download the <a target="_blank" href="https://github.com/thecodercoder/frontend-boilerplate">Git repo</a> of this project onto your computer.</li>
<li>Open the project, and in the root project folder, go to your command line and run <code>npm install</code>. This will install the npm packages listed in the <code>package.json</code> file, particularly Gulp 4.</li>
</ul>
<p>You should now have the project files set up, and all the npm packages installed that we’ll be using to run Gulp tasks.</p>
<p>Since the files from the repo are ready to go, if you type in <code>gulp</code> in the command line, you should see an output like this:</p>
<pre><code>&gt; gulp [<span class="hljs-number">22</span>:<span class="hljs-number">29</span>:<span class="hljs-number">48</span>] Using gulpfile ~\Documents\GitHub\frontend-boilerplate\gulpfile.js [<span class="hljs-number">22</span>:<span class="hljs-number">29</span>:<span class="hljs-number">48</span>] Starting <span class="hljs-string">'default'</span>... [<span class="hljs-number">22</span>:<span class="hljs-number">29</span>:<span class="hljs-number">48</span>] Starting <span class="hljs-string">'scssTask'</span>... [<span class="hljs-number">22</span>:<span class="hljs-number">29</span>:<span class="hljs-number">48</span>] Starting <span class="hljs-string">'jsTask'</span>... [<span class="hljs-number">22</span>:<span class="hljs-number">29</span>:<span class="hljs-number">48</span>] Finished <span class="hljs-string">'jsTask'</span> after <span class="hljs-number">340</span> ms [<span class="hljs-number">22</span>:<span class="hljs-number">29</span>:<span class="hljs-number">48</span>] Finished <span class="hljs-string">'scssTask'</span> after <span class="hljs-number">347</span> ms [<span class="hljs-number">22</span>:<span class="hljs-number">29</span>:<span class="hljs-number">48</span>] Starting <span class="hljs-string">'watchTask'</span>...
</code></pre><p>And you’ve run Gulp!</p>
<h3 id="heading-whats-happening-in-the-project-when-you-run-gulp">What’s happening in the project when you run Gulp?</h3>
<p>All right, you have the project successfully working! Now let’s get into more detail on what’s in the project and how it works.</p>
<p>First, here’s a quick rundown of the file structure of our project:</p>
<ul>
<li><strong>index.html</strong> — your main HTML file</li>
<li><strong>package.json</strong> — contains all the npm packages to install when you run <code>npm install</code>.</li>
<li><strong>gulpfile.js</strong> — contains the config and runs all the Gulp tasks</li>
<li><strong>/app</strong> — working folder, you will edit SCSS/JS files in here</li>
<li><strong>/dist</strong> — Gulp will output files here, don’t edit these files</li>
</ul>
<p>In your workflow, you will edit the HTML, SCSS, and JS files. Gulp will then detect any changes and compile everything. Then you will load your final CSS/JS files from the <code>/dist</code> folder in your <code>index.html</code> file.</p>
<p>Now that we have Gulp running, let’s take a closer look at how Gulp works.</p>
<h3 id="heading-what-exactly-is-in-the-gulpfilejs">What exactly is in the gulpfile.js?</h3>
<p>Here’s an in-depth explanation of each section of the gulpfile, and what they do:</p>
<h4 id="heading-step-1-initialize-npm-modules">Step 1: Initialize npm modules</h4>
<p>At the very top of the <code>gulpfile.js</code>, we have a whole bunch of constants that import the npm packages we installed earlier, using the <code>require()</code> function.</p>
<p>Here’s what our packages do:</p>
<p>Gulp package:</p>
<ul>
<li><code>gulp</code> — runs everything in the gulpfile.js. We’re exporting just the specific gulp functions that we will be using, like <code>src</code>, <code>dest</code>, <code>watch</code>, and others. This allows us to call those functions directly without the <code>gulp</code>, for example we can just type in <code>src()</code> instead of <code>gulp.src()</code>.</li>
</ul>
<p>CSS packages:</p>
<ul>
<li><code>gulp-sourcemaps</code> — maps the CSS styles back to the original SCSS file in your browser dev tools</li>
<li><code>gulp-sass</code> — compiles SCSS to CSS</li>
<li><code>gulp-postcss</code> — runs autoprefixer and cssnano (see below)</li>
<li><code>autoprefixer</code> — adds vendor prefixes to CSS</li>
<li><code>cssnano</code> — minifies CSS</li>
</ul>
<blockquote>
<p><em>If you’ve used Gulp before, you might be wondering why I’m using straight-up <code>autoprefixer</code> and <code>cssnano</code>, instead of <code>gulp-autoprefixer</code> and <code>gulp-cssnano</code>. For some reason, using them will work, but will prevent sourcemaps from working. Read more about that issue <a target="_blank" href="https://github.com/gulp-sourcemaps/gulp-sourcemaps/issues/60">here</a>.</em></p>
</blockquote>
<p>JS packages:</p>
<ul>
<li><code>gulp-concat</code> — concatenates multiple JS files into one file</li>
<li><code>gulp-uglify</code> — minifies JS</li>
</ul>
<p>Now that we have our modules added, let’s put them to work!</p>
<h4 id="heading-step-2-use-the-modules-to-run-your-tasks">Step 2: Use the modules to run your tasks</h4>
<p>A “task” in Gulp is basically a function that performs a specific purpose.</p>
<p>We’re creating a few utility tasks to compile our SCSS and JS files, and also to watch those files for any changes. Then those utility tasks will be run in our default Gulp task when we type <code>gulp</code>on the command line.</p>
<h4 id="heading-creating-constants-for-file-paths"><strong>Creating constants for file paths</strong></h4>
<p>Before writing our tasks, though, we have a few lines of code that save our file paths as constants. This is optional, but I like using path variables so that we don’t have to manually retype them each time:</p>
<p>This code creates <code>scssPath</code> and <code>jsPath</code> constants that we will use to tell Gulp where files are found.</p>
<h4 id="heading-sass-task"><strong>Sass task</strong></h4>
<p>Here’s the code for our Sass task:</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">scssTask</span>(<span class="hljs-params"></span>)</span>{        <span class="hljs-keyword">return</span> src(files.scssPath)        .pipe(sourcemaps.init())        .pipe(sass())        .pipe(postcss([ autoprefixer(), cssnano() ]))        .pipe(sourcemaps.write(<span class="hljs-string">'.'</span>))        .pipe(dest(<span class="hljs-string">'dist'</span>)    );}
</code></pre><p>Our Sass task, called <code>scssTask()</code>, is doing several things. In Gulp, you can chain multiple functions by using the Gulp function <code>pipe()</code> after the first function.</p>
<p>In our task, Gulp is first running <code>src()</code> to load the source directory of the SCSS files. It’s using the constant we created earlier, <code>files.scssPath</code>.</p>
<p>Then after <code>src()</code> we’re piping everything else into the build process. You can think about it like adding more and more sections of pipe onto an existing pipe.</p>
<p>The functions it’s running are:</p>
<ul>
<li><code>sourcemaps.init()</code> — sourcemaps needs to be added first after <code>src()</code></li>
<li><code>sass()</code> does the compiling of all the SCSS files to one CSS file</li>
<li><code>postcss()</code> runs two other plugins:</li>
<li><ul>
<li><code>autoprefixer()</code> to add vendor prefixes to the CSS</li>
</ul>
</li>
<li><ul>
<li><code>cssnano()</code> to minify the CSS file</li>
</ul>
</li>
<li><code>sourcemaps.write()</code> creates the sourcemaps file in the same directory.</li>
<li><code>dest()</code> tells Gulp to put the final CSS file and CSS sourcemaps file in the <code>/dist</code> folder.</li>
</ul>
<h4 id="heading-js-task"><strong>JS task</strong></h4>
<p>Here’s the code for our JavaScript task:</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">jsTask</span>(<span class="hljs-params"></span>)</span>{    <span class="hljs-keyword">return</span> src([files.jsPath])        .pipe(concat(<span class="hljs-string">'all.js'</span>))        .pipe(uglify())        .pipe(dest(<span class="hljs-string">'dist'</span>)    );}
</code></pre><p>Our JavaScript task, called <code>jsTask()</code>, is also running multiple functions:</p>
<ul>
<li><code>src()</code> to load the JS files from <code>files.jsPath</code>.</li>
<li><code>concat()</code> to concatenate all the JS files into one JS file.</li>
<li><code>uglify()</code> to uglify/minify the JS file.</li>
<li><code>dest()</code> to move the final JS file into the <code>/dist</code> folder.</li>
</ul>
<h4 id="heading-watch-task"><strong>Watch task</strong></h4>
<p>The <code>watch()</code> function is a super handy part of Gulp. When you run it, it will run continuously, waiting to detect any changes to the files you tell it to watch. When it detects changes, it will run any number of tasks you tell it to.</p>
<p>This is great because then you don’t have to keep manually running <code>gulp</code> after every code change.</p>
<p>Here’s the code for our watch task:</p>
<pre><code><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">watchTask</span>(<span class="hljs-params"></span>)</span>{    watch(        [files.scssPath, files.jsPath],        parallel(scssTask, jsTask)    );}
</code></pre><p>The <code>watch()</code> function takes three parameters, but we’re just using two:</p>
<ul>
<li>globs, meaning the file path strings,</li>
<li>options (not used), and</li>
<li>tasks, meaning which tasks we want to run.</li>
</ul>
<p>What we’re having our watch task do is watch the files in our <code>scssPath</code> and <code>jsPath</code> directories. Then if any changes are made in either, run the <code>scssTask</code> and <code>jsTask</code> tasks simultaneously.</p>
<p>Now that we’ve gotten our utility tasks set up, we need to set up our main Gulp task.</p>
<h4 id="heading-default-task"><strong>Default task</strong></h4>
<p>This is the main Gulp task, which will automatically run if you type in <code>gulp</code> on the command line.</p>
<pre><code><span class="hljs-built_in">exports</span>.default = series( parallel(scssTask, jsTask), watchTask);
</code></pre><p>Gulp will automatically search for a <code>default</code> task in your <code>gulpfile.js</code> when you use the <code>gulp</code>command. So you must export the default task in order for it to work.</p>
<p>Our default task will do the following:</p>
<ul>
<li>Run the <code>scssTask</code> and <code>jsTask</code> simultaneously using <code>parallel()</code></li>
<li>Then run the <code>watchTask</code></li>
</ul>
<p>You’ll also notice that we are putting all those tasks inside the <code>series()</code> function.</p>
<p><strong>This is one of the major new changes in Gulp 4 for how it handles tasks– you are required to wrap all tasks (even single ones) either in <code>series()</code> or <code>parallel()</code>.</strong></p>
<h3 id="heading-a-note-on-registering-tasks-whats-changed-in-gulp-4">A note on registering tasks: what’s changed in Gulp 4</h3>
<p>If you’ve been using the <code>tasks()</code> function to run everything, you may have noticed that there’s a difference now.</p>
<p>Instead of using <code>gulp.task()</code> to contain all your task functions, we’re creating actual functions like <code>scssTask()</code> and <code>watchTask()</code>.</p>
<p>This is to follow Gulp’s official guidelines for creating tasks.</p>
<p>The Gulp team recommends using <code>exports</code> rather than <code>tasks()</code>:</p>
<blockquote>
<p><em>“In the past, <code>task()</code> was used to register your functions as tasks. While that API is still available, exporting should be the primary registration mechanism, except in edge cases where exports won’t work.” [<a target="_blank" href="https://gulpjs.com/docs/en/getting-started/creating-tasks">Gulp Documentation</a>]</em></p>
</blockquote>
<p>So, while they still let you use the <code>task()</code> function, it’s more current to create JS functions for each task and then export them.</p>
<p><a></a></p>
<p>If you can, I’d recommend updating your syntax to reflect this, as it’s possible that Gulp will deprecate <code>task()</code> at some point.</p>
<h3 id="heading-problems-migrating-from-gulp-3">Problems Migrating from Gulp 3?</h3>
<p>If you’ve been using Gulp 3 and all you want is to get the dang thing working with Gulp 4, you’re in luck!</p>
<p>Before, in Gulp 3, you could simply list a single function or multiple functions in an array. However, in Gulp 4, doing so without wrapping them in either <code>series()</code> or <code>parallel()</code> will throw an error now.</p>
<p>Something like:</p>
<p><code>AssertionError [ERR_ASSERTION]: Task function must be specified</code></p>
<p>Gulp 4 introduces two new functions to run tasks: <code>series()</code> and <code>parallel()</code>. It gives you the option of running multiple tasks concurrently, or one after the other.</p>
<p>So to quickly fix the error, put all your tasks into either a <code>series()</code> or <code>parallel()</code>function.</p>
<p><strong>Tasks in (old) Gulp 3 syntax</strong></p>
<p>In Gulp 3, you might have run tasks this way:</p>
<p><code>gulp.task('default', ['sass', 'js', 'watch']);</code></p>
<p><code>gulp.watch('app/scss/*.scss', ['sass']);</code></p>
<p><strong>Tasks in Gulp 4 syntax</strong></p>
<p>Put those tasks into a series() function like this:</p>
<p><code>gulp.task('default', gulp.series('sass', 'js', 'watch'));</code></p>
<p><code>gulp.watch('app/scss/*.scss', gulp.series('sass'));</code></p>
<p>And that will fix the task function error with the smallest change possible! ?</p>
<h3 id="heading-project-files-download"><strong>Project files download</strong></h3>
<p>All the code I’ve displayed here is from a GitHub repository I have for front-end boilerplate. It’s meant as a quick starter kit for any simple front-end website project.</p>
<p>You’re welcome to check it out, customize, and use it for your own projects!</p>
<p><a target="_blank" href="https://github.com/thecodercoder/frontend-boilerplate">Check out the GitHub repository here.</a></p>
<h3 id="heading-in-closing"><strong>In closing</strong></h3>
<p>I hope that you’ve found this walk-through of how to get Gulp 4 running helpful!</p>
<p>If you enjoyed this post or have a question, feel free to leave a comment below! ?</p>
<p><strong>Want more?</strong></p>
<p>? Read more tutorials on my blog, c<a target="_blank" href="https://coder-coder.com">oder-coder.com.</a><br>? S<a target="_blank" href="https://coder-coder.com/subscribe">ign up here to get emails about new articles.</a><br>? Join 24,000+ others — Follow @<a target="_blank" href="https://www.instagram.com/thecodercoder/">thecodercoder on Instagram.</a><br>? Check out coding tutorials on <a target="_blank" href="https://www.youtube.com/c/codercodertv">my YouTube channel</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to minify images with Gulp & gulp-imagemin and boost your site’s performance ]]>
                </title>
                <description>
                    <![CDATA[ By Jonathan Sexton Images are everywhere across the internet. You would be hard pressed to find a single page or application that doesn’t contain at least one image in some form or another. Images are great way to help tell stories and emphasize crit... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-minify-images-with-gulp-gulp-imagemin-and-boost-your-sites-performance-6c226046e08e/</link>
                <guid isPermaLink="false">66d45f74ffe6b1f641b5fa0b</guid>
                
                    <category>
                        <![CDATA[ Gulp ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Productivity ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ UX ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 06 Feb 2019 19:47:30 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9ca58e740569d1a4ca6a30.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Jonathan Sexton</p>
<p>Images are everywhere across the internet. You would be hard pressed to find a single page or application that doesn’t contain at least one image in some form or another. Images are great way to help tell stories and emphasize critical parts of our lives.</p>
<p>But if you’re like me you know that having a large image can seriously impact the performance of your site/app. So today, I’m going to teach you how to use Gulp and an <code>npm</code> package called <code>gulp-imagemin</code> to reduce the size of your images on the fly.</p>
<p>If you don’t know what all of these words mean, fear not! I have some relevant and important links/descriptions below to help bring you up to speed.</p>
<ul>
<li><a target="_blank" href="https://en.wikipedia.org/wiki/Minification_(programming)">Minification</a>, or minifying as I like to call it, is the act or process of removing unnecessary parts of source code to reduce size.</li>
<li><code>Gulp</code> is a JavaScript build tool that allows you to automate parts of your workflow to streamline your process. It takes care of some not so interesting, but important, aspects of your workflow (like reducing image size) so that you can focus on the building. You can <a target="_blank" href="https://gulpjs.com/">find Gulp here</a>.</li>
<li>To make use of <code>npm</code> we'll need to install <code>Node.js</code> which is, in a nutshell, the framework that allows developers to use JavaScript code in a server (back end) environment. You can <a target="_blank" href="https://nodejs.org/en/download/">find Node here</a>.</li>
<li><code>npm</code> (Node Package Manager) is and does what its name implies. It is a package manager for JavaScript and "the world's largest software registry". Just think of <code>npm</code> as a giant storage area for awesome packages/utilities to help developers. You can <a target="_blank" href="https://www.npmjs.com/">find npm here</a>.</li>
<li><code>gulp-imagemin</code> is one of those awesome packages I mentioned earlier. Using this package we'll be able to automatically reduce the size of our images every time a save occurs. You can <a target="_blank" href="https://www.npmjs.com/package/gulp-imagemin">find gulp-imagemin here</a>.</li>
</ul>
<p>Alright, now that explanations are out of the way let’s get to the fun parts :D</p>
<h3 id="heading-project-file-structure">Project File Structure</h3>
<p>Start by opening up your text editor of choice and creating a directory for your project or if you have an existing directory navigate to that directory in your terminal and skip down to the <strong>Installing Node &amp; npm Section</strong>.</p>
<p>If you’re using <a target="_blank" href="https://code.visualstudio.com/">VS Code</a> you can find the <a target="_blank" href="https://code.visualstudio.com/docs/editor/integrated-terminal">built in terminal</a> by hitting <code>ctrl +</code> (tilde)`.</p>
<p>Here’s how my project structure looks in my terminal:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/SFUA0D00x1r1kSEyEAha4U0uoXy5zUVKElHf" alt="Image" width="489" height="75" loading="lazy"></p>
<p>And here’s how my project file structure looks in the explorer inside VS Code:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/8GrSHZyzovwyyYWmHSBRWhR0CBVrZPKQfUsX" alt="Image" width="288" height="752" loading="lazy"></p>
<p>As you can see I have a separate directory for my base files and the minified files. Once you have your project directory established it’s time to start installing everything we’ll need.</p>
<h3 id="heading-installing-node-amp-npm">Installing Node &amp; npm</h3>
<p>Alright, now that our directory is up and running let’s start installing our dependencies. If you already have <code>Node &amp; npm</code> installed, feel free to skip down to the <strong>Installing Gulp &amp; gulp-imagemin Section</strong>.</p>
<ol>
<li>First, enter <code>node --v</code> within your terminal to check and see if you have the Node installed. If you do, you'll get something back like <code>v8.9.3</code></li>
<li>If you get nothing back or an error, simply download and <a target="_blank" href="https://nodejs.org/en/download/">install Node from here</a>. It could take a few minutes so +be patient.</li>
<li>Once <code>Node.js</code> is installed, you'll have <code>npm</code> installed as well because it comes bundled with <code>Node</code>. You can check the version of <code>npm</code> by typing <code>npm -v</code> in your terminal. You should get something like <code>6.4.1</code>back.</li>
<li>Next we need to create a <code>package.json</code> file for our project. We do this by using the command <code>npm init</code> (find out more about <code>[package.json](https://docs.nodejitsu.com/articles/getting-started/npm/what-is-the-file-package-json/)</code> <a target="_blank" href="https://docs.nodejitsu.com/articles/getting-started/npm/what-is-the-file-package-json/">here</a>). You'll be asked a series of questions but if you don't want to answer them you don't have to, just hit enter until you see <code>Is this OK? (yes)</code>, then hit <code>Enter</code> one last time and you'll be finished with this section.</li>
</ol>
<p><img src="https://cdn-media-1.freecodecamp.org/images/ra8Rx3jITz4p8TiUA32gdv9o0PmTqoLvrYd5" alt="Image" width="707" height="773" loading="lazy"></p>
<p>You’ll notice that this file was created in a different directory than the one I started with. This is so I can provide an example, as I have previously installed all of this in my current project directory.</p>
<h3 id="heading-installing-gulp-amp-gulp-imagemin">Installing Gulp &amp; gulp-imagemin</h3>
<p>Once <code>Node &amp; npm</code> have been installed, we can now install <code>Gulp &amp; gulp-imagemin</code> by following these steps:</p>
<ol>
<li>First, type <code>npm install --save-dev gulp</code> in your terminal. If you want to know what the <code>--save-dev</code> flag does, check out this <a target="_blank" href="https://stackoverflow.com/questions/19223051/what-does-save-dev-mean-in-npm-install-grunt-save-dev">Stack Overflow post</a>.</li>
<li>Again, be patient as installing Gulp might take a minute but you’ll eventually end up with something like this: <code>gulp@4.0.0 added 318 packages from 218 contributors and audited 6376 packages in 49.362s found 0 vulnerabilities</code></li>
<li>You can check your Gulp version by typing <code>gulp -v</code> in your terminal and you'll get something similar to this: <code>[13:06:56] CLI version 2.0.1 [13:06:56] Local version 4.0.0</code></li>
<li>Now let’s install <code>gulp-imagemin</code> by typing <code>npm install --save-dev gulp-imagemin</code> and again you'll get something like this back: <code>gulp-imagemin@5.0.3 added 232 packages from 97 contributors and audited 10669 packages in 39.103s found 0 vulnerabilities</code></li>
<li>And the final step for this section is to create our <code>gulpfile.js</code> <strong>It is very important that your file has this exact name and is in the outer most level of your project folder structure!</strong></li>
</ol>
<h3 id="heading-writing-the-code-finally-the-fun">Writing the Code — Finally the Fun!</h3>
<p>Ok, now that we’ve taken care of installing everything in the correct place, let’s open up our <code>gulpfile.js</code> and write the actual code that will do all of the hard work.</p>
<ol>
<li>Start by requiring <code>gulp</code> --&amp;g<code>t; const gulp = require('gulp</code>');We're basically taking advantage of Node's module system to use code that is located in different files</li>
<li>Now require <code>gulp-imagemin</code> --&amp;g<code>t; const imagemin = require('gulp-imagemin</code>'); Again we're taking advantage of the module system to use this code in our project</li>
<li>Now, we need to write the function that will do all of the image squashing:<br><code>function imgSquash() {</code><br><strong>return</strong> gulp <strong>.</strong>src("./img/<em>")<br><strong>.</strong>pipe(imagemin())<br><strong>.</strong>pipe(gulp<em>*.</em></em>dest("./minified/images"));<br><code>}</code></li>
<li>If you set your directory up following mine, the code above will work. If your directory looks different you will need to change the <code>.src &amp; .dest</code> lines to match where your files are located and where you want them piped to after they've been minified.</li>
<li><code>Gulp</code> operates based off of tasks and we can give it plenty of those to keep it busy. Once we've defined the actual function to do the heavy lifting, we need to tell <code>Gulp</code> what to do with that function: <code>gulp.task("imgSquash", imgSquash);</code></li>
<li>Now, we want <code>Gulp</code> to watch our given directory for changes (new images) and when it detects those, we want it to automatically run our <code>imgSquash</code> function, minify our images, and pipe them to the destination we set. We achieve that by defining another task to watch the directory:<br><code>gulp**.**task("watch", () **=&amp;**gt; {</code><br><code>g**u**lp.watch("./img/*", imgSquash);</code><br>});</li>
<li>The last step to writing the code is defining the last task to call our <code>imgSquash</code> and <code>watch</code> tasks in succession: <code>gulp**.**task("default",gulp**.**series("imgSquash","watch"));</code> Here the word "default" refers to the word <code>gulp</code> in the terminal and the <code>gulp.series</code> will ensure that the <code>imgSquash</code> function runs and immediately after Gulp will watch the directory for changes.</li>
</ol>
<p>Here is what our finished file should look like:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/PGzForat5pR93lI2H5W4hSfBJcPFojPVcoJ6" alt="Image" width="555" height="375" loading="lazy"></p>
<p>Save this file, open your terminal, and type <code>gulp</code> and hit enter. You should see something like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/tSVYnl8tIMN0VIy8KoZbE81GXwtdPAIDl4Wi" alt="Image" width="500" height="314" loading="lazy"></p>
<p>As you can see, each time a new file was added to the base directory, our tasks kicked in because Gulp was watching and immediately ran our <code>imgSquash</code> function to minify our images. When you're finished using Gulp you can hit <code>ctrl + c</code> in your terminal to terminate the watch process.</p>
<p>Now you can start using your minified images on your website/app and enjoy that new found boost in performance!</p>
<h3 id="heading-wrap-up">Wrap Up</h3>
<p>Gulp is a very powerful JavaScript build tool that can help automate some of the more tedious, but important, aspects of building your project. With less than an hour’s worth of work you were able to get your images minified, thus reducing load time and increasing performance for your website/app. That’s awesome and you should be proud of yourself!</p>
<p>This is just one of the many ways that build tools like Gulp can help you. There are many more ways it can help (minifying/concatenating CSS/JS files) and I hope you explore some of those awesome options.</p>
<p>If you enjoyed this article please consider donating some claps as it helps others find my work. Also, drop a comment and let me know what you’re working on and how Gulp helps you focus on the building.</p>
<p>And finally, this article was originally posted on <a target="_blank" href="https://jonathansexton.me/blog">my personal blog</a>. While you’re there don’t forget to sign up for the <strong>Newsletter</strong> which can be found at the top right corner of my blog page. I send it out monthly (I promise not to spam your inbox) and it’s filled with awesome articles from across the web that I think you’ll find helpful.</p>
<p>As always, have an awesome day full of love, happiness, and coding!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to set up Gulp-sass using the command line if you’re a beginner ]]>
                </title>
                <description>
                    <![CDATA[ By Simeon Bello I intern at a tech firm presently, and few days ago I got a challenge from my boss about writing an article. So I decided to write something on Gulp-sass.Setting it up can be frustrating sometimes, especially when you are new to it. I... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-set-up-gulp-sass-using-the-command-line-if-youre-a-beginner-17729f53249/</link>
                <guid isPermaLink="false">66c35484c45f383e42e907a6</guid>
                
                    <category>
                        <![CDATA[ coding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Gulp ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Fri, 30 Nov 2018 17:23:30 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*4Ua25jOkgtszd4brxFG0pQ.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Simeon Bello</p>
<p>I intern at a tech firm presently, and few days ago I got a challenge from my boss about writing an article. So I decided to write something on Gulp-sass.<br>Setting it up can be frustrating sometimes, especially when you are new to it. I use Windows, and searching for an article that would solve my problem was like getting Jack in Black-ish to spell “decrease”.</p>
<p>Ok I think I got a little bit carried away there…enough about me, let’s get started!</p>
<p>P.S. this is my first published article and I hope you love it. :)</p>
<h3 id="heading-installing-node">Installing node</h3>
<p>First, open the command line and install <a target="_blank" href="https://nodejs.org">node.js</a> on your computer. It comes with a Node Package Manager(npm) which you can use to install Gulp. After installing, you can install Gulp by running <code>npm install gulp -g</code>. The <code>**-g**</code> instructs <strong>npm</strong> to install Gulp globally on your computer (this means that you can use gulp commands anywhere on your computer.)</p>
<p>Before I continue, I am assuming you are familiar with the command line!</p>
<p>Navigate to your project directory and run <code>npm init</code>. This will create a package.json file, press enter and it will add what you need into<br>the package.json file.</p>
<p>Yeah, you may be wondering what a package.json file is right?<br>A <a target="_blank" href="https://docs.nodejitsu.com/articles/getting-started/npm/what-is-the-file-package-json/">package.json file</a> holds various metadata relevant to your project. This file gives information to npm and allows it to identify the project as well as handle the project’s dependencies. It also makes it easier to install all the tasks that are used in a Gulp project.</p>
<p>If you still don’t get it, you probably need Diane to explain it better — what is my problem/obsession with Black-ish??</p>
<p>After running <code>npm-init</code>, type <code>npm install gulp --save-dev</code><strong>,</strong> this instructs <strong>npm</strong> to install Gulp into your project. By using <code>--save-dev</code> we store Gulp as a dev dependency in the package.json.</p>
<h3 id="heading-creating-a-gulpfile">Creating a Gulpfile</h3>
<p>Now that you’ve installed Gulp, you’re ready to install the first task. You have to <code>require</code> Gulp. Create a new file called gulpfile.js in your project directory — You can do this by using any text editor. Start by adding the code below to your gulpfile.</p>
<pre><code>‘use strict’;
</code></pre><pre><code><span class="hljs-keyword">var</span> gulp = <span class="hljs-built_in">require</span>(‘gulp’);
</code></pre><h3 id="heading-setting-up-a-task">Setting up a task</h3>
<p>Now you can install a <strong>gulp task —</strong> in this case we would install Gulp-sass. This task makes it possible to convert <em>Sass to CSS</em>. Still using the command line, you can install Gulp-sass by running <code>**npm** install gulp-sass --save-dev</code>. After that, require Gulp-sass in your gulpfile.js.</p>
<p>Put <code>var sass = require(‘gulp-sass’);</code>under the line you required gulp.</p>
<h3 id="heading-structuring-your-project">Structuring your project</h3>
<p>Before you use the lines below, I am also assuming you know how to structure a web app. Here I am going to use the structure of common web apps.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/Qe0oi9tR9oQ1WTgIJxWTV0VTBnZEHAkHEE2s" alt="Image" width="800" height="148" loading="lazy"></p>
<h3 id="heading-compiling-sassscss">Compiling sass/scss</h3>
<p>The last thing then is to instruct gulp what files it needs to convert and where the destination should be — where the output file will be stored.</p>
<p>Use the following;</p>
<pre><code><span class="hljs-comment">//compile gulp.task(‘sass’, function () { gulp.src(‘app/scss/app.scss’) .pipe(sass().on(‘error’, sass.logError)) .pipe(gulp.dest(‘app/css’)); });</span>
</code></pre><p>The file in <strong>gulp.src</strong> will be converted, you can also select all .scss files in a directory by using <code>“app/scss/*.scss”</code>. This will select all your .scss files in the folder scss.</p>
<p>The <strong>gulp.dest</strong> is the output. The output will be stored in the CSS folder inside the app folder.</p>
<h3 id="heading-gulp-watch-sass">Gulp-watch-sass</h3>
<p>Gulp has a watch syntax which allows it to monitor source files and then “watch” for changes made to your code. Just add the syntax to your gulpfile.js by typing:</p>
<pre><code><span class="hljs-comment">//compile and watch gulp.task(‘sass:watch’, function() { gulp.watch(‘app/scss/app.scss’, [‘sass’]);});</span>
</code></pre><p>That’s pretty much everything you have to do! It wasn’t that stressful, or was it?</p>
<p>Thanks for reading!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to minify your CSS with gulp ]]>
                </title>
                <description>
                    <![CDATA[ By Vinicius Gularte In this article, I'm going to show a simple way to automatically minify your CSS files using gulp. ? To start — what is gulp? Gulp is a JavaScript task runner that lets you automate tasks such as… Bundling and minifying libraries... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-minify-your-css-with-gulp-6ff3f4a896b5/</link>
                <guid isPermaLink="false">66c353ab9972b7c5c7624ee8</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Front-end Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Gulp ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 10 Oct 2018 17:20:07 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*buKin7TOLVwnO4affvO8Qg.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Vinicius Gularte</p>
<p>In this article, I'm going to show a simple way to automatically minify your CSS files using gulp. ?</p>
<h3 id="heading-to-start-what-is-gulp"><strong>To start — what is gulp?</strong></h3>
<p>Gulp is a JavaScript task runner that lets you automate tasks such as…</p>
<ul>
<li>Bundling and minifying libraries and stylesheets.</li>
<li>Refreshing your browser when you save a file.</li>
<li>Quickly running unit tests.</li>
<li>Running code analysis.</li>
<li>Less/Sass to CSS compilation.</li>
<li>And much more!</li>
</ul>
<p>The gulp workflow works as follows:</p>
<p>We can create tasks that we would like to fulfill. In these tasks we load files that we want gulp to work on (modifying or not), then we return them to some return folder.</p>
<p>It’s simple.</p>
<p>In this little tutorial, I will teach you how to create a task to minify all CSS files in your folder. Then put the minified ones in another folder.</p>
<h3 id="heading-lets-start">Let’s start</h3>
<p>For this tutorial, make sure you have the latest version of the npm package installed on your machine. If you do not have it, you can download it <a target="_blank" href="http://www.npmjs.com"><strong>here</strong></a>.</p>
<p>Once you have npm installed, in the base directory of your project we will install gulp using these commands:</p>
<p><code>npm install gulp-cli -g</code></p>
<p><code>npm install gulp -D</code></p>
<p>We will also use a gulp plugin to minify CSS called <strong>gulp-clean-css</strong>, so install it in the project type:</p>
<p><code>npm install gulp-clean-css --save-dev</code></p>
<p>Very good, now with the dependencies installed in the project, let’s create a file called <strong>Gulpfile.js</strong>. This file will be responsible for our tasks.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*RHjTJ_6QntCKKKnuZm_ndA.png" alt="Image" width="259" height="27" loading="lazy"></p>
<p>We will create two folders in this repository too. One will be called styles where our style files will stay, and another called dist where the minified files will go.</p>
<p>In the end, our project will have this structure:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*dLew0FI0XbGbyxKuCIadGA.png" alt="Image" width="302" height="113" loading="lazy"></p>
<h3 id="heading-in-gulpfilejs">In Gulpfile.js</h3>
<p>At the beginning of the file, we make the calls of the packages that we will use.</p>
<p>With the packages called, we will create the responsible task by minifying our files.</p>
<p>You might be wondering — you’re already able to minify your files? Yes, by executing the gulp command in the terminal followed by the task name.</p>
<p>But running this command all the time is a bit annoying, isn’t it? We can create a method for observing changes to files in the styles folder.</p>
<p>In this way, running the gulp command will wait for changes in the selected files to activate the minify-css task.</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>This is just a small way that gulp can help us in the development of our applications.</p>
<p>You can find the code for this project in <a target="_blank" href="https://github.com/ViniciusGularte/MinifiedCssGulp">this repository</a> on GitHub.</p>
<p><em>Thank you for reading, please feel free to ? and help others find it.</em></p>
<p><em>See you soon.</em> ?</p>
<h3 id="heading-references">References</h3>
<p><a target="_blank" href="https://gulpjs.com/"><strong>gulp.js</strong></a><br><a target="_blank" href="https://gulpjs.com/">_By preferring code over configuration, node best practices, and a minimal API surface - gulp makes things simple like…_gulpjs.com</a><a target="_blank" href="https://www.npmjs.com/package/gulp-clean-css"><strong>gulp-clean-css</strong></a><br><a target="_blank" href="https://www.npmjs.com/package/gulp-clean-css">_Minify css with clean-css._www.npmjs.com</a></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Super simple Gulp tutorial for beginners ]]>
                </title>
                <description>
                    <![CDATA[ By Jessica Chan These days, using a build tool is an indispensable part of your web development workflow. Gulp is one of the most popular build tools these days — along with Webpack. But there’s a definite learning curve to learning Gulp. One of the ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/super-simple-gulp-tutorial-for-beginners-45141974bfe8/</link>
                <guid isPermaLink="false">66d461633a8352b6c5a2ab25</guid>
                
                    <category>
                        <![CDATA[ coding ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Gulp ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 08 Aug 2018 10:17:27 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*4oEHylwkvSTcgoxgygdbcg.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Jessica Chan</p>
<p>These days, using a build tool is an indispensable part of your web development workflow.</p>
<p><a target="_blank" href="https://gulpjs.com/">Gulp</a> is one of the most popular build tools these days — along with Webpack.</p>
<p>But there’s a definite learning curve to learning Gulp. One of the biggest hurdles is figuring out the seemingly hundreds of different parts that go into it.</p>
<p>And on top of that, you have to do everything on the command line, which can be incredibly intimidating if you haven’t worked much with it.</p>
<p>This tutorial will walk you through the basics of npm (Node Package Manager) and setting up Gulp for your front-end projects. Once you’re done, you’ll feel way more confident setting up your workflow and using the command line!</p>
<h4 id="heading-so-whats-the-big-deal-with-gulp"><strong>So what’s the big deal with Gulp?</strong></h4>
<p>Gulp is a huge time saver. By using Gulp, you can let your computer handle tedious tasks, such as:</p>
<ul>
<li>Compiling Sass files to CSS</li>
<li>Concatenating (combining) multiple JavaScript files</li>
<li>Minifying (compressing) your CSS and JavaScript files</li>
<li>And automatically running the above tasks when a file change is detected</li>
</ul>
<p>Gulp can do a lot more complex tasks than the ones I’ve mentioned above. However, this tutorial will be focused on just the basics of Gulp and how it works.</p>
<h4 id="heading-quick-outline-of-what-well-be-doing"><strong>Quick outline of what we’ll be doing</strong></h4>
<p>Here are the steps this tutorial will be going through:</p>
<ol>
<li>Install Node.js and npm to your computer</li>
<li>Install Gulp and other packages needed for your project</li>
<li>Configure your gulpfile.js file to run the tasks you want</li>
<li>Let your computer do your work for you!</li>
</ol>
<p>Don’t worry if you don’t totally understand all the terms above. I’ll explain everything one step at a time.</p>
<p>Now let’s get started!</p>
<h3 id="heading-set-up-your-environment">Set up your environment</h3>
<h4 id="heading-nodejs"><strong>Node.js</strong></h4>
<p>In order to get Gulp up and running on your computer, you need to install Node.js onto your local environment.</p>
<p>Node.js is self-described as a “JavaScript runtime”, which is considered the back-end of JavaScript. Gulp runs using Node, so you understandably need to install Node before getting started.</p>
<p>You can download it from the <a target="_blank" href="https://nodejs.org/en/">Node.js</a> website. When you install Node, it also installs npm onto your computer.</p>
<p>What’s npm, you ask?</p>
<h4 id="heading-npm-node-package-manager"><strong>Npm (Node Package Manager)</strong></h4>
<p><a target="_blank" href="https://www.npmjs.com/get-npm">Npm</a> is a continually updated collection of JavaScript plugins (called packages), written by developers around the world. Gulp is one of those plugins. You’ll also need a few more, which we’ll get into later.</p>
<p>The beauty of npm is that it allows you to install packages directly on your command line. This is great, because you don’t have to manually go to the website, download and execute the file to install.</p>
<p>Here’s the basic syntax to install a package:</p>
<p><code>npm install [Package Name]</code></p>
<p><strong>Note for Mac users:</strong><br> Depending on your setup, you may need to add the “sudo” keyword at the beginning to run this with root permissions.</p>
<p>So for Macs if would look like: <code>sudo npm install [Package Name]</code></p>
<p>Seems pretty straightforward, right?</p>
<h4 id="heading-the-nodemodules-folder"><strong>The node_modules folder</strong></h4>
<p>One thing to note: when you install an npm package, npm creates a folder called node_modules and stores all the package files there.</p>
<p>If you’ve ever had a project with a node_modules folder and dared to see what it contained, you probably saw that it had lots (and I mean LOTS) of nested folders and files.</p>
<p>Why does this happen?</p>
<p>Well, this is because npm packages tend to rely on other npm packages in order to run their specific function. These other packages are known as dependencies.</p>
<p>If you’re writing a plugin, it makes sense to take advantage of the functionalities of existing packages. No one wants to reinvent the wheel every time.</p>
<p>So when you install a plugin into your node_modules folder, that plugin will then install additional packages that <strong>it</strong> needs into its own node_modules folder.</p>
<p>And so on and so forth until you have nested folders out the wazoo.</p>
<p>You don’t need to worry too much about messing in the node_modules folder at this point — just wanted to briefly explain what’s going on in that crazy folder.</p>
<h3 id="heading-keeping-track-of-packages-with-packagejson">Keeping track of packages with package.json</h3>
<p>Another cool feature of npm is that it can remember what specific packages you’ve installed for your project.</p>
<p>This is important in case you have to reinstall everything for some reason.</p>
<p>Also it makes life easier for other developers, because they can quickly and easily install all the packages for your project on their computers.</p>
<p>How does it manage to do this?</p>
<p>Npm utilizes a file called package.json to keep track of what packages and what package versions you have installed. It also stores other information about the project, like its name, author, and Git repository.</p>
<h4 id="heading-creating-your-packagejson"><strong>Creating your package.json</strong></h4>
<p>To initialize this file, you can again use the command line.</p>
<p>First, navigate to your project folder, wherever you have it located on your computer.</p>
<p>Then type in the following command:<br><code>npm init</code></p>
<p>Npm will then prompt you to enter in information about the project. For the majority of options, you can hit enter and use the default value that’s in parentheses.</p>
<p>When you’re done, npm will generate the package.json file in your project folder! If you open it up in your editor, you should see something like this:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"super-simple-gulp-file"</span>,
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"description"</span>: <span class="hljs-string">"Super simple Gulp file"</span>,
  <span class="hljs-attr">"main"</span>: <span class="hljs-string">"gulpfile.js"</span>,
  <span class="hljs-attr">"scripts"</span>: {
    <span class="hljs-attr">"test"</span>: <span class="hljs-string">"echo \"Error: no test specified\" &amp;&amp; exit 1"</span>
  },
  <span class="hljs-attr">"repository"</span>: {
    <span class="hljs-attr">"type"</span>: <span class="hljs-string">"git"</span>,
    <span class="hljs-attr">"url"</span>: <span class="hljs-string">"git+https://github.com/thecodercoder/Super-Simple-Gulp-File.git"</span>
  },
  <span class="hljs-attr">"keywords"</span>: [
    <span class="hljs-string">"gulp"</span>
  ],
  <span class="hljs-attr">"author"</span>: <span class="hljs-string">"Jessica @thecodercoder"</span>,
  <span class="hljs-attr">"license"</span>: <span class="hljs-string">"ISC"</span>,
  <span class="hljs-attr">"bugs"</span>: {
    <span class="hljs-attr">"url"</span>: <span class="hljs-string">"https://github.com/thecodercoder/Super-Simple-Gulp-File/issues"</span>
  },
  <span class="hljs-attr">"homepage"</span>: <span class="hljs-string">"https://github.com/thecodercoder/Super-Simple-Gulp-File#readme"</span>
}
</code></pre>
<p>Of course, for your project you’ll have your own name and information instead of what I have here.</p>
<p>At this point I wouldn’t worry about getting all the fields correct. This informational part is mainly used for packages that get published to npm as public plugins.</p>
<p>Now, what you <strong>will</strong> be putting into your package.json file is the list of all the packages that you need for running Gulp.</p>
<p>Let’s see just how you can add them in.</p>
<h4 id="heading-installing-packages"><strong>Installing packages</strong></h4>
<p>In the previous section above, we talked about typing: <code>npm install [Package Name]</code> into your command line to download and install the package into your node_modules folder.</p>
<p>It will install the package and automatically save it to your package.json file as a dependency.</p>
<p><strong>Note:</strong> Prior to npm version 5.0.0, you had to add the flag “–save” in order for npm to add the package as a dependency. You no longer have to do that with versions 5 and up.</p>
<p>So if we want to install Gulp to our packages, we’d type in: <code>npm install gulp</code>.</p>
<p>It might take a minute or two for your computer to install everything related to Gulp. You will likely see some warning messages, but I wouldn’t worry about those unless the install fails.</p>
<p>Now, if you open your package.json file, you’ll see at the bottom that Gulp has been added as a dependency:</p>
<pre><code><span class="hljs-string">"dependencies"</span>: { <span class="hljs-string">"gulp"</span>: <span class="hljs-string">"^3.9.1"</span> }
</code></pre><p>This list of dependencies will grow as you install additional npm packages.</p>
<h4 id="heading-other-packages-needed-for-gulp"><strong>Other packages needed for Gulp</strong></h4>
<p>Initially, we wanted to use Gulp to run tasks like compiling your SCSS/CSS and JavaScript files. To accomplish this, we’ll be using the following packages:</p>
<ul>
<li><a target="_blank" href="https://www.npmjs.com/package/gulp-sass">gulp-sass</a> — compiles your Sass files into CSS</li>
<li><a target="_blank" href="https://www.npmjs.com/package/gulp-cssnano">gulp-cssnano</a> — minifies your CSS files</li>
<li><a target="_blank" href="https://www.npmjs.com/package/gulp-concat">gulp-concat</a> — concatenates (combines) multiple JavaScript files into one large file</li>
<li><a target="_blank" href="https://www.npmjs.com/package/gulp-uglify">gulp-uglify</a> — minifies your JavaScript files</li>
</ul>
<p>Just like before, install each package by typing these lines one by one. You’ll have to wait a few seconds while each one installs before going on to the next line.</p>
<pre><code>npm install gulp-sass 
npm install gulp-cssnano 
npm install gulp-concat 
npm install gulp-uglify
</code></pre><h4 id="heading-gulp-cli-vs-global-gulp"><strong>Gulp-cli vs global Gulp</strong></h4>
<p>In the past, to be able to run “gulp” from your command line, you would have to install Gulp globally on your local computer, using the command:  </p>
<p><code>npm install –global gulp</code></p>
<p>However, having a single global version of Gulp could cause issues if you have multiple projects all requiring different versions of Gulp.</p>
<p>The current consensus recommends installing a different package, <a target="_blank" href="https://gulpjs.org/getting-started">Gulp-cli</a>, globally instead of Gulp itself.</p>
<p>This will allow you to still run the “gulp” command, but you’re able to use different versions of Gulp across your different projects.</p>
<p>Here’s the code for that:</p>
<pre><code>npm install --<span class="hljs-built_in">global</span> gulp-cli
</code></pre><p>If you’re interested, you can read more context on this <a target="_blank" href="https://teamtreehouse.com/community/gulp-global-install-gulp-vs-gulpcli">Treehouse thread</a>.</p>
<p>All right, once all your packages are installed, you have all the tools you need. Let’s move on to setting up our project files!</p>
<h3 id="heading-set-up-your-file-structure">Set up your file structure</h3>
<p>Before we start creating files and folders, just know that there are many different ways to set up your file structure. The approach that you’ll be using is good for basic projects, but the “right” setup will depend a lot on what your particular needs are.</p>
<p>This basic method will help you get a grasp on the basic functionality of all the moving parts. Then you can build off or change the setup to your own liking in the future!</p>
<p>Here’s what the project tree will look like:</p>
<p><strong>Root Project Folder</strong></p>
<ul>
<li>index.html</li>
<li>gulpfile.js</li>
<li>package.json</li>
<li>node_modules (folder)</li>
<li>app (folder)</li>
<li>script.js</li>
<li>style.scss</li>
<li>dist (folder)</li>
</ul>
<p>We already went over the package.json file and the node_modules folder. And the index.html file will be, of course, your main website file.</p>
<p>The gulpfile.js file is where we’ll configure Gulp to run all the tasks we talked about at the beginning of this article. We’ll get into that in a bit.</p>
<p>But right now I want to mention the two folders, app and dist, as they’re important for the Gulp workflow.</p>
<h4 id="heading-app-and-dist-folders"><strong>App and dist folders</strong></h4>
<p>In the app folder, we have your basic JavaScript file (script.js) and your basic SCSS file (style.scss). Those files are where you will write all your JavaScript and CSS code.</p>
<p>The dist folder exists only to store the final compiled JavaScript and CSS files after Gulp has processed them. You shouldn’t make any changes in the dist files, only the app files. But these files in dist are what will be loaded in index.html, since we want to use the compiled files in the website.</p>
<p>Again, there are lots of ways you can set up your project files and folders. The main important thing to keep in mind is that your structure makes sense and allows you to work the most efficiently.</p>
<p>Now let’s get to the meat of this tutorial: configuring Gulp!</p>
<h3 id="heading-create-and-configure-your-gulpfile">Create and configure your Gulpfile</h3>
<p>The Gulpfile contains the code to load installed packages and run different functions. The code performs two basic functions:</p>
<ol>
<li>Initialize your installed packages as Node modules.</li>
<li>Create and run Gulp tasks.</li>
</ol>
<h4 id="heading-initialize-packages"><strong>Initialize packages</strong></h4>
<p>In order to take advantage of all the features of the npm packages you added to your project, you need to export them as modules in Node — hence the folder name “node_modules”.</p>
<p>At the top of your Gulpfile, add the modules like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> gulp = <span class="hljs-built_in">require</span>(<span class="hljs-string">'gulp'</span>); 
<span class="hljs-keyword">var</span> cssnano = <span class="hljs-built_in">require</span>(<span class="hljs-string">'gulp-cssnano'</span>); 
<span class="hljs-keyword">var</span> sass = <span class="hljs-built_in">require</span>(<span class="hljs-string">'gulp-sass'</span>); 
<span class="hljs-keyword">var</span> concat = <span class="hljs-built_in">require</span>(<span class="hljs-string">'gulp-concat'</span>); 
<span class="hljs-keyword">var</span> uglify = <span class="hljs-built_in">require</span>(<span class="hljs-string">'gulp-uglify'</span>);
</code></pre>
<p>Now that the packages are added, you can then use their functions and objects in your Gulpfile scripts. You’ll also be using some built-in functions that are part of Node.js.</p>
<p>If you want to read more about npm packages and Node modules, the npm site has a great explanation <a target="_blank" href="https://docs.npmjs.com/getting-started/packages">here</a>.</p>
<h3 id="heading-create-your-gulp-tasks"><strong>Create your Gulp tasks</strong></h3>
<p>Creating a Gulp task is done by using the following code:</p>
<pre><code class="lang-javascript">gulp.task(<span class="hljs-string">'[Function Name]'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{    
   <span class="hljs-comment">// Do stuff here </span>
}
</code></pre>
<p>This allows you to run the Gulp task by typing in <code>gulp [Function Name]</code> into the command line. This is important because you can then run that named function from other Gulp tasks.</p>
<p>Specifically, we are building several different Gulp tasks, which will <strong>all</strong> be run when you run the default Gulp task.</p>
<p>Some of the main functions that we’ll be using are:</p>
<ul>
<li><code>.task()</code> — Creates a task, as mentioned above</li>
<li><code>.src()</code> — identifies what files you will be compiling in a particular task</li>
<li><code>.pipe()</code> — adds a function to the Node stream that Gulp is using; you can pipe multiple functions in the same task (read an excellent write-up on this topic on <a target="_blank" href="https://florian.ec/articles/gulp-js-streams/">florian.ec</a>)</li>
<li><code>.dest()</code> — writes the resulting file to a specific location</li>
<li><code>.watch()</code> — identifies the files to detect any changes</li>
</ul>
<p>If you’re curious, you can read up more on the Gulp documentation <a target="_blank" href="https://github.com/gulpjs/gulp/tree/v3.9.1/docs">here</a>.</p>
<p>All set? Now let’s get down to business (cue Mulan music) and write those tasks!</p>
<p>These are the following tasks that we want Gulp to run:</p>
<ul>
<li>Sass task to compile SCSS to a CSS file and minify</li>
<li>JavaScript task to concatenate the JavaScript files and minify/uglify</li>
<li>Watch task to detect when SCSS or JavaScript files are changed, and re-run the tasks</li>
<li>Default task to run all needed tasks when you type <code>gulp</code> into the command line</li>
</ul>
<h4 id="heading-sass-task"><strong>Sass task</strong></h4>
<p>For the Sass task, first we want to create the task in Gulp using <code>task()</code>, and we will name it “sass.”</p>
<p>Then we add in each component that the task will run. First we will designate that the source will be the app/scss/style.scss file, using <code>src()</code>. Then we will pipe in the additional functions.</p>
<p>The first one runs the <code>sass()</code> function — using the gulp-sass module which we called “sass” at the top of the Gulpfile. It will automatically save the CSS file with the same name as the SCSS file, so ours will be named style.css.</p>
<p>The second one minifies the CSS file with <code>cssnano()</code>. And the last puts the resulting CSS file in the dist/css folder.</p>
<p>Here’s the code for all that:</p>
<pre><code class="lang-javascript">gulp.task(<span class="hljs-string">'sass'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{    
    <span class="hljs-keyword">return</span> gulp.src(<span class="hljs-string">'app/style.scss'</span>)       
        .pipe(sass())       
        .pipe(cssnano())       
        .pipe(gulp.dest(<span class="hljs-string">'dist/css'</span>)); 
});
</code></pre>
<p>To test, I just put in some filler SCSS in the style.scss file:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">div</span> {    
    <span class="hljs-attribute">display</span>: block; 
       &amp;.content {       
        <span class="hljs-attribute">position</span>: relative;    
    } 
} 

<span class="hljs-selector-class">.red</span> { 
    <span class="hljs-attribute">color</span>: red; 
}
</code></pre>
<p>You can run each individual Gulp task on the command line if you type <code>gulp</code> and the name of the task. So to test the Sass task, I typed in <code>gulp sass</code> to check if it works without errors, and generates the minified dist/style.css file.</p>
<p>If everything works correctly, you should see messaging like this in your command line:</p>
<pre><code>[<span class="hljs-number">15</span>:<span class="hljs-number">04</span>:<span class="hljs-number">53</span>] Starting <span class="hljs-string">'sass'</span>... [<span class="hljs-number">15</span>:<span class="hljs-number">04</span>:<span class="hljs-number">53</span>] Finished <span class="hljs-string">'sass'</span> after <span class="hljs-number">121</span> ms
</code></pre><p>Checking in the dist folder shows that there is indeed a style.css file, and opening it shows correctly-minified CSS:</p>
<pre><code>div{<span class="hljs-attr">display</span>:block}div.content{<span class="hljs-attr">position</span>:relative}.red{<span class="hljs-attr">color</span>:red}
</code></pre><p>Ok, our Sass task is now done. On to JavaScript!</p>
<h4 id="heading-js-task">JS <strong>task</strong></h4>
<p>The JS Gulp task is similar to the Sass task, but has a few different elements.</p>
<p>First we’ll create the task and call it “js,” then we’ll identify the source files. In the <code>src()</code> function, you can identify multiple files a couple different ways.</p>
<p>One is to utilize the wildcard <code>(*)</code> to tell Gulp to use all files with the <code>*.js</code> extension like this:</p>
<pre><code>gulp.src(<span class="hljs-string">'app/*.js'</span>)
</code></pre><p>However this will compile the files in alphabetical order, which could potentially cause errors if you end up loading scripts that are dependent on other scripts before those other script files.</p>
<p>You can control the order by manually designating each JavaScript file if you don’t have too many script files.</p>
<p>The <code>src()</code> function can take an array of values as a parameter, by using the square brackets like this:</p>
<pre><code>gulp.src([<span class="hljs-string">'app/script.js'</span>, <span class="hljs-string">'app/script2.js'</span>])
</code></pre><p>If you do have a lot of JavaScript files, you can make sure that you load dependencies first by keeping them in a separate sub-folder, like say “app/js/plugins”. Then keep other JavaScript files in the parent “app/js” folder.</p>
<p>Then you can use the wildcard notation to load all lib (library) scripts, followed by regular scripts:</p>
<pre><code>gulp.src([<span class="hljs-string">'app/js/lib/*.js'</span>, <span class="hljs-string">'app/js/script/*.js'</span>])
</code></pre><p>Your approach will vary depending on the number and types of JavaScript files you have.</p>
<p>Once you’ve set your source files, you’ll pipe in the remaining functions. The first is to concatenate the files into one large JavaScript file. The <code>concat()</code> function requires one parameter with the name of the resulting file.</p>
<p>Then you’ll uglify the JavaScript file, and save it in the destination location.</p>
<p>Here’s the complete code for the JS task:</p>
<pre><code class="lang-javascript">gulp.task(<span class="hljs-string">'js'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{    
    <span class="hljs-keyword">return</span> gulp.src([<span class="hljs-string">'app/js/plugins/*.js'</span>, <span class="hljs-string">'app/js/*.js'</span>])          
        .pipe(concat(<span class="hljs-string">'all.js'</span>))       
        .pipe(uglify())       
        .pipe(gulp.dest(<span class="hljs-string">'dist'</span>)); 
});
</code></pre>
<p>Just like the Sass task, you can test that the JS task works by typing in <code>gulp js</code> into the command line.</p>
<pre><code>[<span class="hljs-number">14</span>:<span class="hljs-number">38</span>:<span class="hljs-number">31</span>] Starting <span class="hljs-string">'js'</span>... [<span class="hljs-number">14</span>:<span class="hljs-number">38</span>:<span class="hljs-number">31</span>] Finished <span class="hljs-string">'js'</span> after <span class="hljs-number">36</span> ms
</code></pre><p>Now that we’ve finished our main two worker Gulp tasks, we can move on to the Watch task.</p>
<h4 id="heading-watch-task"><strong>Watch task</strong></h4>
<p>The Watch task will watch the files that you tell it to for any changes. Once it detects a change, it will run the tasks that you designate and then continue watching for changes.</p>
<p>We will create two watch functions, one to watch SCSS files and the other to watch JavaScript files.</p>
<p>The <code>watch()</code> function takes two parameters: the source location, and then the tasks to run when a change is detected.</p>
<p>The Sass Watch function will watch any SCSS files in the app folder and then run the Sass task if it detects changes.</p>
<p>The function will look like this:</p>
<pre><code>gulp.watch(<span class="hljs-string">'app/*.scss'</span>, [<span class="hljs-string">'sass'</span>]);
</code></pre><p>For the JS Watch function, we’ll have to take advantage of a really useful Node feature called “globbing.” Globbing refers to using the “**” symbols as a kind of wildcard for folders and subfolders. We need it for the JavaScript files, because we have a JavaScript file in the app/js folder, and a JavaScript file in the app/js/plugins folder.</p>
<p>And here’s what that function will look like:</p>
<pre><code>gulp.watch(<span class="hljs-string">'app/js/**/*.js'</span>, [<span class="hljs-string">'js'</span>]);
</code></pre><p>The way the glob (“**”) works is it will look for JavaScript files anywhere in the app/js folder. It will look either directly in the folder or in any subsequent child folders, like the plugins folder. Globbing comes in handy so that you don’t have to designate each sub-folder as a separate source in the <code>watch()</code> function.</p>
<p>Here’s the complete Watch task:</p>
<pre><code class="lang-javascript">gulp.task(<span class="hljs-string">'watch'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{       
    gulp.watch(<span class="hljs-string">'app/*.scss'</span>, [<span class="hljs-string">'sass'</span>]);          
    gulp.watch(<span class="hljs-string">'app/js/**/*.js'</span>, [<span class="hljs-string">'js'</span>]); 
});
</code></pre>
<p>Now we’re almost done! The last task to create is the default Gulp task.</p>
<h4 id="heading-default-gulp-task"><strong>Default Gulp task</strong></h4>
<p>The default Gulp task is what you want to run when you just type in <code>gulp</code> in the command line. When you create the task, you have to call it “default” in order for Gulp to recognize that that’s what you want to run.</p>
<p>What we’d like to do is run the Sass and JS tasks once, then run the Watch task to re-run tasks when files are changed.</p>
<pre><code>gulp.task(<span class="hljs-string">'default'</span>, [<span class="hljs-string">'sass'</span>, <span class="hljs-string">'js'</span>, <span class="hljs-string">'watch'</span>]);
</code></pre><p>You can create other tasks to run your builds, just don’t reuse the “default” name. For instance, let’s say you want to leave your CSS and JavaScript files unminified by default, but you do want to minify them for production.</p>
<p>You could create separate Gulp tasks to minify your CSS and JavaScript files called “minifyCSS” and “minifyJS.” Then you wouldn’t add those tasks to your default Gulp task, but you could create a new Gulp task called “prod” that has everything the default task has, and also has your minify tasks.</p>
<h4 id="heading-references-in-your-indexhtml"><strong>References in your index.html</strong></h4>
<p>Once you’ve gotten your Gulp process working, make sure that your index.html file references all the correct CSS and JavaScript files.</p>
<p>For the examples I’ve given you here, you’ll want to add a CSS reference to <code>dist/style.css</code> in your :</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"dist/style.css"</span>&gt;</span>
</code></pre>
<p>And add a script tag referencing <code>dist/all.js</code> in your :</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"dist/all.js"</span>&gt;</span><span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">;</span>/<span class="hljs-attr">script</span>&gt;</span></span>
</code></pre>
<h3 id="heading-in-closing">In closing</h3>
<p>Congrats on making it through! I hope that you found this basic Gulp tutorial helpful.</p>
<p>Like I mentioned at the beginning, this is just a very simple tutorial of the basics of npm and Gulp.</p>
<p>Most devs add many additional tasks to their Gulpfile. Let me know if you’d be interested to see another article on those more advanced topics!</p>
<p>Lastly, you can check out all the code from this tutorial on my GitHub account <a target="_blank" href="https://github.com/thecodercoder/Super-Simple-Gulp-File">here</a>.</p>
<p>I hope you found this post helpful! Let me know any thoughts you have in the comments below.</p>
<h4 id="heading-want-more">Want more?</h4>
<ul>
<li>Read more tutorials on my blog, c<a target="_blank" href="https://coder-coder.com">oder-coder.com.</a></li>
<li>Si<a target="_blank" href="https://coder-coder.com/subscribe">gn up here to get emails about new articles.</a></li>
<li>Join 25,000+ others — Follow @th<a target="_blank" href="https://www.instagram.com/thecodercoder/">ecodercoder on Instagram.</a></li>
<li>Check out coding tutorials on <a target="_blank" href="https://www.youtube.com/c/codercodertv">my YouTube channel</a>.</li>
</ul>
<p><em>This post was originally published on <a target="_blank" href="https://coder-coder.com/gulp-tutorial-beginners/">Coder-Coder.com</a>.</em></p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Gulp! I Improved my Workflow! ]]>
                </title>
                <description>
                    <![CDATA[ By Stefano Grassi yet another hands-on experience with Gulp.js _Jökulsárlón, Iceland by [Jeremy Goldberg](https://unsplash.com/jeremy" rel="noopener" target="blank" title=") Unless you’ve been living under a rock for the past few years, the number o... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/gulp-i-improved-my-workflow-354d31d25655/</link>
                <guid isPermaLink="false">66c34bfa30aba6677fb9f9c0</guid>
                
                    <category>
                        <![CDATA[ Front-end Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Gulp ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 17 Oct 2015 22:33:35 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*3vp5N6O1BBr79sdNtU6cQg.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Stefano Grassi</p>
<h4 id="heading-yet-another-hands-on-experience-with-gulpjs">yet another hands-on experience with Gulp.js</h4>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*3vp5N6O1BBr79sdNtU6cQg.jpeg" alt="Image" width="800" height="363" loading="lazy">
_Jökulsárlón, Iceland by [Jeremy Goldberg](https://unsplash.com/jeremy" rel="noopener" target="<em>blank" title=")</em></p>
<p>Unless you’ve been living under a rock for the past few years, the number of tools at the disposal of front-end developers have grown rapidly. What we have now is a wide range of projects dedicated to speed-up, automate and increase the quality of our workflow.</p>
<p>For example, just imagine:</p>
<ul>
<li>compiling <a target="_blank" href="https://www.npmjs.com/package/gulp-sass">SASS</a>/<a target="_blank" href="https://www.npmjs.com/package/gulp-less">LESS</a>/<a target="_blank" href="https://www.npmjs.com/package/postcss">PostCSS</a>/<a target="_blank" href="https://www.npmjs.com/package/gulp-stylus">Stylus</a> to a minified CSS, <a target="_blank" href="https://www.npmjs.com/package/gulp-uncss">tailored</a> to your needs, <a target="_blank" href="https://www.npmjs.com/package/gulp-autoprefixer">auto-prefixed</a> and in real-time</li>
<li><a target="_blank" href="https://www.npmjs.com/package/gulp-concat">concatenating</a> and <a target="_blank" href="https://www.npmjs.com/package/gulp-uglify">minifying</a> your scripts</li>
<li>compressing html files created from <a target="_blank" href="https://www.npmjs.com/package/gulp-file-include">templates</a> and atomic modules</li>
<li><a target="_blank" href="http://www.browsersync.io/">preview</a> your webapp from a virtual server during the compilation, auto-refreshed and synced to all your devices</li>
<li>test your web <a target="_blank" href="https://www.npmjs.com/package/gulp-sitespeedio">performance</a></li>
<li>self-updating style-<a target="_blank" href="https://trulia.github.io/hologram/">guide</a> of the project</li>
<li><a target="_blank" href="https://www.npmjs.com/package/gulp-imagemin">compressing</a> images and creating <a target="_blank" href="https://www.npmjs.com/package/gulp.spritesmith">sprites</a></li>
</ul>
<p>Some years ago this sounded more like a Disneyan dream but we’re living in the future, so fear not! <a target="_blank" href="http://gruntjs.com/">Grunt</a>, <a target="_blank" href="http://mimosa.io/">Mimosa</a>, <a target="_blank" href="http://broccolijs.com/">Broccoli</a> and <a target="_blank" href="http://gulpjs.com/">Gulp</a> to the rescue!</p>
<p>Each system has its own strong points. I’ve chosen Gulp for my needs but make sure to check them all out before deciding which best suits you.</p>
<h4 id="heading-so-gulp-whats-that">So… gulp? What’s that?</h4>
<p><a target="_blank" href="https://github.com/gulpjs/gulp/blob/master/docs/getting-started.md"><strong>gulpjs/gulp</strong></a><br><a target="_blank" href="https://github.com/gulpjs/gulp/blob/master/docs/getting-started.md">_gulp — The streaming build system_github.com</a></p>
<p>As its site states, Gulp is a “<a target="_blank" href="http://gulpjs.com/">streaming build system</a>” which means that you can set your own tasks to be run on a pipeline, monitor a folder for a change and re-run.</p>
<p>And it’s <strong>super simple</strong>.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*_PQJkvbZJNE_BjXpvIGCPQ.jpeg" alt="Image" width="431" height="300" loading="lazy">
<em>That’s ingenious, if I understand it correctly.<br>It’s a Swiss watch.<br>Yeah, the beauty of this is its simplicity.</em></p>
<h4 id="heading-gulp-basic-concepts">Gulp Basic Concepts</h4>
<p>Let’s sip through the main elements</p>
<p><strong>gulp.task</strong><br>aka the action you want to achieve. Managing CSS? Generating the docs?<br>Gulp define them with <a target="_blank" href="https://github.com/robrich/orchestrator">orchestrator</a>, a module that allows us to define dependencies and maximum concurrency</p>
<pre><code>gulp.task(‘somename’, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">// Do stuff});</span>
</code></pre><p><img src="https://cdn-media-1.freecodecamp.org/images/1*MRor084bOQstofwPYVjaFQ.jpeg" alt="Image" width="720" height="187" loading="lazy"></p>
<p><strong>gulp.watch</strong><br>aka the folders you want to keep checked for changes</p>
<pre><code>gulp.watch(‘folder<span class="hljs-comment">/*.ext’, [‘firstTask’, ‘secondTask’]);</span>
</code></pre><p>Every <strong>stream</strong> originates from a source(s) matching a specific <strong>glob</strong> (a pattern that a file needs to match)</p>
<pre><code>gulp.src(globs[, options])
</code></pre><p>a series of <strong>pipes</strong> (actions)</p>
<pre><code>.pipe(concat()),.pipe(minify())
</code></pre><p>and a <strong>destination</strong> defined with</p>
<pre><code>gulp.dest(path[, options])
</code></pre><p>To operate, gulp needs two core files, <strong>package.json</strong> and <strong>gulpfile.js.</strong><br><em>(For the installation of gulp, follow the official docs)</em></p>
<h4 id="heading-gulpfilejs">Gulpfile.js</h4>
<p>In the <strong>gulpfile</strong> we’ll declare which plugins are we going to use, the tasks we want to run, which folders we’re going to watch, etc…</p>
<h4 id="heading-packagejson">Package.json</h4>
<p>The <strong>package.json</strong> file is used to store all the information regarding the dependencies of the project (gulp included!).</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*p1_LFP4jZEH6b9asHPueyg.jpeg" alt="Image" width="720" height="480" loading="lazy"></p>
<ul>
<li>To <strong>create</strong> it</li>
</ul>
<pre><code>$ npm init
</code></pre><p>You’ll be prompted to enter some basic info for the heading of the file, like the author name, the project name and so on.</p>
<ul>
<li>To <strong>install</strong> a plugin and save the dependency on the json file</li>
</ul>
<pre><code>$ npm install --save-dev yourPluginName
</code></pre><ul>
<li>To <strong>uninstall</strong> a plugin and remove the dependency on the json file</li>
</ul>
<pre><code>$ npm uninstall --save-dev yourPluginName
</code></pre><ul>
<li>If you need to <strong>install all the dependencies</strong> from a compiled package.json</li>
</ul>
<pre><code>$ npm install
</code></pre><h4 id="heading-project-organization">Project Organization</h4>
<p>This is my approach to organize the folder of a project managed with Gulp</p>
<h4 id="heading-plugins-ftw">Plugins FTW!</h4>
<p>Gulp has an impressing list of plugins (<strong>1895</strong> at the time I’m writing this article)</p>
<p><a target="_blank" href="http://gulpjs.com/plugins/"><strong>gulp.js plugin registry</strong></a><br><a target="_blank" href="http://gulpjs.com/plugins/">_Discover gulp.js plugins_gulpjs.com</a></p>
<p><strong>Must Have</strong></p>
<ul>
<li><a target="_blank" href="https://github.com/jackfranklin/gulp-load-plugins">gulp-load-plugins</a><br>This lazy-loads the plugins installed in your project. You assign a variable to it, and use it to reference other plugins instead of repeating the requirement declaration for every other plugin.</li>
</ul>
<pre><code><span class="hljs-keyword">var</span> $ = <span class="hljs-built_in">require</span>(<span class="hljs-string">'gulp-load-plugins'</span>)();
</code></pre><pre><code><span class="hljs-comment">// Example for gulp-concat.pipe($.concat('main.js'))</span>
</code></pre><ul>
<li><a target="_blank" href="http://www.browsersync.io/docs/gulp/">browsersync</a><br>page refresh at any change on every device connected to the same URL (localhost or LAN)</li>
<li><a target="_blank" href="https://www.sitespeed.io">sitespeed</a><br>my favourite tool for performance testing</li>
<li><a target="_blank" href="https://github.com/giakki/uncss">uncss</a><br>are you using a CSS framework like Bootstrap for a landing page?<br>You NEED this.</li>
</ul>
<h4 id="heading-what-how-do-i-update-gulp-plugins-you-ask">What? How do I update Gulp plugins, you ask?</h4>
<pre><code>$ npm install -g npm-check-updates
</code></pre><pre><code>$ npm-check-updates -u
</code></pre><pre><code>$ rm -fr node_modules
</code></pre><pre><code>$ npm install
</code></pre><blockquote>
<p>Basically this installs <strong>npm-check-updates</strong> globally, runs it against your package.json and updates the dependency versions.<br>Then you just delete the node modules folder and re-install.</p>
<p>from: <a target="_blank" href="https://stackoverflow.com/questions/27024431/updating-gulp-plugins">https://stackoverflow.com/questions/27024431/updating-gulp-plugins</a></p>
</blockquote>
<p>Note: As a general rule, and as a last resort, we better <strong>clean</strong> the npm cache with</p>
<pre><code>$ npm cache clean
</code></pre><h4 id="heading-thats-all-folks-thank-you-for-reaching-this-point"><em>That’s all, folks! Thank you for reaching this point!</em></h4>
<h4 id="heading-i-hope-that-i-kept-you-interested-enough-to-check-some-of-the-links-that-stuffed-this-article-theyre-there-because-i-wanted-to-show-my-support-for-all-the-great-work-that-fellow-developers-are-doing-for-the-community"><em>I hope that I kept you interested enough to check some of the links that stuffed this article. They’re there because I wanted to show my support for all the great work that fellow developers are doing for the community.</em></h4>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
