Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using custom transformers #15

Closed
ashwin027 opened this issue Mar 10, 2017 · 3 comments
Closed

Using custom transformers #15

ashwin027 opened this issue Mar 10, 2017 · 3 comments

Comments

@ashwin027
Copy link

ashwin027 commented Mar 10, 2017

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
Copy link
Author

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

@Taritsyn
Copy link
Owner

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants