Minify ASP.NET Razor Views (.cshtml files).
JavaScript
Latest commit bc89a6b Sep 18, 2015 @RehanSaeed Create .npmignore
Permalink
Failed to load latest commit information.
.gitignore Initial commit Sep 14, 2015
.npmignore Create .npmignore Sep 18, 2015
LICENSE Initial commit Sep 14, 2015
README.md Update README.md Sep 16, 2015
index.js Update index.js Sep 16, 2015
package.json Update package.json Sep 14, 2015

README.md

gulp-minify-cshtml NPM version

Minify ASP.NET Razor Views (.cshtml files).

Usage

First, install gulp-minify-cshtml as a development dependency:

npm install --save-dev gulp-minify-cshtml

Then, add it to your gulpfile.js:

Simple

var minifyCshtml = require('gulp-minify-cshtml');

gulp.task('minify-cshtml', function(){
  gulp.src('./Views/**/(!(*.min.cshtml)|*.cshtml)')
    .pipe(minifyCshtml())
    .pipe(gulp.dest('./Views/**/*.min.cshtml'));
});

Advanced

var minifyCshtml = require('gulp-minify-cshtml');

gulp.task('minify-cshtml', function(){
  gulp.src('./Views/**/(!(*.min.cshtml)|*.cshtml)')
    .pipe(minifyCshtml({
      comments: true,       // Remove HTML comments <!-- -->
      razorComments: true,  // Remove Razor comments @* *@
      whitespace: true      // Remove white-space
    }))
    .pipe(gulp.dest('./Views/**/*.min.cshtml'));
});

API

gulp-replace can be called with an optional 'options' object.

minifyCshtml(options)

options

Type: Object

options.comments

Type: boolean
Default: false

Remove HTML comments <!-- -->

options.razorComments

Type: boolean
Default: true

Remove Razor comments @* *@

options.whitespace

Type: boolean
Default: true

Remove white-space between angle brackets > <

Known Issues and Limitations

This plugin is not doing proper Razor and HTML parsing (That would be a very big project). It uses simple regular expressions to do the replacements which works in all cases except those unlikely cases listed below.

If you have strings in your Razor C# code like the two lines below, they will be wrongly modified. The workaround is to split up the string.

string a = ">   <"; // Will be changed to "><". Use "> " + " <" instead.
string b = "@* *@"; // Will be changed to "". Use "@* " + " *@" instead.