Problem
I’m having an odd problem where the mvc4 bundler refuses to include files with the extension. min.js
I declare in my BundleConfig class that
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/Scripts/jquery")
.Include("~/Scripts/jquery-1.8.0.js")
.Include("~/Scripts/jquery.tmpl.min.js"));
}
I declare, in my opinion.
<html>
<head>
@Scripts.Render("~/Scripts/jquery")
</head><body>test</body>
</html>
And when it renders, it just renders one thing at a time.
<html>
<head>
<script src="/Scripts/jquery-1.8.0.js"></script>
</head>
<body>test</body>
</html>
Both scripts are rendered correctly if I rename jquery.tmpl.min.js to jquery.tmpl.js (and update the path in the bundle accordingly).
Is there a setting in the config that causes it to ignore.min.js files?
Asked by Fatal
Solution #1
The solution I first proposed is debatable (is a dirty hack). As numerous commenters have pointed out, the altered behavior has changed in the Microsoft.AspNet.Web.Optimization package, and the tweak no longer works. I’m currently unable to duplicate the problem with the package’s version 1.1.3.
Please review the System.Web.Optimization sources. For a better grasp of what you’re about to perform, use BundleCollection (for example, dotPeek). Read Max Shmelev’s response as well.
Original answer:
Rename.min.js to.js or do something similar.
public static void AddDefaultIgnorePatterns(IgnoreList ignoreList)
{
if (ignoreList == null)
throw new ArgumentNullException("ignoreList");
ignoreList.Ignore("*.intellisense.js");
ignoreList.Ignore("*-vsdoc.js");
ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
//ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
}
public static void RegisterBundles(BundleCollection bundles)
{
bundles.IgnoreList.Clear();
AddDefaultIgnorePatterns(bundles.IgnoreList);
//NOTE: it's bundles.DirectoryFilter in Microsoft.AspNet.Web.Optimization.1.1.3 and not bundles.IgnoreList
//...your code
}
Answered by Pavel
Solution #2
The following is what Microsoft suggests (and what I prefer to do in my projects):
As a result, you’ll have the script right away. In DEBUG mode, js is included, whereas in RELEASE mode, script.min.js is included.
There is also a.debug.js version available. In that situation, the file is placed in DEBUG with the following priority:
in RELEASE:
By the way, in MVC4, the only need to have.min versions of your scripts is if the minified version cannot be executed automatically. The following code, for example, cannot be automatically obfuscated:
if (DEBUG) console.log("Debug message");
In all other circumstances, a debug version of your script will suffice.
Answered by Max Shmelev
Solution #3
If you only have a minified version of a file, the best approach I’ve found is to duplicate the minified file, delete the.min suffix from the copied file’s name, and then reference the non-minified file name in your bundle.
As an example, suppose you bought a js component and received a file named some-lib-3.2.1.min.js. To include this file in a bundle, follow these steps:
The fact that a file with no’min’ in its name is actually minified shouldn’t cause any problems (apart from the fact that it’s virtually illegible). It’s only utilized in debug mode, and it’s written out separately. The pre-compiled min file should be included in your bundle when not in debug mode.
Answered by joelfp
Solution #4
Instead of using ScriptBundle, I found a decent solution that works at least in MVC5. Simply use Bundle instead of ScriptBundle. In this scenario, it does not have the clever behavior of ScriptBundle that we dislike (ignoring.min, etc.). I utilize Bundle for 3rd party scripts with.min and.map files, and ScriptBundle for our code in my solution. There haven’t been any disadvantages to it that I’ve noticed. To make it operate this way, add the original file, such as angular.js, and it will load angular.js in debug mode and bundle angular.min.js in release mode.
Answered by Ilya Chernomordik
Solution #5
BundleTable must be disabled in order to render *.min.js files. EnableOptimizations is a globally applicable parameter that affects all bundles.
You can construct your own ScriptBundle type that temporarily enables optimizations while enumerating files in a bundle if you just wish to activate optimizations for specific bundles.
public class OptimizedScriptBundle : ScriptBundle
{
public OptimizedScriptBundle(string virtualPath)
: base(virtualPath)
{
}
public OptimizedScriptBundle(string virtualPath, string cdnPath)
: base(virtualPath, cdnPath)
{
}
public override IEnumerable<BundleFile> EnumerateFiles(BundleContext context)
{
if (context.EnableOptimizations)
return base.EnumerateFiles(context);
try
{
context.EnableOptimizations = true;
return base.EnumerateFiles(context);
}
finally
{
context.EnableOptimizations = false;
}
}
}
For bundles where the minified file should always be utilized, regardless of whether a non-minified file exists, use OptimizedScriptBundle instead of ScriptBundle.
Kendo UI for ASP.NET MVC is an example project that is delivered as a collection of just minified files.
bundles.Add(new OptimizedScriptBundle("~/bundles/kendo")
.Include(
"~/Scripts/kendo/kendo.all.*",
"~/Scripts/kendo/kendo.aspnetmvc.*",
"~/Scripts/kendo/cultures/kendo.*",
"~/Scripts/kendo/messages/kendo.*"));
Answered by Steven Liekens
Post is based on https://stackoverflow.com/questions/11980458/bundler-not-including-min-files