Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Created October 3, 2012 00:51
Show Gist options
  • Save prabirshrestha/3824265 to your computer and use it in GitHub Desktop.
Save prabirshrestha/3824265 to your computer and use it in GitHub Desktop.
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/Scripts/jquery")
.Include("~/assets/javascripts/vendors/jquery-{version}.js"));
bundles.Add(new ScriptBundle("~/Scripts/app")
.IncludeFromSprocketFile("~/assets/javascripts/main.js"));
var templateBundle = new Bundle("~/Scripts/templates").IncludeDirectory("~/assets/templates", "*.html");
templateBundle.Transforms.Add(new HtmlBundleTransform());
bundles.Add(templateBundle);
}
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
public class HtmlBundleTransform : IBundleTransform
{
private readonly string javascriptVariableName;
public HtmlBundleTransform()
: this("JST")
{
}
public HtmlBundleTransform(string javascriptVariableName)
{
this.javascriptVariableName = javascriptVariableName;
}
public void Process(BundleContext context, BundleResponse response)
{
var sb = new StringBuilder();
sb.AppendFormat("window.{0} = window.{0} || {{}};", this.javascriptVariableName);
foreach (var file in response.Files)
sb.AppendFormat("{0}['{1}'] = \"{2}\";", this.javascriptVariableName, Path.GetFileName(file.FullName), File.ReadAllText(file.FullName));
response.Content = sb.ToString();
response.ContentType = "text/javascript";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment