Created
August 31, 2018 14:10
-
-
Save RichardD2/19d124e3a1c1bee2973a62823b090747 to your computer and use it in GitHub Desktop.
Modified version of "BetterStyleBundle" from https://tech.trailmax.info/2014/01/mvc-bundling-and-minification-a-betterstylebundle/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class AsIsBundleOrderer : IBundleOrderer | |
{ | |
private AsIsBundleOrderer() | |
{ | |
} | |
public static IBundleOrderer Instance { get; } = new AsIsBundleOrderer(); | |
public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files) => files; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class BetterStyleBundle : Bundle | |
{ | |
public BetterStyleBundle(string virtualPath) : base(virtualPath) | |
{ | |
} | |
public override IBundleOrderer Orderer | |
{ | |
get { return AsIsBundleOrderer.Instance; } | |
set { } | |
} | |
public override Bundle Include(params string[] virtualPaths) | |
{ | |
foreach (string path in virtualPaths) | |
{ | |
Include(path, CssRewriteUrlTransformAppRelatve.Instance); | |
} | |
return this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class CssRewriteUrlTransformAppRelatve : IItemTransform | |
{ | |
private static readonly Regex UrlRegex = new Regex("url\\(['\"]?(?<url>[^)]+?)['\"]?\\)", RegexOptions.Compiled | RegexOptions.ExplicitCapture); | |
private CssRewriteUrlTransformAppRelatve() | |
{ | |
} | |
public static IItemTransform Instance { get; } = new CssRewriteUrlTransformAppRelatve(); | |
public string Process(string includedVirtualPath, string input) | |
{ | |
if (includedVirtualPath == null) throw new ArgumentNullException(nameof(includedVirtualPath)); | |
if (string.IsNullOrWhiteSpace(input)) return input; | |
string directory = VirtualPathUtility.GetDirectory(includedVirtualPath); | |
if (string.IsNullOrWhiteSpace(directory)) return input; | |
if (directory[directory.Length - 1] != '/') directory += "/"; | |
return UrlRegex.Replace(input, RebaseUrl); | |
string RebaseUrl(Match match) | |
{ | |
string url = match.Groups["url"].Value; | |
if (string.IsNullOrWhiteSpace(url)) return match.Value; | |
if (url.StartsWith("data:", StringComparison.Ordinal)) return match.Value; | |
if (url.StartsWith("https://", StringComparison.Ordinal)) return match.Value; | |
if (url.StartsWith("http://", StringComparison.Ordinal)) return match.Value; | |
if (url[0] == '/') return match.Value; | |
url = VirtualPathUtility.ToAbsolute(directory + url); | |
return "url(\"" + url + "\")"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment