gulp-minify-cshtml

Minify ASP.NET Razor CSHTML

Usage no npm install needed!

<script type="module">
  import gulpMinifyCshtml from 'https://cdn.skypack.dev/gulp-minify-cshtml';
</script>

README

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.