Creates JavaScript bundles using CDNs depending on the installed nuget package version with local fallback.
I've added a list of packages that i use with this to the wiki. Please feel free to submit your packages / cdn paths and fallback expressions there, so that the list can grow. https://github.com/mbeckenbach/nugetBundling/wiki
##Install via Nuget
PM> Install-Package nugetBundling
##How to use
- Install the nugetBundling package via nuget powershell console or gui
- Open your Bundle Config. Typically located in App_Start\BundleConfig.cs
- Add a using statement for nugetBundling:
using nugetBundling;
- Enable cdn usage
public static void RegisterBundles(BundleCollection bundles)
{
// Use CDN scripts if available
bundles.UseCdn = true;
- Install a JavaScript nuget package lke JQuery
- Find a working cdn. If the local file name contains a version number, the cdn url should contain a version numer that equals that local version numer. Thats how we can match the installed local version with your cdn version.
- Add a bundle to your collection
bundles.Add(nugetBundling.JSBundle.Create("JQuery", "//ajax.googleapis.com/ajax/libs/jquery/{version}/jquery.min.js", "window.jQuery", "~/Scripts/jquery-{version}.js"));
- Use your new bundle in a view
@Scripts.Render("~/bundles/js/JQuery")
- Test Simply change the cdn url to something that wont work or disconnect your internet connection. Then run your application. The script library should get loaded from your local file. Reconnect the internet / undo your change to a wrong cdn path and the script will be loaded from a cdn. (Have a look at F12 tools network tab.)
##Get a list of all installed nuget packages nugetBundling includes helper class es that read your installed nuget packages.config and converts them into a nice list object.
Simply create a new packageReader like this and use it the way you like:
NugetReader reader = new NugetReader(HttpContext.Current.Server.MapPath("~/"));
List<NugetPackage> installedPackages = reader.NugetPackages;
NugetPackage jquery = installedPackages.SingleOrDefault(x => x.Id == "jQuery");
string jqueryVersion = jquery.Version;