{"id":925,"date":"2016-07-19T23:50:15","date_gmt":"2016-07-19T13:50:15","guid":{"rendered":"http:\/\/www.moneystock.net\/wp_e\/?p=925"},"modified":"2016-08-06T00:16:20","modified_gmt":"2016-08-05T14:16:20","slug":"how-to-use-bower-to-manage-front-end-library-in-visual-studio-2015-for-beginners","status":"publish","type":"post","link":"https:\/\/moneystock.net\/wp_e\/2016\/07\/19\/how-to-use-bower-to-manage-front-end-library-in-visual-studio-2015-for-beginners\/","title":{"rendered":"How to use bower to manage front-end library in Visual Studio 2015 : for beginners"},"content":{"rendered":"<h2>Background<\/h2>\n<p>While looking for the JS libary to populate folder as beautiful tree structure and hopefully having modern UI, I\u00a0found a useful angularjs based open source called, angular file manager(<a href=\"https:\/\/github.com\/joni2back\/angular-filemanager\">git<\/a>), which has beautiful and easy-to-use UI, worth to spend some time for my work.<\/p>\n<p style=\"text-align: justify;\">The manual of the app said &#8216;use\u00a0<span style=\"color: #999999;\"><em>bower install &#8211;save angular-filemanager&#8217;<\/em><\/span> to use in existing app. It sounded very easy, because it looks to me that that magic word looks doing all heavy lifting. However, I didn&#8217;t know how to use bower in my ASP.Net MVC 5 app within Visual Studio 2015.<\/p>\n<p style=\"text-align: justify;\">Searching a bit for how to use bower in Visual Studio 2015, I found bunch of great articles and blogs, but most of explanation and tutorial wasn&#8217;t that straightforward.<\/p>\n<p style=\"text-align: justify;\">So, I just write down my way to keep record of it. In VS 2015 it should be easier than what it used to be in VS 2013.<\/p>\n<h2 style=\"text-align: justify;\">Challenge<\/h2>\n<p style=\"text-align: justify;\">The first challenge was so many new tools involved. e.g. Node.js, npm, bower, gulp, grunt.<\/p>\n<p style=\"text-align: justify;\">To have a taste of Node.js,\u00a0https:\/\/johnpapa.net\/get-up-and-running-with-node-and-visual-studio\/ is well written by John Papa.<\/p>\n<p style=\"text-align: justify;\">If you still using VS 2013,\u00a0http:\/\/devkimchi.com\/1511\/integrating-grunt-and-bower-with-visual-studio-2013\/ will be great place to refer.<\/p>\n<p style=\"text-align: justify;\">I found that\u00a0https:\/\/chsakell.com\/2015\/09\/19\/typescript-angularjs-gulp-and-bower-in-visual-studio-2015\/ is the best to understand 101 around these package manager.<\/p>\n<p style=\"text-align: justify;\">For beginner like me, there were quite a few information to digest. So, let&#8217;s do this in an easiest way. After some reading, I decided to use bower to manage all front-end JS libaries and CSS libaries. I&#8217;ll use nuget only for .Net libraries. Reason for this decision is that 1) more and more front-end libraries and open-sources will be shared though bower 2) the way bower keep the dependency is simpler than nuget, which sometimes does not work clean as it ideally should do.<\/p>\n<h2 style=\"text-align: justify;\">How I did,<\/h2>\n<p>I created basic APS.Net MVC project which includes jquery and bootstrap as a default. I will replace those jquery and bootstrap in Scripts folder with ones managed by bower.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1. NPM<\/strong><\/p>\n<p>To start with, we need npm, bower and gulp. VS2015 comes with Node.js and Bower. So, just make sure we have both in Command Prompt.<\/p>\n<pre>npm -v<\/pre>\n<p>If npm is installed, you will see the version number. If not, go to Node.js home page and install it.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2. Bower<\/strong><\/p>\n<p>Check if bower has been installed already.<\/p>\n<pre>bower -v<\/pre>\n<p>If not, install it as global, using npm.<\/p>\n<pre>npm install -g bower<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>3. Gulp<\/strong><\/p>\n<p>Install gulp.<\/p>\n<pre>npm install -g gulp<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>4. Configure bower<\/strong><\/p>\n<p>Once created a default ASP.Net MVC web app, we can see jquery and other JS libaries are automatically installed in Scripts folder using Nuget.<\/p>\n<p><a href=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-934\" src=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower2.jpg\" alt=\"bower2\" width=\"289\" height=\"390\" srcset=\"https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower2.jpg 289w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower2-222x300.jpg 222w\" sizes=\"auto, (max-width: 289px) 100vw, 289px\" \/><\/a><\/p>\n<p>So, uninstall jquery, jquery.validate, bootstrap using NuGet.<\/p>\n<p>Now, we will add those back in using bower.<\/p>\n<p>Add bower configuration file to the project. VS 2015 already have a template for it.<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-933\" src=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower1.jpg\" alt=\"bower1\" width=\"942\" height=\"152\" srcset=\"https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower1.jpg 942w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower1-300x48.jpg 300w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower1-768x124.jpg 768w\" sizes=\"auto, (max-width: 942px) 100vw, 942px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">Edit the bower.json file, which has a package information we can \/ will define which library to use.<\/p>\n<p style=\"text-align: justify;\">Type in &#8216;jquery&#8217; as below and we will have an intellisense support automatically provided by VS 2015.<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-935\" src=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower3.png\" alt=\"bower3\" width=\"397\" height=\"312\" srcset=\"https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower3.png 397w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower3-300x236.png 300w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">We can choose version as well, which also magically loaded as an option to choose.<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower0.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-931\" src=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower0.png\" alt=\"bower0\" width=\"578\" height=\"220\" srcset=\"https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower0.png 578w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower0-300x114.png 300w\" sizes=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">By typing in library entries in dependencies section, all done about dependency definition.<\/p>\n<pre style=\"text-align: justify;\">{\r\n \"name\": \"ASP.NET\",\r\n \"private\": true,\r\n \"dependencies\": {\r\n \"jquery\": \"~3.1.0\",\r\n \"jquery-validate\": \"~1.15.0\",\r\n \"bootstrap\": \"~3.3.6\",\r\n \"lodash\": \"~4.13.1\"\r\n },\r\n \"resolutions\": {\r\n \"jquery\": \"&gt;= 1.7.2\"\r\n }\r\n}<\/pre>\n<p>Once save the bower.json file, VS 2015 automatically load all libraries listed, which is stored in \/wwwroot\/bin folder under your app project folder.<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower6.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-938\" src=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower6.jpg\" alt=\"bower6\" width=\"337\" height=\"537\" srcset=\"https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower6.jpg 337w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower6-188x300.jpg 188w\" sizes=\"auto, (max-width: 337px) 100vw, 337px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">BTW, it contains source and bunch of other stuffs, we don&#8217;t really need.<\/p>\n<p style=\"text-align: justify;\">Some just include all these files in the project and directly link to this folder. However, I would rather keep these folder separate from VS project and copy only jquery.min.js into Scripts folder so that we can keep the default MVC folder structure and also keep project folder as clean as possible. By doing this, we don&#8217;t need to commit any files under wwwroot\/bin folder.<\/p>\n<p style=\"text-align: justify;\">We will use Gulp here to automate the process to copy jquery.min.js from wwwroot\/bin\/jquery\/dis\/ folder to Scripts folder.<\/p>\n<p style=\"text-align: justify;\"><strong>5. Configure Gulp<\/strong><\/p>\n<p style=\"text-align: justify;\">Add Gulp configuration file into root directory of VS project.<\/p>\n<p style=\"text-align: justify;\"><a href=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-936\" src=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower4.jpg\" alt=\"bower4\" width=\"943\" height=\"141\" srcset=\"https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower4.jpg 943w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower4-300x45.jpg 300w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower4-768x115.jpg 768w\" sizes=\"auto, (max-width: 943px) 100vw, 943px\" \/><\/a><\/p>\n<p style=\"text-align: justify;\">We need to understand gulp API and javascript a bit, but just follow the below for now, which will copy bootstrap and jquery into Scripts folder.<\/p>\n<pre>\/\/\/ &lt;binding BeforeBuild='default' \/&gt;\r\n'use strict'\r\n\r\nvar _ = require('lodash');\r\nvar gulp = require('gulp');\r\n\r\ngulp.task('default', function () {\r\n var js = {\r\n js: [\r\n '.\/wwwroot\/lib\/bootstrap\/dist\/js\/bootstrap.min.js',\r\n '.\/wwwroot\/lib\/jquery\/dist\/jquery.min.js',\r\n '.\/wwwroot\/lib\/jquery-validate\/dist\/jquery.validate.min.js',\r\n ]\r\n };\r\n _(js).forEach(function (js, type) {\r\n gulp.src(js).pipe(gulp.dest('.\/Scripts'));\r\n });\r\n});<\/pre>\n<p>Open [Task Runner Explore] from [View &#8211; Other Windows], we will be able to see &#8216;default&#8217; task. If not, click the reload icon left top corner of Task Runner Explore window.<\/p>\n<p><a href=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower01.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-930\" src=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower01.png\" alt=\"bower01\" width=\"396\" height=\"226\" srcset=\"https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower01.png 396w, https:\/\/moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower01-300x171.png 300w\" sizes=\"auto, (max-width: 396px) 100vw, 396px\" \/><\/a><\/p>\n<p>We can run it and see jquery.min.js and others libraries are copied over to Scripts folder.<\/p>\n<p><a href=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower10.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-932\" src=\"http:\/\/www.moneystock.net\/wp_e\/wp-content\/uploads\/2016\/07\/bower10.jpg\" alt=\"bower10\" width=\"240\" height=\"186\" \/><\/a><\/p>\n<p>We can bind &#8216;default&#8217; task to &#8216;Before Build&#8217;, which will automatically run the task when we build the APS.Net MVC project.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Background While looking for the JS libary to populate folder as beautiful tree structure and hopefully having modern UI, I\u00a0found a useful angularjs based open source called, angular file manager(git), which has beautiful and easy-to-use UI, worth to spend some time for my work. The manual of the app said &#8216;use\u00a0bower install &#8211;save angular-filemanager&#8217; to&hellip; <a class=\"more-link\" href=\"https:\/\/moneystock.net\/wp_e\/2016\/07\/19\/how-to-use-bower-to-manage-front-end-library-in-visual-studio-2015-for-beginners\/\">Continue reading <span class=\"screen-reader-text\">How to use bower to manage front-end library in Visual Studio 2015 : for beginners<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[122,203,79],"tags":[96,223,224,99,225,222,227,226],"class_list":["post-925","post","type-post","status-publish","format-standard","hentry","category-asp-net-web-development","category-library","category-web-development","tag-asp-net","tag-bower","tag-gulp","tag-mvc","tag-node-js","tag-npm","tag-visual-studio-2015","tag-vs-2015","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/comments?post=925"}],"version-history":[{"count":13,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/925\/revisions"}],"predecessor-version":[{"id":949,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/posts\/925\/revisions\/949"}],"wp:attachment":[{"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/media?parent=925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/categories?post=925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moneystock.net\/wp_e\/wp-json\/wp\/v2\/tags?post=925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}