Modification in SASS files

Jun 3, 2015 at 2:25 PM
Hello,

I have main SASS file which contains:
  • with one variable, for example $userColor="#0000FF"
  • many @imports with files that uses my $userColor variable
Is possible using BundleTransformer to change of $userColor variable value to another variable declared when application is running? I want to get color value after every refresh of page by declare and add bundle in my view. Now I trying to do it with standard IItemTransform and Process method but not working this way.
Coordinator
Jun 3, 2015 at 6:40 PM
Hello, Dagna!
I want to get color value after every refresh of page by declare and add bundle in my view.
Сontents of bundles is always cached (this functionality is at level of the Microsoft ASP.NET Web Optimization Framework). In this case, you will have to write your own implementation of a caching system for this framework.

But it seems to me, that your problem is that you want to write a multi-tenant web application. Solution of a similar problem (only for LESS) described in the “BundleTransformer.Less inject variables depending on context/request” discussion on Stack Overflow. I.e. you also have to write your own implementation of caching system and use InjectContentItemTransform class (to declare variables need to use Sass-syntax). But unlike LESS, you do not have the ability to modify variables (available only ability to declare variables). I recommend you to examine source code of SmartStore.NET.
Jun 6, 2015 at 10:39 PM
Hi, I tried to use CustomStyleBundle with my interpretation of IItemTransform interface:
    public class ReplacePlaceholdersBundleTransform : IItemTransform
    {
        private readonly IDictionary<string, string> _replacements;

        public ReplacePlaceholdersBundleTransform()
        {
            _replacements = new Dictionary<string, string>();
        }

        public ReplacePlaceholdersBundleTransform(IDictionary<string, string> replacements)
        {
            _replacements = replacements ?? new Dictionary<string, string>();
        }

        //public void Process(BundleContext context, BundleResponse response)
        //{
        //    if (_replacements == null || _replacements.Count == 0)
        //        return;

        //    foreach (BundleFile css in response.Files)
        //    {
        //        //string cssAppRelativePath = css.FullName.Replace(context.HttpContext.Request.PhysicalApplicationPath, context.HttpContext.Request.ApplicationPath).Replace(Path.DirectorySeparatorChar, '/');
        //        //string cssDir = cssAppRelativePath.Substring(0, cssAppRelativePath.LastIndexOf('/'));
        //        //response.Content = exp.Replace(response.Content, m => TransformUrl(m, cssDir));
        //    }

        //    foreach (var replacement in _replacements)
        //    {

        //        response.Content = response.Content.Replace(replacement.Key, replacement.Value);
        //    }
        //}

            public string Process(string includedVirtualPath, string input)
            {
                if (_replacements == null || _replacements.Count == 0)
                    return input;

                foreach (var replacement in _replacements)
                {

                    input = input.Replace(replacement.Key, replacement.Value);
                }

                return input;//(input manipulated with proper path replacing bad path)
            }
    }
Without the line:
 BundleTable.EnableOptimizations = true;
the process result is ignored, but with the line I have error that any variable from @import file isn't available. What can be the reason here?
Coordinator
Jun 7, 2015 at 11:51 AM
Hello, Dagna!

I recommend you to read about CustomBundleResolver class in the “Examples of usage” section of documentation.