npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-cshtml-minify

v2.0.0

Published

Minify .cshtml files with gulp

Downloads

82

Readme

Why minify?

Your site can experience a 27% higher conversion rate by improving page speed by 1 second. Or read about these stories; and consider that Google ranks your page higher on search engine results the faster it loads. No matter what your website, you want people to use it. The slower it loads, the less patient people are going to be waiting around for the content.

Minification is just one of the ways you can make your website more accessible to others, besides other options like adding a load balancer to help distribute network traffic or upgrading to the latest and greatest version of your framework (which you SHOULD do if you are using .NET Core). Thankfully, minification is easy and this library aims to give you the biggest possible performance improvement for your .cshtml/html files.

Why not use another minification library that's recommended by Google? That library only works on HTML files, but rest assured, we are taking heavy inspiration from this library in order to make use of the same minification techniques for .cshtml files. In case our library's approach of using gulp to minify your .cshtml does not work for you, I would recommend an .exe in order to minify your .cshtml and steer you away from options like this if you can help it. (Minifying at runtime incurs a performance penalty).

How it works

This minification library works by taking the biggest offenders to page weight and nullifies them. We reduce the number of bytes your .cshtml/html is made of, which makes it quicker to send the page to the end user. What do we do exactly? The list isn't comprehensive (and more features are being added), but here is what we have right now:

  • [X] Removes leading/trailing whitespace
  • [X] Removes HTML/Razor/CSS comments
  • [X] Minifies inline <script> blocks with uglify-js
  • [X] Minifies inline <style> blocks with clean-css
  • [X] Remove optional closing tags
  • [X] Collapse whitespace within tag attributes
  • [X] Remove quotes around eligible tag attribute values
  • [X] Remove optional closing slashes on void elements
  • [X] Remove url schemes

How to use

  1. npm install gulp-cshtml-minify
var minifyCshtml = require('gulp-cshtml-minify'),
    gulp = require('gulp');

gulp.src("test.cshtml")
    .pipe(minifyCshtml())
    .pipe(gulp.dest("result"));

Options

You can pass in a number of options into the function to customize what options are turned on and off. The values shown below are the default options and will be set as such if you do not pass in any custom options when running the library (as seen in the above example).

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

gulp.src("test.cshtml")
    .pipe(minifyCshtml({
        removeHtmlComments: true,
        removeRazorComments: true,
        removeCssComments: true,
        minifyCss: true,
        minifyJs: true,
        collapseWhitespace: false, /* collapses whitespace to one space */
        optionalClosingTags: true, /* removes optional tags */
        urlSchemes: true, /* https:// -> // */
        uglifyjsOptions: {}, /* options for uglifyjs */
        cleancssOptions: {} /* options for cleancss */
    }))
    .pipe(gulp.dest("result"));

Note - Collapsing whitespace can cause issues if you are relying on spacing to display inline elements.

Note - Removing optional closing tags in seldom circumstances may lead to unexpected behavior.

Note - Shortening the url schemes may cause problems if you load resources over https when your page is loaded over http, which should no longer apply to the majority (but may apply to you).

Benchmarks

I have taken random pages from different websites to show you the performance of this library. This will be built into a nice command you can run on your own in the package at a later date but for now will remain solely as text. Please note - that these numbers are not taking into account GZIP, which on its own can reduce text content by 60-88%. But don't just gzip and not minify, read here, here or here why you should be doing both.

| Url | Original | Minified | Savings | | ------------- |-------------| ----- | ----- | | MDN | 112.9KB | 97KB | 15% | Vox | 157.5KB | 138KB | 12.4% | Food Network | 151.8KB | 126.2KB | 17% | Olive Garden | 620.1KB | 536.9KB | 14%