Replacements?

Jan 17, 2011 at 4:16 PM

Hi all.  At my former employer, we had a custom minifier that allowed us to use placeholder variables such as web.config  appsettings varibles that would get replaced at the time of minification.  For example, in our css files, we would write: 

body { background: url([[Config.CdnUrl]]/background.jpg) }

...where [[Config.CdnUrl]] would get replaced w/ the equivalent of ConfigurationManager.AppSettings["CdnUrl"] from the server. 

My current employer is using the YUI compressor and as of now we aren't able to accomplish the above code.  Is it possible to customize the minification process to add a step like this and if so, what is the best way to go about it?  Ideally I would like to keep my js and css files as is and not turn them into .aspx pages w/ js or css content types and asp script tags.

Thanks.

Coordinator
Jan 17, 2011 at 9:23 PM

HI gb1200 :) 

thanks for dropping by & giving a moment to check out the project :) Ok, totally understand the problem. In my own projects at work, we also have a CDN key/value in our appSettings element, which we prepend, etc. So i hear ya.

So, lets just imagine we can get YUICompressor.NET to do this. The first question would be: HOW does YUICompressor.net know what to substitute? Well, looking at your example ...

body { background: url([[Config.CdnUrl]]/background.jpg) }

You've created a custom variable in your code, right? Well, what happens with this css file when we are using it BEFORE we minifiy and/or combine? ie. we're just including that file in a stock web page? I guess it will error, as the browser won't be able to resolve that url, because the CDN variable is there, messing things up.

Ok .. so lets think about this. We're trying to put VARIABLES in css. Ding! What about .Less (pronounced dot less) ? The awesome guys that ported the original Ruby code to .NET have even included YUICompressor.NET in their own project! Do I smell a double rainbow, here? I do!

So hope over to .LESS and see how simple it is to add variables to CSS -and- minify the CSS .. all part of the .LESS library package. Want some magic unicorns with this package? At the time of me posting this reply, there's even a NuGet package, which REALLY makes your life easy :)

Does this help you?

Jan 17, 2011 at 10:07 PM

Yeah I was able to accomplish what I wanted to do but only by modifying the YUI .net source code and implementing my custom logic.  The .less stuff seems very cool - why reinvent the wheel?  I shall check it out.  Thanks!!

Coordinator
Jan 17, 2011 at 10:42 PM

:) great news gb1200!