Click here to Skip to main content
15,892,298 members
Articles / Web Development / ASP.NET
Tip/Trick

Gulp Configuration in ASP.NET MVC Core 1.0 Application

Rate me:
Please Sign up or sign in to vote.
4.29/5 (5 votes)
19 Aug 2016CPOL1 min read 11.1K   32   9  
Step by step guide to configure gulp in ASP.NET MVC core 1.0

Introduction

This tip is a continuation of my previous article http://www.codeproject.com/Articles/1118616/Create-a-simple-mvc-application-using-Asp-Net-Core.

Background

The scope of this tip is to demonstrate the step by step procedure to configure Gulp in ASP.NET MVC Core 1.0 application.

Setup NPM (Node Package Manager)

Add npm file into the project (package.json). This file is used to store the client side dependencies of nuget packages.

NPM Settings

Structure of package.json file is as follows:

C++
{
 "version": "1.0.0",
 "name": "coreapp",
 "private": true,
  "devDependencies": {
    "gulp": "3.9.1",
    "gulp-concat": "2.6.0",
    "gulp-cssmin": "0.1.7",
    "gulp-uglify": "2.0.0"
  },
  "dependencies": {
    "bootstrap": "3.3.7"
  }
}

Add the following nuget packages of gulp:

  • "gulp": "3.9.1", : Used for gulp tasks
  • "gulp-concat": "2.6.0" : Used for file concatenation
  • "gulp-cssmin": "0.1.7" : Use for CSS minification
  • "gulp-uglify": "2.0.0" : Use for JavaScript minification

Note: In ASP.NET Core 1.0, there is no concept of bundling and minification.

Setup Gulp

After adding gulp nuget packages, you are able to find gulp configuration file in VS2015. Add the gulp configuration file (gulpfile.js) into the project and don't rename it.

Gulp

Create Tasks in Gulp File

JavaScript
/// <binding AfterBuild='min' />
/*
This file in the main entry point for defining Gulp tasks and using Gulp plugins.
Click here to learn more. http://go.microsoft.com/fwlink/?LinkId=518007
*/

var gulp = require('gulp'),
    concat = require('gulp-concat'),
    cssmin = require('gulp-cssmin'),
    uglify = require('gulp-uglify');

var paths = {
    webroot: "./wwwroot/"
};

paths.bootstrapCss = "./node_modules/bootstrap/dist/css/bootstrap.css";

paths.bootstrapJs = "./node_modules/bootstrap/dist/js/bootstrap.js";

paths.jsDest = paths.webroot + "js";

paths.cssDest = paths.webroot + "css";

gulp.task('min:js', function () {
    return gulp.src([paths.bootstrapJs])
                .pipe(concat(paths.jsDest + "/min/site.min.js"))
                .pipe(uglify())
                .pipe(gulp.dest("."));
});

gulp.task('copy:js', function () {
    return gulp.src([paths.bootstrapJs])
                .pipe(gulp.dest(paths.jsDest));
});

gulp.task('min:css', function () {
    return gulp.src([paths.bootstrapCss])
                .pipe(concat(paths.cssDest + "/min/site.min.css"))
                .pipe(cssmin())
                .pipe(gulp.dest("."));
});

gulp.task('copy:css', function () {
    return gulp.src([paths.bootstrapCss])
                .pipe(gulp.dest(paths.cssDest));
});

gulp.task('min', ["min:js", "min:css"]);

gulp.task('copy', ["copy:js", "copy:css"]);

The purpose of these tasks are:

  1. Create minification file of bootrap.css
  2. Create minification file of bootrap.js
  3. Place both the minified as well as original files under wwwroot folder so it will be accessible in the application
  4. We are keeping original files for debugging purposes

How to Run gulp Task in VS2015?

Task Runner

Open task runner explorer window. Right click on copy/min tasks and run it.

Task Runner

It will copy the original and min (JavaScript and CSS) files under wwwroot folder. You can also add these tasks as a build part (before or after build).

Folder structure after gulp tasks is as shown below:

FS

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --