Nancy.BundleIt
A bundling library made for Nancyfx as an alternative to Cassette.
Nancy.BundleIt minifies, obfuscates, concatenates, caches and versions all of your Javascript and CSS files. It uses the well tested and proven Port of Yahoo!'s Java YUICompressor to .NET to minify & obfuscates the code.
DonnyV@Twitter
If your using BundleIt in a project you would like to share. Send me the url and I'll add it here!Features
- Manage your debug and release versions of your .js and .css files in one place.
- Minify javascript and css using YUICompressor
- Override default YUICompressor settings for each file.
- Bundles files automatically in release mode.
- Support for CDNs
- Force release mode or debug mode
- Force an exception to be thrown when a file is missing.
- If ".min." is detected in file name, BundleIt will use source as is and not compress it.
- Works with .NET and Mono.
- Bundles can be combined to make new bundles. Only need to declare files once!
- In memory caching for optimal performance.
- Aggressive HEADER cache settings. Using ETag and Cache-Control. Only download bundle once!
How to install
Nuget will drop a class into your project called "BundleItConfig.cs".
using System.Collections.Generic;
using Nancy.BundleIt;
/// <summary>
/// Configures Nancy.BundleIt assets on application startup.
/// There can only be 1 class that uses the interface IBundleItConfig per project.
/// BundleIt will use the first one it finds.
/// </summary>
public class BundleItConfig : IBundleItConfig
{
public void Configure(Bundles bundles, ConfigSettings settings)
{
// Optional settings
//settings.ScriptPath = "_scriptbundle";
//settings.StylePath = "_stylebundle";
//settings.ThrowExceptionWhenFileMissing = true;
//settings.ForceDebugMode = false;
//settings.ForceReleaseMode = true;
// Add scripts based on relative path from project's root.
var bundleRactive = bundles.AddScripts("ractive", new List<BundleItFile>
{
new BundleItFile("app/vendors/ractiveJS/ractive.0.4.0.js", "app/vendors/ractiveJS/ractive.0.4.0.min.js"),
new BundleItFile("app/vendors/ractiveJS/ractive-transitions-fade.js")
});
// You can also add a CDN and use bundles you have already created.
var bundleBase = bundles.AddScripts("base", new List<BundleItFile>
{
new BundleItFile("app/vendors/jquery.1.11.0.js", "//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js", true),
new BundleItFile(bundleRactive),
new BundleItFile("app/vendors/lodash.2.4.1.min.js"),
new BundleItFile("app/common/js/toolbox.js")
});
// Add your CSS also
var base_bundle_css = bundles.AddStyles("base", new List<BundleItFile>
{
new BundleItFile("app/common/css/common.css")
});
}
}
Remember to add a reference in the "RazorConfig.cs" file.
public class RazorConfig : IRazorConfiguration
{
public IEnumerable<string> GetAssemblyNames()
{
yield return "Nancy.BundleIt";
}
public IEnumerable<string> GetDefaultNamespaces()
{
yield return "Nancy.BundleIt";
}
public bool AutoIncludeModelNamespace
{
get { return false; }
}
}
Then call your bundles in the view.
@using Nancy.BundleIt
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Account Dashboard</title>
@BundleItAssets.RenderCSS("base")
</head>
<body>
<b>Account Dashboard</b>
<!-- content -->
@BundleItAssets.RenderJS("base")
</body>
</html>
YUI Compressor Notes
It is possible to prevent a local variable, nested function or function argument from being obfuscated by using "hints". A hint is a string that is located at the very beginning of a function body like so:
function fn (arg1, arg2, arg3) {
"arg2:nomunge, localVar:nomunge, nestedFn:nomunge";
...
var localVar;
...
function nestedFn () {
....
}
...
}
The hint itself disappears from the compressed file.
Copyright
Copyright © 2014 Donny Velazquez and contributors
License
Nancy.BundleIt is licensed under MIT.