To those that are already used to using the .Net platform, version 4.0 and 4.5, it knows that to decrease files as js, css and others, you must use Bundle. Bundle is something fantastic and widely utilized, but I see a disadvantage, it is only used by those who develop in .Net. Its improvement and extension is limited by the ASP.NET community. Somebody can criticize my argument, I don't think it's a big argument, but why can't we use tools that other communities are using and improving all the time and thus add to our community? Microsoft comes betting on this with your new stack of development. Microsoft, from Visual Studio 2015 to the new ASP.NET, decided to give us the liberty to use Node, Bower (I did an article about both) and Gulp, something that was not developed by Microsoft, but by the Node.js community. Today's subject is about Gulp, what is and how use it in our projects. With Gulp, we going to learn to create tasks and integrate in the build of Visual Studio. Let’s go!
Sometimes I have the thought that development is easy, focusing only on the code and hearing music is something nice. However, development is not just that. It is taking care of others parts that oftentimes are boring and painful. This moment, I think again and change my mind. Have you ever had this thought? For instance, you create an application of the ground, the start is beautiful and it is following your manifest. Nevertheless, when it arrives close to the end, you notice that it still lacks many things, I am not talking about application functions, but of small things needed and done routinely. A lot of these tasks are in relationship to deployment, such as decreasing files to improve site performance. That is just an example, your team may have more tasks.
In the development of a site, many tasks are repeated and as explained above, Gulp can automate these tasks. In your site, gulpjs.com, it describes itself like “automatize and improve your workflow”. Node.js community has created Gulp. Gulp alone almost does not do too much, because it needs plug-ins to augment the task desired. For instance, do you want to decrease a file? It has a plug-in. Do you want to validate your js? It also has another plug-in. Each plug-in has a specific responsibility. The philosophy of Gulp is simple, set up the tasks through your own code . The configuration of Gulp in ASP.NET Core projects is zero, because it already comes installed. In projects legacies, you must do some steps that I am going to show below.
The first step is installing the Node.js, if you have doubts about the installation, read another article about the subject that I did before. Remember, Microsoft is investing too in Node.js, if you do not know, it is good to know from now on.
After installing Node.js, automatically it is installed the NPM. The objective of this article is not explains about Node.js, but I will do a summary: Node.js is a development platform and NPM is your package manager. With the NPM installed, go to a command-line of your computer and type in root of your web project: “NPM init”. By typing this command, it will do some tasks to be inserted in file created. Was Gulp installed? Of course not, this command serves only to create a configuration file in our project. Through this file, all node packages that I am going to install with the help of NPM will be listed. Validate in root of project if the package.json file was created with success.
After, return to command-line and type: “NPM install gulp –save-dev”. This command will install Gulp in your project and insert your dependency in the file that was created above. Now we have Gulp installed, but is not configured. For this, create a file in the root of your web project called “gulpfile.js”. This is the file responsible for receiving the tasks. Before writing some tasks, I am going to explain some structures of Gulp to improve your understanding. These are all methods used by Gulp: Task, Src, Watch, Dest, Parallel and Pipe.
Task: Method of input, create a task and your parameters are a name and a function to execute the task.
Src: Through this method is selected a file or files that must be modified after of the task.
Watch: As your own name says, it has the goal of watching a file or files that when changed is executed automatically.
Dest: This method informs the destiny where the modified file will be stored.
Pipe: Maybe the most important method, with it is executed all processes needed without expecting the end of each process. Also we can do a chain of process in the order needed.
Parallel and Series: Used in case where must execute processes in parallel.
Nice, you have an idea about Gulp and its methods, now we can go ahead. In file created, called “gulpfile.js”, add the following code and return to command-line and type: “Gulp default”.
- var gulp = require('gulp' );
- gulp.task('default', function () {
- console.log('hello world');
- });
If you note the response in command-line, you will see that was printed the messag was informed in the task. Realize also, to create a task we used the Task method. However, before creating a task, it was necessary to load the Gulp package, for this, it is necessary to use the Require method. According to what I said above, Gulp alone does not do too much and to improve the tasks level is needed to use others packages too. Before seeing more packages, I am going to create an application in Visual Studio 2015 and improve this project utilizing Gulp. The goal is to improve the performance.
I am going to begin the jobs with the css files, note in code below that have two styles in page layout. My idea is concatenate the files to be only one, decrease the size, pulling out spaces not used and finally, generating the styles that are not being used in the application, because when bootstrap is used, neither all styles of css are used. This tip is good to improve your site
- <link href="/Content/bootstrap.css" rel ="stylesheet" />
- <link href ="/Content/site.css" rel ="stylesheet" />
- var gulp = require('gulp' );
- var concat = require('gulp-concat' );
- var uncss = require('gulp-uncss' );
- var cssmin = require("gulp-cssmin" );
- gulp.task('css-concat', function () {
- return gulp.src([ 'content/*.css', '!content/*.min.css' ])
- .pipe(concat('site.min.css'))
- .pipe(gulp.dest('content/'));
- });
- gulp.task('css', ['css-concat'], function () {
- return gulp.src( 'content/site.min.css')
- .pipe(uncss({
- html: [ 'http://localhost:64161/']
- }))
- .pipe(cssmin())
- .pipe(gulp.dest( 'content/'));
- });


Bhuvanesh MohankumarPosted May 3, 2016, 12:53 PM
Good one..
Rajeev PunhaniPosted Apr 21, 2016, 5:00 AM
Nice Share.
Sr KarthigaPosted Apr 20, 2016, 11:06 PM
nice explanation
Vignesh ManiPosted Apr 20, 2016, 3:54 PM
Nice