我怎么能仍然运行一个自定义IBundleTransform时EnableOptimization是关闭的ASP.NET? [英] How can I still run a custom IBundleTransform when EnableOptimization is off in ASP.NET?

查看:327
本文介绍了我怎么能仍然运行一个自定义IBundleTransform时EnableOptimization是关闭的ASP.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先背景 - 我使用的把手为模板HTML,我想利用ASP​​.NET捆绑的捆绑和缓存功能,模板提供给客户。为此,我创建了自己的捆绑子类的实现 IBundleTransform ,做我的模板,一些必要的转换。特别是我:

First the background - I'm using Handlebars for templating HTML and I want to make use of the bundling and caching capabilities of ASP.NET Bundling to deliver the templates to the client. To that end I've created my own Bundle subclass and an implementation of IBundleTransform that does some necessary conversion of my templates. In particular I:


  1. 添加模板到Visual Studio的HTML,但嵌入在&LT模板,脚本类型=文本/ X-车把模板'> ...< / SCRIPT> 这似乎给了我很大的模板/ HTML的智能感知

  2. 在我实施 IBundleTransform 我包的最后模板的东西沿着 Injector.register({0}功能的线路( ){{返回window.atob({1});}}); ,其中 {0} 被替换的路径模板,用于获取客户端code模板和 {1} 被替换为一个base64恩的HTML codeD形式(直到我自己看着办出编码的HTML字符串更好的办法!)

  1. add the templates to visual studio as HTML but embed the template in a <script type='text/x-handlebars-template'>...</script> which seems to give me great template/html intellisense
  2. in my implementation of IBundleTransform I wrap the final template in something along the lines of Injector.register('{0}', function() {{ return window.atob('{1}'); }}); where {0} is replaced with the path of the template and is used to fetch the template in client code and {1} is replaced with a base64 encoded form of the HTML (until I can figure out a better way of encoding the html string!)

我的问题就来了,如果我设置 BundleTable.EnableOptimizations = FALSE 在这上面的2号点发生没有,我提供的原始文件。我暂时使出以下code停靠的所有文件的缩小,但仍然将它们转换:

My problem comes if I set BundleTable.EnableOptimizations = false at which point none of number 2 above occurs and I'm delivered the raw file. I've temporarily resorted to the following code which stops minification of all files but still transforms them :

BundleTable.EnableOptimizations = true;    
if (System.Diagnostics.Debugger.IsAttached)
{
    foreach (var bundle in bundles)
    {
        if (bundle.Path != "~/bundles/handlebars-templates")
                  bundle.Transforms.Clear();
    }
}

有一个更好的选择,让我把优化掉,把我的所有模板单独的浏览器,但仍然有他们去之前他们转化?

Is there a better option that allows me to turn optimizations off, deliver all my templates individually to the browser but still have them transformed before they go?

推荐答案

您可以使用 BundleTransformer.Handlebars 库,便于通过的NuGet安装。它会改变你的车把文件(所以没有更多的增加你的HTML模板内!的),无论是优化或没有。你还需要安装一个Javascript引擎,在文件中则建议使用以下的人之一:

You can use the BundleTransformer.Handlebars library, easy to install through NuGet. It will transform your handlebars files (so no more adding templates inside your html !) no matter if optimizations are on or not. You also need to install a Javascript engine, in the documentation it's recommended to use one of the following ones:


  • JavaScriptEngineSwitcher.Msie

  • JavaScriptEngineSwitcher.V8

  • JavaScriptEngineSwitcher.ChakraCore

这三种方法都容易通过的NuGet安装,需要0配置。然后,你需要做的唯一的事情就是改变你的的Web.config 文件,以便使用JavaScript引擎正确的,这里是与MSIE引擎的例子:

All three are easy to install through NuGet and require 0 configuration. Then the only thing you need to do is change your Web.config file so the correct javascript engine is used, here's an example with the msie engine:

<jsEngineSwitcher xmlns="http://tempuri.org/JavaScriptEngineSwitcher.Configuration.xsd">    
   <msie engineMode="Auto" />
      <core>
        <engines>
            <add name="MsieJsEngine" type="JavaScriptEngineSwitcher.Msie.MsieJsEngine, JavaScriptEngineSwitcher.Msie" />
        </engines>
      </core>
</jsEngineSwitcher>

...

<handlebars namespace="Handlebars.templates" rootPath="" knownHelpers="" knownHelpersOnly="false" data="true">
  <jsEngine name="MsieJsEngine" />
</handlebars>

BundleConfig 类将是这个样子:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new CustomScriptBundle("~/bundles/myBundle")
           .IncludeDirectory("~/Content/Templates", "*.handlebars", true)
           .Include("~/Scripts/MyScript.js"));
}

注意 CustomScriptBundle 而不是 ScriptBundle 这是一类的 BundleTransformer 库。使用这个类可以确保在scriptbundle默认的转换尚未完成(这会导致handlebarstransformer停止工作)时( EnableOptimalization = TRUE 污染减量)。它可以使用minifier也一样,如果你将其添加为变换来捆绑,所以你仍然可以最小化您的文件。在我的例子,我在内容/模板文件夹中的所有文件扩展名为 .handlebars 将使用JavaScript引擎获得转化成javascript函数,这将会同捆发送。您可以直接访问模板,无需进一步汇编code这样的(该文件将被称为 TemplateName.handlebars 在我的项目有两个把手变量 {{}的firstName} {{}的lastName}

Notice the CustomScriptBundle instead of ScriptBundle which is a class of the BundleTransformer library. Using this class makes sure that the default transformations on a scriptbundle (minifying when EnableOptimalization = true) isn't done (this causes the handlebarstransformer to stop working). It is possible to use a minifier too, if you add it as a Transform to the bundle, so you can still minimize your files. In my example, every file in my Content/Templates folder with the extension .handlebars will get transformed using the javascript engine into a javascript function, which gets sent with the bundle. You can directly access the templates without requiring further compilation in code like this (the file would be called TemplateName.handlebars in my project with two Handlebars variables {{firstName}} and {{lastName}}):

var html = Handlebars.templates["TemplateName"]({ 
   firstName: 'Alexander',
   lastName: 'Derck',
});

默认情况下,库将只适用的转换,以 .handlebars .hbs 文件,但你可以改变它在web.config中。 上BundleTransformer库在这里更多的文档。

By default the library will only apply the transformations to .handlebars and .hbs files but you can change it in the web.config. More documentation on BundleTransformer library here.

这篇关于我怎么能仍然运行一个自定义IBundleTransform时EnableOptimization是关闭的ASP.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆