3

I have the following ScriptBundle defined in BundleConfig.cs-

    public class BundleConfig
    {
        public static void RegisterBundles(BundleCollection bundles)
        {
          bundles.Add(new ScriptBundle("~/js/yepnope").Include(
                "~/Scripts/yepnope.{version}.js"));
       }
    }

It isn't in fact the only bundle, but that is all in there that is pertinent to the question - the rest is just other bundle definitions.

When in "debug" mode as set in the web.config-

<compilation debug="true" targetFramework="4.5">

Both the full and minified versions of the script are sent to the browser-

<script src="/Scripts/yepnope.1.5.4-min.js"></script>
<script src="/Scripts/yepnope.1.5.4.js"></script>

The script is added using the HTML helper like so-

@section HeadScripts
{
    @Scripts.Render("~/js/yepnope")
}

This is an MVC4 project running in Visual Studio 2012 on Windows 7 Professional 64-bit.

Whatever I do I cannot get the {version} wildcard to behave as described in the Microsoft documentation-

Note: Unless EnableOptimizations is true or the debug attribute in the compilation Element in the Web.config file is set to false, files will not be bundled or minified. Additionally, the .min version of files will not be used, the full debug versions will be selected.

For ASP.NET MVC 4, this means with a debug configuration, the file jquery-1.7.1.js will be added to the bundle. In a release configuration, jquery-1.7.1.min.js will be added. The bundling framework follows several common conventions such as:

  • Selecting “.min” file for release when “FileX.min.js” and “FileX.js” exist.
  • Selecting the non “.min” version for debug.
  • Ignoring “-vsdoc” files (such as jquery-1.7.1-vsdoc.js), which are used only by IntelliSense.

The {version} wild card matching shown above is used to automatically create a jQuery bundle with the appropriate version of jQuery in your Scripts folder. In this example, using a wild card provides the following benefits:

  • Allows you to use NuGet to update to a newer jQuery version without changing the preceding bundling code or jQuery references in your view pages.
  • Automatically selects the full version for debug configurations and the ".min" version for release builds.

If I add "EnableOptimizations" it seems to behave as expected.

Has anyone else noticed this or found a solution?

6
  • Publishing without the debug attribute also appears to cause it to generate a single minified version of the file from the full .js file - the minified variable names are different to those in the provided minified version. Commented Sep 6, 2013 at 17:36
  • You probably mean for that file to be named .min.js and not -min.js. Commented Sep 6, 2013 at 17:47
  • 1
    @MikeSmithDev It probably came as -min.js. It's a common problem using NuGet package with MVC4: you grab a package and it comes with a .js and either a -min.js or .min.js. If you get a .min.js file, everything should work. But if you get a -min.js file it just causes problems. Better to get rid of it entirely, as in my answer. Commented Sep 6, 2013 at 17:49
  • @TimothyShields removing it works, but following the docs and having a '.min.js' file works too, and in a quick test, the optimized file was smaller using the .min than when it wasn't there, so I will advocate renaming the file and using optimization as designed. Commented Sep 6, 2013 at 18:22
  • @MikeSmithDev If the default minifier is less efficient than some other one that was used to create the packaged min.js, you can switch the minifier to be that instead... Commented Sep 6, 2013 at 18:24

1 Answer 1

5

MVC4 only knows how to handle a .min.js file. It doesn't recognize -min.js (with a dash).

The way I typically do this, with easy success, is to get rid of any .min.js or -min.js files provided by libraries that provide both a .js and either a .min.js or -min.js. By default, MVC will automatically minify any bundled .js files when you deploy your website, so there's no need to use the provided .min.js or -min.js files.

This isn't necessarily a direct answer to your particular question - it's a way to circumvent the problem entirely.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.