Bundler not including .min files
Solution 1
The solution I originally posted is questionable (is a dirty hack). The tweaked behaviour has changed in Microsoft.AspNet.Web.Optimization package and the tweak does not work anymore, as pointed out by many commenters. Right now I cannot reproduce the issue at all with the version 1.1.3 of the package.
Please see sources of System.Web.Optimization.BundleCollection (you can use dotPeek for example) for better understanding of what you are about to do. Also read Max Shmelev's answer.
Original answer:
Either rename .min.js to .js or do something like
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
}
Solution 2
Microsoft implies the following behavior (and I prefer to follow it in my projects):
short version
- You have both debug and minified versions of a script in your project under the same folder:
- script.js
- script.min.js
- You add only script.js to a bundle in your code.
As a result you will automatically have the script.js included in DEBUG mode and script.min.js in RELEASE mode.
long version
You can have also .debug.js version. In that case the file is included in the following priority in DEBUG:
- script.debug.js
- script.js
in RELEASE:
- script.min.js
- script.js
note
And by the way, the only reason to have a .min versions of your scripts in MVC4 is the case when the minified version can not be processed automatically. For example the following code can not be obfuscated automatically:
if (DEBUG) console.log("Debug message");
In all the other cases you can go with just a debug version of your script.
Solution 3
If all you have is a minified version of a file, the simplest solution I've found is to copy the minified file, remove .min from the copied file's name, then reference the non-minified file name in your bundle.
For example, let's say you purchased a js component and they gave you a file called some-lib-3.2.1.min.js. To use this file in a bundle, do the following:
Copy some-lib-3.2.1.min.js and rename the copied file to some-lib-3.2.1.js. Include both files in your project.
-
Reference the non-minified file in your bundle, like this:
bundles.Add(new ScriptBundle("~/bundles/libraries").Include( "~/Scripts/some-lib-{version}.js" ));
Just because the file without 'min' in the name is actually minified shouldn't cause any issues (other than the fact it's essentially unreadable). It's only used in debug mode and gets written out as a separate script. When not in debug mode the pre-compiled min file should be included in your bundle.
Solution 4
I have found a good solution that works at least in MVC5, you can just use Bundle
instead of ScriptBundle
. It does not have the smart behavior of ScriptBundle
that we don't like (ignoring .min, etc.) in this case. In my solution I use Bundle
for 3d party scripts with .min and .map files and I use ScriptBundle
for our code. I have not noticed any drawbacks of doing it. To make it work this way you will need to add original file e.g. angular.js, and it will load angular.js in debug and it will bundle the angular.min.js in release mode.
Solution 5
To render *.min.js
files, you must disable BundleTable.EnableOptimizations
, which is a global setting that applies to all bundles.
If you want to enable optimizations for specific bundles only, you can create your own ScriptBundle
type that temporarily enables optimizations when enumerating files in the bundle.
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;
}
}
}
Use OptimizedScriptBundle
instead of ScriptBundle
for bundles whose minified file should always be used, regardless of whether a non-minified file exists.
Example for Kendo UI for ASP.NET MVC, which is distributed as a collection of only 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.*"));
Related videos on Youtube
Fatal
Updated on September 15, 2022Comments
-
Fatal over 1 year
I have a weird issue with the mvc4 bundler not including files with extension .min.js
In my BundleConfig class, I declare
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")); }
In my view, I declare
<html> <head> @Scripts.Render("~/Scripts/jquery") </head><body>test</body> </html>
And when it renders, it only renders
<html> <head> <script src="/Scripts/jquery-1.8.0.js"></script> </head> <body>test</body> </html>
If I rename the jquery.tmpl.min.js to jquery.tmpl.js (and update the path in the bundle accordingly), both scripts are rendered correctly.
Is there some config setting that is causing it to ignore '.min.js' files?
-
Eric J. over 11 yearsI am using MVC 4 bundler and it is including .min.js files.
-
Fatal over 11 yearsthe RTM version or the RC? it was working ok in the RC for me too
-
Pieter Germishuys over 11 yearsThe idea is that working in debug mode, that the "dev" version without minification will be used and when you are in non-debug mode, that the minified version is picked. To see it in action, change your web.config debug value from true to false.
-
Fatal over 11 yearsin some cases you dont have the non-minified version of the script though. I could possibly understand it if both files existed.
-
nothingisnecessary about 9 yearsIt's a bummer that it works like this by default... sure, the file may already be minified, but I think Microsoft failed to see the benefit of adding pre-minified scripts to bundles for cache-busting purposes (the nice little
v
parameter hash that gets added to the url, and changes when file contents change)
-
-
Dan B over 11 yearsThanks from me too - so adding this to my MVC4 gotcha list :)
-
Giedrius over 11 yearsthis was long wtf moment, they could have been adding those min files commented out with reason or something to result, now whole hour wasted wondering who is stealing my script files from output.
-
danmiser over 11 yearsPer Fatal's comment in the OP, this solution will end up rednering duplicate references for both the minified and regular versions if they both exist (e.g. jquery).
-
Eonasdan over 11 yearsnice explanation however, the OP's problem was that some scripts (
jquery.tmpl
) don't have, didn't come with, didn't get downloaded the non-min version of the file. The bundler ignores the script files altogether if it doesn't have a non-min version in debug mode -
Diego over 11 yearsAgree, nice explanation but it does not answer at all OP's question.
-
user981375 over 11 yearsShort version didn't work for me. "script.js" would be included in both of these release types. I toggled DEBUG/RELEASE and (when I looked at the source) 'script.js' was the one selected/rendered.
-
Max Shmelev over 11 yearsuser981375, you also need to set <compilation debug="false" /> in the web.config file
-
GONeale over 11 yearsI don't like the idea of doing this, but I have no other option, the library I am using didn't provide anything but the .min.js :(
-
Fatal over 11 yearsI'm not a fan of this proposed solution at all. You're now having to maintain the set of scripts in (at least) two places
-
Adaptabi about 11 yearsM$, when somefile.min.js is explicitely specified, or when just the .min.js file exists, then only include the .min.js file! Otherwise just apply the current behavior!
-
James Reategui about 11 yearsI prefer this answer because it is a "best practices" approach and explains how the bundler's default rules can be followed to achieve the same goal.
-
Oliver about 11 yearsThis pretty much defeats the purpose of the bundling, don't you think?
-
J.P. almost 11 yearsIncredible that the actual class is called "IgnoreList", yet it derives straight from Object. No LINQ, no iteration. - msdn.microsoft.com/en-us/library/…
-
Chad Levy almost 11 yearsBundling already works this way. If you run the project in debug mode each CSS file is loaded individually, however when you run in release they're combined and minified into one. The code within the if block is identical to what's rendered on the page automatically in debug mode.
-
MoSs over 10 yearsAdding debug="false" still doesn't work for me. min.js files are still not included even though I clear the IgnoreList as well...
-
jmrnet almost 10 yearsI also had to add: bundles.DirectoryFilter.Clear();
-
Mike Cheel almost 10 yearsDoing the code above did nothing for me. After calling Clear() on bundles.DirectoryFilter did things start working (without clearing the ignore list).
-
mykola.rykov almost 10 yearsI can reproduce it with Microsoft.AspNet.Web.Optimization 1.0.0, and it is fixed at v. 1.1.3.
-
Buthrakaur over 9 yearsIs it possible to add custom transformation logic (IItemTransform) for *.min.css files? The CssRewriteUrlTransform transformation doesn't seem to get invoked on *.min.css files, so just a presence of *.min.css file breaks URL references (they're not being transformed).
-
Anirudha Gupta over 9 yearsEverytime nuget download the file, you need to rename then.
-
R K Sharma over 9 yearsI know its not the right way to do it, but just a temporary solution
-
Anirudha Gupta over 9 yearsput this line are working for me // BundleTable.EnableOptimizations = true;
-
Steven Liekens about 8 yearsIt doesn't seem to work when optimizations are disabled (
ScriptTable.EnableOptimizations = false
). Script paths that contain a pattern are not rendered (e.g.~/Scripts/thirdpartylib/*.js
). It does work whenEnableOptimizations = true
, but so doesScriptBundle
. -
Ilya Chernomordik about 8 yearsI use it with false as well (during development) and I don't have problems that you describe, so it might be something else that's affecting it for you.You also need to do IncludeDirectory instead of include for the pattern to work.
-
Steven Liekens about 8 yearsAre you sure it is rendering the ".min.js" version of your file? Are you using bundles from the namespace
System.Web.Optimizations
or from the nuget packageMicrosoft.Web.Optimizations
? -
Ilya Chernomordik about 8 yearsI use System.Web.Optimization and I checked that it renders min version, but I have figured out I do not use IncludeDirectory in fact, but it would be strange if that did not work with min version though... As IncludeDirectory is not enough for me I do a custom scan and add all the filter using Include, so might be that the pattern and IncludeDirectory does not work properly there, though it's a bit strange.
-
Steven Liekens about 8 yearsThe problem only exists when including files using a wildcard pattern. Pattern matching doesn't work for
*.min.js
files. Not even when usingBundle
instead ofScriptBundle
. -
Ilya Chernomordik about 8 yearsYou should do *.js, not *.min.js, the whole point is that it figures out itself. Probably that is the reason?
-
Steven Liekens about 8 yearsNo, the problem is two-fold: only a
.min.js
file exists, and the*.js
pattern does not match files ending in.min.js
. -
Ilya Chernomordik about 8 yearsYou can try adding the .js file in addition to the .min.js file to the same folder. That might fix it. I think it ignores all the .min.js files alltogether. As .min.js never handled on their own, but only as a part of original file when optimization is turned on. Sounds reasonable to have both files since one is used for development and another for production.
-
Steven Liekens about 8 yearsThat's assuming that the non-minified file is available and that you are licensed to use it. That's not a given for many libraries and plugins on the internet.
-
Ilya Chernomordik about 8 yearsThen just rename you .min.js to .js since you will never have access to the full version :) Another option is to copy the file and make .js. But I see the problem, but I never used any libraries that are not available in both (or at least unminifed) version, so never had this problem.
-
user2211290 over 7 yearsMinification automatically happens with bundling when debug mode is set to false in web.cong . Even debug mode we cab force to bundle using or bundletable.enableoptimizations = true in bundleconfig.cs
-
Felipe over 6 yearsI came across some missing scripts in a production publish action, and thinking it was an MVC issue, I found this answer.. lo and behold, kendo is involved. Having worked with it for too long, I do anything to get away from kendo
-
Steven Liekens over 6 years@Felipe I was in the same situation which is why I wrote this answer. Kendo is horrible. I hope my example is helpful.