How can I still run a custom IBundleTransform when EnableOptimization is off in ASP.NET?

徘徊边缘 提交于 2019-12-23 19:07:48

问题


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. 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!)

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?

来源:https://stackoverflow.com/questions/28632692/how-can-i-still-run-a-custom-ibundletransform-when-enableoptimization-is-off-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!