Some of my styles use url(../img/sprites/main_sprite.png) to local resources in development and stage. However in production I use CDN and all my static resources are on it. Is it possible to transform bundles so that all urls in .css are replaced with cdn path?
For Example:
.Logo {
background-image: url(../img/sprites/main_sprite.png);
}
However, in production I would like it to be
.Logo {
background-image: url(http://MyCdn.com/img/sprites/main_sprite.png);
}
I already use CssRewriteUrlTransform() to rewrite my relative paths to absolute, so the resources can be found after they bundled.
I was thinking to extend the class as something like this
public string Process(string includedVirtualPath, string input)
{
if (_useCdn)
{
return new CssRewriteUrlTransform().Process(_cdn + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);
}
else
{
return new CssRewriteUrlTransform().Process("~" + VirtualPathUtility.ToAbsolute(includedVirtualPath), input);
}
}
However, Process must have VirtualPath, otherwise it throws an exception when I append CDN path.
Is there an equivalent of this class to rewrite URLS with CDN in it?
I was not able to find an existing solution. So, I used CssRewriteUrlTransform code as the base for my CDNStylesTransformer. I hope it will be useful for you too.
In your BundleConfiguration class