Taritsyn / LibSassHost

.NET wrapper around the LibSass library with the ability to support a virtual file system.
Apache License 2.0
82 stars 11 forks source link

Using custom transformers #15

Closed ashwin027 closed 7 years ago

ashwin027 commented 7 years ago

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,

Packages installed: LibSassHost 1.0.0 LibSassHost.Native.win-x86 1.0.0

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?

ashwin027 commented 7 years ago

Sorry, I should probably add this to the bundle transformer account instead of this one. Im closing this out. My apologies.

Taritsyn commented 7 years ago

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.

Taritsyn commented 7 years ago

On CodePlex not working notifications.

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.