Giter VIP home page Giter VIP logo

system.web.staticoptimization's Introduction

System.Web.StaticOptimization

The MSBuild tasks package contains utilities for JS, CSS and HTML minification without server-side participation in release, HTML preprocessing. It uses WebGreace config for bundles definition.

Dependencies:

  1. Microsoft Ajax for JS and CSS minification.
  2. Zeta HTML Compressor - a .NET Port of Google's HTML compresor

Quick start for ASP.NET MVC developers

Manual for developers who use cshtml, i.e. not static HTML. If you have no plans to use static HTML you can look for alternative, but IMHO the definition of the package is less convienment that [WebGrease] (http://webgrease.codeplex.com/wikipage?title=WebGrease%20configuration%20file).

  1. Install System.Web.StaticOptimization package from Nuget (if you have probs, here is the help).
  2. Add reference to System.Web.StaticOptimization.dll, System.Web.StaticOptimization.Mvc.dll (will be automated later in special NuGet package for ASP.NET MVC developers)
  3. Add following initialization code in Global.asax.cs:
        protected void Application_Start()
        {
            ...
#if DEBUG
            BundleTable.Init(System.Web.HttpContext.Current.Server.MapPath("~/bundles.config"));
#endif
        }
  1. Edit bundles.config file by describing your JS and CSS bundles.
  2. Fasten the belts and press F6 (rebuild) ;).

How it works? The package adds MSBuild post build tasks in your csproj. Simple and Easy. Have problems? Check out my demos.

How to use HTML preprocessing

For example, we have {filename}.template.html for template files and we want to get {filename}.html preprocessed:

index.template.html:

<!DOCTYPE html>
<html ng-app="homepage">
<head>
    <!--style:~/Content/css/common.css-->
</head>
<body>
    <div class="container-fluid">
        <error></error>
        <busyindicator></busyindicator>
        <div ng-view jq-show-effect="!(isLoading || isError)"></div>
    </div>
    <!--template:Views/Shared/templates.html-->
    <!--script:~/bundles/common.js-->
</body>
</html>

bundles.config:

<?xml version="1.0" encoding="utf-8"?>
<WebGrease>
  <CssFileSet name="~/Content/css/common.css">
    <Input>~/Content/css/bootstrap.css</Input>
    <!--+ over 9000 styles
    ...
    -->
  </CssFileSet>
  <JsFileSet name="~/bundles/common.js">
    <Input>~/Scripts/libs/jquery-1.9.1.js</Input>
    <!-- + 100500 scripts
    ...
    -->
  </JsFileSet>
</WebGrease>

We expect to generate index.html with substitution of JS, CSS bundles and html templates.

Let we write in csproj or your external MSBuild file:

  <!--Task import-->
  <UsingTask AssemblyFile="$(StaticOptimizationLib)" TaskName="System.Web.StaticOptimization.HtmlMinifierTask" />
  <Target Name="AfterBuild">
    <ItemGroup>
      <!--output HTML replace rules goes here. This example means that it will create
      the output file with the same name as input, but with replaced '.template.html'
      occurrence to '.html'. E.g.: index.template.html -> index.html -->
      <OutputFileNameReplaceRule Include=".template.html">
        <ReplaceBy>.html</ReplaceBy>
      </OutputFileNameReplaceRule>
    </ItemGroup>
    <PropertyGroup>
      <BundleConfig>$(MSBuildProjectDirectory)\bundles.config</BundleConfig>
    </PropertyGroup>
    <!-- Process all *template.html files with concrete bundles config and using release mode for rendering of bundles-->
    <HtmlPreprocessingTask OutputFileNameReplaceRules="@(OutputFileNameReplaceRule)" InputFile="*template.html" BundleConfig="$(BundleConfig)" IsRelease="True" />
    <!--Here you can place html minificator (template.html is ignored by default -->
    <!--<HtmlMinifierTask InputDir="$(MSBuildProjectDirectory)" />-->
  </Target>

Press F6;)

Plans

  1. Prepare demo for SPA (using HTML preprocessing engine)
  2. Automate Global.asax generation
  3. Detailed documentation for extra input params of each task

system.web.staticoptimization's People

Contributors

2xmax avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

system.web.staticoptimization's Issues

VS 2015 Update 2 Build Failed

Thanks for sharing this, I tried installing the task in my own project, I was getting build failed for your task. I then attempted to download the demos - same issue.
I also tried installing ajaxmin package to demos but no difference.

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.