Giter VIP home page Giter VIP logo

razorhtmlminifier.mvc5's Introduction

RazorHtmlMinifier.Mvc5 logo RazorHtmlMinifier.Mvc5

Build status NuGet version NuGet downloads

Trivial compile-time Razor HTML Minifier for ASP.NET MVC 5.

Installation

Download

Binaries of the last build can be downloaded on the AppVeyor CI page of the project.

The library is also published on NuGet.org, install using:

PM> Install-Package RazorHtmlMinifier.Mvc5

RazorHtmlMinifier.Mvc5 is built for .NET v4.8 with a dependency on ASP.NET MVC 5.3.0 and System.Web.

Configuration

Find the Web.config with your Razor configuration (by default it's in Views/Web.config). You should see something like this inside:

<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.3.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

In order to start minifying views and partial views, replace it with (after the NuGet package is installed):

<host factoryType="RazorHtmlMinifier.Mvc5.MinifyingMvcWebRazorHostFactory, RazorHtmlMinifier.Mvc5, Version=2.0.0.0, Culture=neutral, PublicKeyToken=a517a17e203fcde4" />

Then rebuild your solution, which should also restart the app.

If you're using Razor @helper functions placed inside the App_Code folder, you have to do additional configuration in order to minify those. In the root Web.config, you should see something like this:

<compilation debug="true" targetFramework="4.8" />

Your <compilation> element might have different attributes. Leave the current attributes as-is, and add <buildProviders> like so:

<compilation debug="true" targetFramework="4.8">
    <buildProviders>
        <add extension=".cshtml" type="RazorHtmlMinifier.Mvc5.MinifyingRazorBuildProvider, RazorHtmlMinifier.Mvc5" />
    </buildProviders>
</compilation>

How it works

The minifier processes the code generated during Razor compilation. Because it runs in compile-time, it shouldn't add any overhead during runtime.

The entire source code is just two files, feel free to view them.

The minification algorithm is fairly trivial. It basically:

  • replaces any amount of whitespace characters with a single space;
  • replaces any line breaks with just the line feed character (\n);
  • if there's a sequence of whitespace characters and line breaks, it only takes the first in the sequence.

The minification process is deliberately trivial so that it would be easy to understand and expect.

CAUTION! The minification is not context-sensitive, and it doesn't have any special handling of <pre> tags or similar. If you use <pre> tags or have any other significant white-space in your HTML, you shouldn't use this library.

The code is inspired by Meleze.Web (an older project), but it's much simplified and updated to be used for with the latest version of ASP.NET MVC.

Resolving IntelliSense issues

When you change the Razor factory, you might experience IntelliSense issues in Visual Studio.

I've investigated this and it looks like VS actually needs to have the assembly in GAC for IntelliSense to work. Luckily, now when the latest version of the library is strong-named, it's possible to add it to GAC. Adding the assembly to GAC shouldn't have any side-effects, but it will need to be added on every developer machine that wants to use see the IntelliSense in Razor files completely (without the squiggly undelines), which can be a hassle.

If you want to add the assembly to GAC, you'll need to do the following:

  • Open Developer Command Prompt for VS (you'll find it in Start menu) as an Administrator.
  • Navigate to the folder of the NuGet package: cd "C:\PATH_TO_YOUR_SOLUTION\packages\RazorHtmlMinifier.Mvc5.2.0.0\lib\net45"
  • Install it to GAC: gacutil /i RazorHtmlMinifier.Mvc5.dll (it should respond Assembly successfully added to the cache)
  • Restart Visual Studio (and maybe also clear any ReSharper caches if you're using that)

Then it should start working.

There are also other alternative solutions:

  • Set up the host factory in Web.config.Release instead of Web.config. That will make the minification run only when the Release configs are applied, and while debugging, you can keep the original host factory, which doesn't have problems with intellisense.
  • Set the host factory in the Web.config file in runtime, e.g. in Global.asax. You'd have to use the WebConfigurationManager API to modify the Web.config during runtime.

However, I wouldn't recommend either of those (I also haven't tried them), as they feel more like a hack, and might cause more issues if you'd be doing stuff like precompilation of the views. Adding the assembly to GAC is probably the easiest, it's still annoying that VS requires that though...

razorhtmlminifier.mvc5's People

Contributors

dependabot[bot] avatar rudeysh avatar tompazourek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

razorhtmlminifier.mvc5's Issues

RazorHtmlMinifier not minify my project views

we install RazorHtmlMinifier using Install-Package RazorHtmlMinifier.Mvc5
then replace Views/Web.config with
then rebuild application but result not minify please help?
we use VS2015 , MVC5

Strip out HTML comments

Hey @tompazourek, thank you for this thing, absolutely love the idea of compile-time transformation.

Though, do you think it's possible to add an ability to remove <!-- HTML comments --> from the outcome as well? I can work on it if you don't want or don't have time - would appreciate if you then accept my pull request and redeploy the lib in Nuget.

Also it would be great if this thing was somehow configurable so that a user can choose whether they want to strip out comments or not, and maybe they don't want newlines at all so they can be completely removed (or replaced with a single space so that nothing breaks). Do you know if any configuration may be read from web.config at the compile time?

Thanks!
Andrew K.

Is it possible to support Razor @helpers in the /App_Code folder?

This minifier seems promising. I was actually looking for something a bit more aggressive, but I understand that's not the goal of this project.

However, I noticed all Razor @helper functions from my /App_Code folder are not being minified. Is there any way to fix that?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.