<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Node &#8211; Cognim &#8211; Internet development</title>
	<atom:link href="https://www.cognim.co.uk/category/node/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.cognim.co.uk</link>
	<description>Enterprise system implementation. Making the complex simple</description>
	<lastBuildDate>Fri, 07 Aug 2015 10:09:23 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">91553907</site>	<item>
		<title>Getting started with Gulp in Visual Studio 2013 and 2015</title>
		<link>https://www.cognim.co.uk/getting-started-with-gulp-and-vs/</link>
					<comments>https://www.cognim.co.uk/getting-started-with-gulp-and-vs/#comments</comments>
		
		<dc:creator><![CDATA[Darren Hall]]></dc:creator>
		<pubDate>Fri, 07 Aug 2015 09:56:50 +0000</pubDate>
				<category><![CDATA[Gulp]]></category>
		<category><![CDATA[Node]]></category>
		<category><![CDATA[NodeJs]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<guid isPermaLink="false">http://www.cognim.co.uk/?p=5148</guid>

					<description><![CDATA[In the last few years a rich set of front end development tools has emerged for building complex web applications. Gulp in particular has taken off in a huge way. However, .NET development is lagging when it comes to adoption of these tools. This is all set to change, especially with the release of ASP.NET 5 where Gulp will become [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In the last few years a rich set of front end development tools has emerged for building complex web applications. Gulp in particular has taken off in a huge way. However, .NET development is lagging when it comes to adoption of these tools. This is all set to change, especially with the release of ASP.NET 5 where Gulp will become part of the standard project setup. But, what is Gulp, and how do you use it in Visual Studio?</p>
<h2>What is Gulp?</h2>
<p><a href="http://gulpjs.com/">Gulp</a> is a build process for front end development. Gulp automates all of the repetitive tasks you have to do to prepare your front end files (such as JavaScript and CSS) for development and deployment. Gulp uses small, simple, composable plugins to that let you define exactly what happens to the files in your build.</p>
<h4>Common tasks you can get Gulp to automate</h4>
<ul>
<li>Minifying JavaScript/CSS/images</li>
<li>Concatenating JavaScipt/CSS files</li>
<li>Running JavaScript lint tooling</li>
<li>Compiling your SASS/LESS</li>
<li>Compiling TypeScript/CoffeeScript</li>
<li>Running JavaScript tests (eg Karma, Jasmine, Mocha)</li>
<li>Transpiling ES6 into ES5</li>
<li>Minifying Angular without breaking Dependency Injection</li>
<li>&#8230;and many more</li>
</ul>
<p>To install and use Gulp, you&#8217;ll need npm. Which begs the question&#8230;</p>
<h4>What is npm?</h4>
<p>npm is the package manager for [simple_tooltip content=&#8217;Node is a runtime environment built in JavaScript. It is somewhat equivalent to the .NET CLR &#8211; and for getting set up it is not necessary to know much more than that&#8217;]<span style="text-decoration: underline;">Node</span>[/simple_tooltip]. It&#8217;s a front-end equivalent to NuGet &#8211; you can use it to install and update packages and their dependencies. We&#8217;ll be using it to install Gulp and all its plugins.</p>
<h2>How to set up Gulp in Visual Studio</h2>
<p>Currently you can set up Gulp in Visual Studio 2013 and 2015. If you have 2015, skip the first 3 points as they come with Visual Studio 2015. Note that the screenshots are taken using Visual Studio 2013 with a standard MVC 5 project template.</p>
<ol>
<li>
<h4>Install Node/npm.</h4>
<p>You can install Node <a href="https://nodejs.org/" target="_blank">from here</a>. What we&#8217;re really after here isn&#8217;t Node, it&#8217;s npm (which comes installed with Node).</li>
<li>
<h4>Install Gulp.</h4>
<p>After installing npm, open up a Console window, and enter:</p>
<blockquote><p>npm install gulp -g</p></blockquote>
<p>This will install Gulp. The &#8220;-g&#8221; option installs it globally, so you can run Gulp commands anywhere.</li>
<li>
<h4>Install the Grunt Launcher Visual Studio Extension.</h4>
[simple_tooltip content=&#8217;Grunt is another build process for front end development. Grunt favours configuration, while Gulp favours code. I am not going to get into the merits of each here!&#8217;]<a href="https://visualstudiogallery.msdn.microsoft.com/dcbc5325-79ef-4b72-960e-0a51ee33a0ff" target="_blank">Grunt Launcher</a>[/simple_tooltip] provides menu options to install npm packages and run Gulp tasks from within Visual Studio.</li>
<li>
<h4>Add Gulp to your project</h4>
<p>At the root of your web project, add a new JSON file called package.json with the following contents:</p>
<pre class="lang:js decode:true">{
    "devDependencies": {
        "gulp":  "3.9.0"
    }   
}
</pre>
<p><a href="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-1.png?ssl=1"><img data-recalc-dims="1" decoding="async" loading="lazy" class="  wp-image-5173 aligncenter" src="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-1.png?resize=889%2C420&#038;ssl=1" alt="gulp-blog-1" width="889" height="420" srcset="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-1.png?resize=1024%2C484&amp;ssl=1 1024w, https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-1.png?resize=300%2C142&amp;ssl=1 300w, https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-1.png?w=1379&amp;ssl=1 1379w" sizes="auto, (max-width: 889px) 100vw, 889px" /></a> The [simple_tooltip content=&#8217;Note a standard package.json will contain other information such as a description, author and a repository for the code&#8217;]<span style="text-decoration: underline;">package.json</span>[/simple_tooltip] is a npm file we use to define metadata about our project. Here we are listing the (front end) dependencies for developing our project &#8211; this will include plugins for running Gulp tasks. For now, we just want to add Gulp to our project.</p>
<p>Now, right-click on the package.json file and click &#8220;NPM install packages&#8221; (if you&#8217;ve installed Grunt Launcher) to add it to our project.</p>
<p><a href="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/NPM-install-vid.gif?ssl=1"><img data-recalc-dims="1" decoding="async" loading="lazy" class=" size-full wp-image-5171 aligncenter" src="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/NPM-install-vid.gif?resize=784%2C600&#038;ssl=1" alt="NPM install vid" width="784" height="600" /></a></p>
<p>You can ignore those warnings for now &#8211; you can add the metadata it is complaining about later.</p>
<p>This will add a node_modules folder in your project folder, where all our npm packages will be kept. Within that will be Gulp.</p>
<p><a href="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-3.png?ssl=1"><img data-recalc-dims="1" decoding="async" loading="lazy" class="alignnone size-full wp-image-5179" src="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-3.png?resize=334%2C423&#038;ssl=1" alt="gulp-blog-3" width="334" height="423" srcset="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-3.png?w=334&amp;ssl=1 334w, https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-3.png?resize=237%2C300&amp;ssl=1 237w" sizes="auto, (max-width: 334px) 100vw, 334px" /></a></li>
<li>
<h4>Add a Gulpfile</h4>
<p>Next we want to add a Gulpfile, where we will define the steps for our build process. Add a JavaScript file called gulpfile.js to the root of the project, and add the following code:</p>
<pre class="striped:true lang:js decode:true">var gulp = require('gulp');

gulp.task('default', function () {
    console.log('Hello world!');
});
</pre>
<p>&nbsp;</p>
<p><a href="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-2.png?ssl=1"><img data-recalc-dims="1" decoding="async" loading="lazy" class="alignnone  wp-image-5174" src="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-2.png?resize=884%2C418&#038;ssl=1" alt="gulp-blog-2" width="884" height="418" srcset="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-2.png?resize=1024%2C484&amp;ssl=1 1024w, https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-2.png?resize=300%2C142&amp;ssl=1 300w, https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/gulp-blog-2.png?w=1379&amp;ssl=1 1379w" sizes="auto, (max-width: 884px) 100vw, 884px" /></a><br />
In line 1 we load Gulp. On line 3 we define a new Gulp task. A task is a step, or a set of steps, that Gulp will execute. A task takes a function that it will run &#8211; in this case we simply log to the console. A task also has a name &#8211; here it&#8217;s called &#8220;default&#8221; and as you might guess it runs automatically when we run Gulp.</p>
<p>We can now run this task by right-clicking on the gulpfile.js and clicking the &#8220;Gulp&#8221; menu item added by the Grunt Launcher extension.</p>
<p><a href="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/Run-Gulpfile-Vid.gif?ssl=1"><img data-recalc-dims="1" decoding="async" loading="lazy" class="alignnone size-full wp-image-5172" src="https://i0.wp.com/www.cognim.co.uk/wp-content/uploads/2015/08/Run-Gulpfile-Vid.gif?resize=764%2C600&#038;ssl=1" alt="Run Gulpfile Vid" width="764" height="600" /></a></p>
<p>&nbsp;</p>
<p>Now Visual Studio is running our task, and outputting &#8220;Hello world!&#8221; to the output window. We&#8217;re done! Well, not quite&#8230;</li>
</ol>
<h2>Doing something actually useful</h2>
<p>Hello World is all well and good, but what about automating something useful? One common task is minifying CSS files, and that&#8217;s what we&#8217;ll do here.</p>
<ol>
<li>
<h4>Add the plugins</h4>
<p>Change your package.json file to the following:</p>
<pre class="striped:true lang:js decode:true">{
    "devDependencies": {
        "gulp": "3.9.0",
        "gulp-uglifycss": "1.0.4",
        "gulp-rename": "1.2.2"
    }
}
</pre>
<p>Here we&#8217;ve added two new packages, gulp-uglifycss (which minifies css) and gulp-rename (which renames files passed into it). We can now use these in our task.</li>
<li>
<h4>Modify the task</h4>
<p>Change the gulpfile.js to the following:</p>
<pre class="striped:true nums:true nums-toggle:true lang:js decode:true">var gulp = require('gulp'),
    uglifycss = require('gulp-uglifycss'),
    rename = require('gulp-rename');

gulp.task('default', function () {
    return 
         gulp.src(['./Content/*.css', '!./Content/*.min.css'])
        .pipe(uglifycss())
        .pipe(rename({
            suffix: ".min",
            extname: ".css"
        }))
        .pipe(gulp.dest('./Content'));
});
</pre>
<p>Here&#8217;s a line by line breakdown of what the task is doing:<br />
Line 1-3: Add references to the plugins we just added in our package.json<br />
Line 7: Get the CSS files from our Content directory (excluding any .min files)<br />
Line 8: Pipe (i.e. pass) the files to gulp-uglifycss, which minifies them<br />
Line 9-12:  Rename the minified files to include a .min suffix<br />
Line 13: Save the minified files back to our Content folder</p>
<p>Now, we can run this task to automatically minify all our CSS files.</p>
<p>And there we are! We now have a process that will save us from a repetitive boring task, and we can build on this to automate lots of other tasks.</li>
</ol>
<h2>Next steps</h2>
<p>Note that for brevity I have excluded everything that isn&#8217;t necessary for getting Gulp set up in Visual Studio. There are a few things you will want to do next to get fully set up:</p>
<ol>
<li>
<h4>Install the Task Runner Explorer extension</h4>
<p>You can install the <a href="https://visualstudiogallery.msdn.microsoft.com/8e1b4368-4afb-467a-bc13-9650572db708" target="_blank">Task Runner Explorer</a> Visual Studio extension to run tasks automatically on each build or every time the project opens.</li>
<li>
<h4>Install NPM Package Intellisense</h4>
<p><a href="https://visualstudiogallery.msdn.microsoft.com/65748cdb-4087-497e-a394-2e3449c8e61e" target="_blank">NPM Package Intellisense</a> gives you package intellisense when installing packages and plugins.</li>
<li>
<h4>Add more plugins</h4>
<p>Nearly any task you want to automate will <a href="http://gulpjs.com/plugins" target="_blank">have a plugin here</a> &#8211; there are over 1500 at time of writing.</li>
</ol>
<p>Happy Gulping!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.cognim.co.uk/getting-started-with-gulp-and-vs/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5148</post-id>	</item>
	</channel>
</rss>
