<?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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David Dunn on Medium]]></title>
        <description><![CDATA[Stories by David Dunn on Medium]]></description>
        <link>https://medium.com/@dd4dev?source=rss-4f1c6d189edb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*cwmmI4AOCL8lgtIV.jpg</url>
            <title>Stories by David Dunn on Medium</title>
            <link>https://medium.com/@dd4dev?source=rss-4f1c6d189edb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 19:42:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dd4dev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to using gulp-sass with gulp 4]]></title>
            <link>https://medium.com/@dd4dev/introduction-to-using-gulp-sass-with-gulp-4-b48e80e35219?source=rss-4f1c6d189edb------2</link>
            <guid isPermaLink="false">https://medium.com/p/b48e80e35219</guid>
            <category><![CDATA[gulp]]></category>
            <category><![CDATA[gulp-sass]]></category>
            <category><![CDATA[gulp-watch]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[David Dunn]]></dc:creator>
            <pubDate>Fri, 15 Mar 2019 19:36:20 GMT</pubDate>
            <atom:updated>2019-03-15T19:43:19.115Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*z06zXJ8BBjJ7GgHn94dtCQ.png" /><figcaption><a href="https://www.liquidlight.co.uk/blog/creating-svg-sprites-using-gulp-and-sass/">image credit</a></figcaption></figure><p>This post is a quick introduction in setting up gulp-sass with the new gulp 4 syntax. I will take you through setting up your npm project, installing gulp and gulp-sass and how to create gulp tasks. Finally I will end with showing you how to make use of gulp’s watch method so that when you save changes to your sass file, gulp will automatically compile it into css for you.</p><p>Jump into the next section to get started!</p><p><strong>Installing npm and creating an npm project</strong></p><p>Before you can create an npm project you need to actually have npm installed, luckily npm provides some useful <a href="https://www.npmjs.com/get-npm">installation instructions</a> on how to do this.</p><p>Once you have npm installed open a command prompt and navigate to your project directory (the project you want to add gulp and gulp-sass to) and then initialise this project as an npm project by running the npm init command:</p><pre>npm init</pre><p>This will create a package.json file in your project. The package.json file holds information (metadata) about your project as well as the list of dependencies your project needs.</p><p><strong>Adding gulp and gulp-sass as project dependencies</strong></p><p>To add gulp and gulp-sass to your project you can use the command line tools that npm provides:</p><pre>npm install gulp --save-deps &amp;&amp; npm install gulp-sass --save-deps</pre><p>Using<strong> </strong>--save-dev stores gulp and gulp-sass as a dev dependency in the package.json file.</p><p><strong>Creating a gulpfile</strong></p><p>Now that you’ve installed npm, gulp and gulp-sass you can setup your gulpfile.js and if you’re new to gulp I’d highly recommend reading their documentation on <a href="https://gulpjs.com/docs/en/getting-started/javascript-and-gulpfiles">JavaScript and Gulpfiles</a> but to provide a high level view a gulpfile is the file that is run via the gulp command and it is where you define the tasks that you want gulp to actually run.</p><p>So in the root of your project create a file called gulpfile.js and then open this file in your preferred text editor.</p><p>Then we can require the gulp and gulp-sass packages in our gulpfile.js:</p><pre>&#39;use strict&#39;</pre><pre>const { src, dest } = require(&#39;gulp&#39;);</pre><pre>const sass = require(&#39;gulp-sass&#39;);</pre><p>Gulp 4 uses <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring">Destructuring assignment</a> which was added in ES6 and through it you can access various properties of gulp for a full list of the available properties and what they do you can view the <a href="https://gulpjs.com/docs/en/getting-started/quick-start">gulp documentation</a>.</p><p>For gulp-sass you only need src and dest which allows you to define a source location, such as your sass file and a destination location, where you want the compiled css to be saved.</p><p><strong>Adding a task for gulp-sass</strong></p><p>Defining a task for gulp is simply adding a JavaScript function to your gulpfile.js that contains logic for the task you want to define. In this case you will want to define a task for gulp-sass:</p><pre>function compileSass(done) {<br>  src(&#39;app/scss/app.scss&#39;)<br>  .pipe(sass().on(&#39;error&#39;, sass.logError))<br>  .pipe(dest(&#39;app/css&#39;));<br> done();<br>}</pre><p>The above example makes the following assumptions:</p><ul><li>Your sass file exists at app/scss/app.scss</li><li>You want your app.css file to be stored in app/css</li></ul><p>You can obviously update each of these locations to match the structure of your own project.</p><p>The .pipe method is provided by Nodejs and allows you to take data from a readable source and send (or pipe) it to some destination. In this case the readable source is the app.scss file which is piped into gulp-sass and which tries to compile the sass into css and assuming there was no error the css is finally piped to the destination file, app/css in this case.</p><p>Finally you need to call the done completion handler, this lets gulp know that this task has finished.</p><p>Now all you need to do is register the gulp task. In gulp 4 this is done by exporting the task:</p><pre>exports.compileSass = compileSass</pre><p>Your gulpfile.js should now look like this:</p><pre>&#39;use strict&#39;</pre><pre>const { src, dest } = require(&#39;gulp&#39;);</pre><pre>const sass = require(&#39;gulp-sass&#39;);</pre><pre>function compileSass(done) {<br>  src(&#39;app/scss/app.scss&#39;)<br>  .pipe(sass().on(&#39;error&#39;, sass.logError))<br>  .pipe(dest(&#39;app/css&#39;));<br> done();<br>}</pre><pre>exports.compileSass = compileSass</pre><p>You can run this gulp task via the command line:</p><pre>gulp compileSass</pre><p>Try adding some valid sass to your sass file and running the gulp task. You should see the compiled css in your destination file.</p><p><strong>Automatically compile sass on save</strong></p><p>Gulp provides a way for you to watch for changes to a file. If changes are made to a watched file gulp can automatically execute some defined tasks.</p><p>To use this you first need to access the watch property of gulp in the same way you accessed the src and dest properties previously:</p><pre>const { src, dest, watch } = require(&#39;gulp&#39;);</pre><p>Next you can create a new function that will act as the watcher for your sass compiler:</p><pre>function watchSass() {<br>   watch(&#39;app/scss/app.scss&#39;, compileSass);<br>}</pre><p>This line of code calls the watch method provided by gulp. As parameters to this method you provide the file you watch gulp to watch for changes as well as the function you want gulp to execute when a change is made to the watched file, in this case it’s the compileSass function.</p><p>Next you just need to add this new function as a gulp task:</p><pre>exports.watchSass = watchSass</pre><p>After which your completed gulpfile.js should look like this:</p><pre>&#39;use strict&#39;</pre><pre>const { src, dest } = require(&#39;gulp&#39;);</pre><pre>const sass = require(&#39;gulp-sass&#39;);</pre><pre>function compileSass(done) {<br>  src(&#39;app/scss/app.scss&#39;)<br>  .pipe(sass().on(&#39;error&#39;, sass.logError))<br>  .pipe(dest(&#39;app/css&#39;));<br> done();<br>}</pre><pre>function watchSass() {<br>   watch(&#39;app/scss/app.scss&#39;, compileSass);<br>}</pre><pre>exports.watchSass = watchSass</pre><p>Now if you run the watchSass task:</p><pre>gulp watchSass</pre><p>The gulp watcher will run in the command window, and anytime you save changes to the app.scss file gulp will execute the compileSass function.</p><p>That’s it! You’ve successfully added automation for compiling sass to your project. There’s much more gulp can do, including css-minify, starting a live server to easily and quickly view your changes in a browser, image optimisation and much much more.</p><p>I hope you found this useful and thanks for reading!</p><p>Feel free to <a href="https://twitter.com/dd4dev">follow me on twitter!</a> I will update when I release new content :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b48e80e35219" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>