Created
May 23, 2018 19:45
-
-
Save iamsingularity/d795bb09042cd836aa9b22ddc0823b7a to your computer and use it in GitHub Desktop.
Unbundling scripts for debugging
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using Newtonsoft.Json; | |
using System.IO; | |
using System.Text; | |
namespace MyApplication | |
{ | |
public static class Bundler | |
{ | |
const string FILENAME = "bundleconfig.json"; | |
public static MvcHtmlString RenderScripts(string bundlePath, bool? expandBundle = null) | |
{ | |
if (expandBundle ?? HttpContext.Current.IsDebuggingEnabled) | |
{ | |
// we're expanding the bundle so get the desired bundle configuration | |
string baseFolder = HttpContext.Current.Server.MapPath(""); | |
var configFile = Path.Combine(baseFolder, FILENAME); | |
var bundles = GetBundles(configFile); | |
var bundle = (from b in bundles where b.OutputFileName.Equals(bundlePath, StringComparison.InvariantCultureIgnoreCase) select b).FirstOrDefault(); | |
if (bundle == null) | |
return null; | |
// build script elements for each input file in the bundle | |
var sb = new StringBuilder(); | |
var inputFiles = GetBundleInputFiles(baseFolder, bundle); | |
foreach (var inFile in inputFiles) | |
{ | |
var fullPath = VirtualPathUtility.ToAbsolute(string.Format("~/{0}", inFile)); | |
if (bundlePath.EndsWith(".css")) | |
{ | |
sb.AppendLine(string.Format("<link href='{0}' rel='stylesheet'></link>", fullPath)); | |
} | |
else | |
{ | |
sb.AppendLine(string.Format("<script src='{0}' type='text/javascript'></script>", fullPath)); | |
} | |
} | |
return new MvcHtmlString(sb.ToString()); | |
} | |
else | |
{ | |
// we're not expanding the bundle to just add as-is | |
var fullPath = VirtualPathUtility.ToAbsolute(string.Format("~/{0}", bundlePath)); | |
if (bundlePath.EndsWith(".css")) | |
{ | |
return new MvcHtmlString(string.Format("<link href='{0}' rel='stylesheet'></link>", fullPath)); | |
} | |
else | |
{ | |
return new MvcHtmlString(string.Format("<script src='{0}' type='text/javascript'></script>", fullPath)); | |
} | |
} | |
} | |
// below is mostly copied from Mads Kristensen's BundlerMinifier -> BundleHandler.cs | |
static List<string> GetBundleInputFiles(string baseFolder, Bundle bundle) | |
{ | |
List<string> inputFiles = new List<string>(); | |
string ext = Path.GetExtension(bundle.OutputFileName); | |
foreach (string inFile in bundle.InputFiles) | |
{ | |
string fullPath = Path.Combine(baseFolder, inFile); | |
if (Directory.Exists(fullPath)) | |
{ | |
DirectoryInfo dir = new DirectoryInfo(fullPath); | |
var files = dir.GetFiles("*" + ext, SearchOption.TopDirectoryOnly); | |
inputFiles.AddRange(files.Select(f => string.Format("{0}/{1}", inFile, f.Name))); | |
} | |
else | |
{ | |
inputFiles.Add(inFile); | |
} | |
} | |
return inputFiles; | |
} | |
static IEnumerable<Bundle> GetBundles(string configFile) | |
{ | |
FileInfo file = new FileInfo(configFile); | |
if (!file.Exists) | |
return Enumerable.Empty<Bundle>(); | |
string content = File.ReadAllText(configFile); | |
return JsonConvert.DeserializeObject<IEnumerable<Bundle>>(content); | |
} | |
} | |
class Bundle | |
{ | |
[JsonProperty("outputFileName")] | |
public string OutputFileName { get; set; } | |
[JsonProperty("inputFiles")] | |
public List<string> InputFiles { get; set; } = new List<string>(); | |
[JsonProperty("minify")] | |
public Dictionary<string, object> Minify { get; set; } = new Dictionary<string, object> { { "enabled", true } }; | |
[JsonProperty("includeInProject")] | |
public bool IncludeInProject { get; set; } | |
[JsonProperty("sourceMaps")] | |
public bool SourceMaps { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment