This project has moved. For the latest updates, please go here.

Using custom transforms

Mar 10 at 12:44 AM
Firstly, this library has been really useful so thanks for all the work. I have an issue that Im trying to work through. Here is the scenario,

Im trying to dynamically add sass variables at runtime to the response content in a custom transformer, like so,
Bundle testCss = new Bundle("~/test/css").Include("~/test/test.scss");
testCss.Transforms.Add(new testTransformer());
testCss.Transforms.Add(new StyleTransformer());
bundles.Add(testCss);
In testTransformer, Im trying to add to response.content with some variables that override other variables.
public class testTransformer : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        //Variables to be pulled from a data store. Hard coding for now.
        response.Content = string.Format("{0}{1}", @"$primary: blue !default", response.Content);
    }
}
When I ran this, the transforms run correctly and the content is updated, but the bundletransformer library reads the file instead of the response content and completely ignores all other transforms.

It would be nice if it can read the response content instead of the file because the response content already has the contents from all the files in the bundle. MVC already gives us this. You could avoid an unnecessary file read and make the solution more extendible.

Is it possible to maybe add a configurable option to use content instead of reading the file again?
Coordinator
Mar 10 at 10:28 AM
Edited Mar 10 at 12:30 PM
Hello, Ashwin!

I recommend you to use the custom item transformation (implements System.Web.Optimization.IItemTransform interface) for these purposes. Details can read in the “BundleTransformer.Less inject variables depending on context/request” discussion (second way) on Stack Overflow.
Mar 10 at 3:15 PM
Looks like this might solve it for me. Thanks Taritsyn!
Mar 10 at 3:51 PM
Edited Mar 10 at 3:52 PM
@Taritsyn I tried the solution and it works fine, except that it doesnt work in debug mode. You mentioned in the stack overflow post that you were working on this. Is it part of your latest release?
Coordinator
Mar 11 at 7:08 AM
I tried the solution and it works fine, except that it doesnt work in debug mode.
On Stack Overflow in comments described solution for this problem:
Hernan! In Bundle Transformer 1.9.0 Beta 1 implemented this functionality - bundletransformer.codeplex.com/releases/view/123398. In order to the debugging HTTP-handlers can use a transformations from bundles need to add in the RegisterBundles method of App_Start/BundleConfig.cs file the following code: BundleResolver.Current = new CustomBundleResolver();
Also I recommend you to read the “Running Autoprefixer with BundleTransformer / LESS in Debug mode” discussion.
Mar 13 at 5:00 PM
Edited Mar 13 at 5:00 PM
I got it to work. Thank you very much for all your help Taritsyn!...and keep up the awesome work with the bundle transformer library!!