Sunday, May 11, 2014

Compile TypeScript On Request with BundleTransformer

I have talked before about BundleTransformer, and how you can use it to compile your LESS on a per request basis. Did you know that it supports TypeScript too?

BundleTransformer is an add on for Microsoft's System.Web.Optimization frame work bundling and minification of web resources. It allows you to convert your compiled languages on the server on request instead of need to create and maintain complied versions of resource files. Best of all BundleTransformer uses a JavaScript engine to compile LESS and TypeScript in with native compiler, ensuring that you will never lag behind the latest version.

Required NuGet Packages

The BundleTransfomer will bring in System.Web.Optimization, but then you need to specify a JavaScriptEngineSwitcher for the framework to run on, as well as a minifier to use on the final scripts when in release.

After including these packages you need only make three more updates...

Add your TypeScript files to a Bundle

protected void Application_Start()
    var bundle = new CustomScriptBundle("~/Scripts/TypeScriptBundled");

Render your Bundle

@using System.Web.Optimization
<!DOCTYPE html>
        <title>@ViewBag.Title - TypeScript Bundle Demo</title>
    <body><div class="container body-content">

Web.config Updates

<bundleTransformer xmlns="">
  <js defaultMinifier="MicrosoftAjaxJsMinifier">
        <add name="NullMinifier" 
             type="BundleTransformer.Core.Minifiers.NullMinifier, BundleTransformer.Core" />
        <add name="MicrosoftAjaxJsMinifier"
             type="BundleTransformer.MicrosoftAjax.Minifiers.MicrosoftAjaxJsMinifier, BundleTransformer.MicrosoftAjax" />
        <add name="NullTranslator" 
             type="BundleTransformer.Core.Translators.NullTranslator, BundleTransformer.Core" 
             enabled="false" />
        <add name="TypeScriptTranslator"
             type="BundleTransformer.TypeScript.Translators.TypeScriptTranslator, BundleTransformer.TypeScript" 
             enabled="true" />
   <jsEngine name="MsieJsEngine" /> 
<jsEngineSwitcher xmlns="">
      <add name="MsieJsEngine" 
           type="JavaScriptEngineSwitcher.Msie.MsieJsEngine, JavaScriptEngineSwitcher.Msie" />
    <add name="TypeScriptAssetHandler" 
         type="BundleTransformer.TypeScript.HttpHandlers.TypeScriptAssetHandler, BundleTransformer.TypeScript" 
         preCondition="" />


No comments:

Post a Comment

Real Time Web Analytics