6

I'm presently working on a project that uses the ASP.NET Web Optimization library (v 1.1.0-Beta1) in conjunction with the Bundle Transformer extension (v 1.7.3-Beta1 for core, 1.7.0-Beta1 for LESS) which is used to convert LESS into CSS. Based on web searches paths within CSS (and less) appear to be a common issue, in most cases it is recommended to manually modify the CSS and be done with it. However, due to differences between our development and production environment, and not owning the affected CSS such a solution is not feasible.

Two solutions seem to exist. The first is to overlay the virtual directory as defined by the bundling over the actual directory that contains the content. To me this seems like a poor option.

Secondly, and the route I've chosen, is to use a IItemTransform such as CssRewriteUrlTransform (mentioned in this post. Even this solution has it's limitations. As such I've attempted to write my own ItemTransformer but it seems that the results of it's execution is ignored when used in the following manner:

public static void RegisterBundles(BundleCollection bundles)
{
    /* among other work pass in IItemTransformer to fix paths */
    var styleBundle = new StyleBundle("~/bundles/css/styles")
        .Include(...)
        .Include("~/Content/less/font-awesome.less", new RewriteUrlTransform())
        .Include(...);

    styleBundle.Transforms.Add(new CssTransformer());
    styleBundle.Orderer = new NullOrderer();

    bundles.Add(styleBundle);
}

Implementation of IItemTransform:

public class RewriteUrlTransform : IItemTransform
{
    public string Process(string includedVirtualPath, string input)
    {
        return (input manipulated with proper path replacing bad path)
    }
}

Unless I'm entirely misunderstanding how an IItemTransform is to be used, which is quite possible due to lack of documentation, I would think that the return of the Process method is the new post processed CSS. However, the return seems to be ignored. The original input is always in use, even when I return a String.Empty(). Am I doing something wrong here or is it indeed a bug?

Community
  • 1
  • 1
rheone
  • 1,399
  • 1
  • 16
  • 28
  • Also of note I've looked into [Casset](http://getcassette.net/) and determined it not to be a good solution for my needs as it does not lend itself to creating multiple bundles and explicitly requesting them at different times of during page render. – rheone Apr 29 '13 at 16:37
  • You had suffered from lack of information, but I hit this question because of its title. The point is that there ARE some problems with logic in original `StyleBundle` when it deals with relative paths in virtual directories (and few other things) and [ProperStyleBundle](http://stackoverflow.com/a/27890912/551322) is what I came up with. – nrodic Jan 25 '15 at 23:50

2 Answers2

3

No you are understanding this correctly, the item transforms are applied to an item before they are bundled together, and then the bundle transforms are run. Have you verified that it is calling your transform when you expect in the debugger?

Hao Kung
  • 27,364
  • 6
  • 81
  • 93
  • Yes, the transformer is called as verified by a break point in the debugger. – rheone Apr 30 '13 at 16:15
  • 1
    My current guess is that full bundling doesn't happen during debug/dev/local and the results are ignored, but can't verify due to the source not yet being available. – rheone Apr 30 '13 at 16:24
  • 3
    Ah yes, you should specify BundleTable.EnableOptimizations = true – Hao Kung Apr 30 '13 at 18:14
  • OK, so my assumption is correct that code is executed by results are unused when optimizations are off. That’s a little surprising. Maybe I’m trying to use the optimization library for things in which it was not intended but it would seem that there may be a case in production where I would need to turn off optimization/bundling but still have the transformations done. For example path correction, is there (or is there planed) to be any sort of mixed mode feature were transformations can be executed but explicit bundling is not run? – rheone Apr 30 '13 at 18:50
  • Well, so debug/release affects the Scripts/Styles helpers, when debug = true, by default it renders out links to the raw files (no bundling). We do have plans to support mixed mode eventually at a more granular level. – Hao Kung Apr 30 '13 at 19:34
3

Bundle Transformer natively supports a automatic transformation of relative paths to absolute in CSS-code. I'm telling you as the developer of this product.

Andrey Taritsyn
  • 1,286
  • 11
  • 26
  • Yes, in most cases this is true. However, I'm having explicit problems with the path being incorrect due to LESS resources being in a different path than the standard bundled CSS. (css from less wants to find resources in other paths) and with the usage of sub domains. Hence my manual transformation of the path. – rheone May 02 '13 at 19:50
  • In [Bundle Transformer 1.7.7 Beta 1](http://bundletransformer.codeplex.com/releases/view/106114) added support of the item transformations. To normal work with the item transformations you will need to use the `NullBuilder` class as builder by default, or use the `CustomStyleBundle` and `CustomScriptBundle` classes. – Andrey Taritsyn May 03 '13 at 20:09
  • In case anyone else is wondering how to use the built-in support for automatic transformation of relative paths, see this article: http://www.seankenny.me/blog/2013/09/04/404-with-relative-paths-and-the-web-optimization-framework/ – Adrian Grigore Jul 25 '16 at 19:44