3

I'm working on an MVC 5.0 (.Net 4.5) application where I need to apply a custom JavaScript transform to my included bundle files. One of those files, which I'm calling dummy.js for illustration purposes, has a minified file called dummy.min.js.

I created a custom script transform to replace injected window.jQuery references with a different expression. Everything works fine when I run locally and in debug mode, but when debug mode is turned off in the Web.config file, the Bundle returns the contents of the dummy.min.js file, but my script transform is not applied to it. It only gets applied to JavaScript files that don't have an associated .min.js file.

Does anyone have an idea on how to resolve this? It almost sounds like a bug in MVC.

A workaround is to remove the minified file. This post kind of addresses my situation by suggesting removing the .min.js file since MVC minifies by default, but I'm looking for an alternative solution (if any).

Thank you so much in advance.


Here's how to reproduce the above:

If you're interested in reproducing my issue, here's a quick BundleConfig and the actual custom script transform. It replaces all instances of window.jQuery with window.$jq1_9||window.jQuery, assuming it is injected via a self-executing anonymous function.

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

public class InjectedJQueryVariableRewriteTransform : System.Web.Optimization.IItemTransform
{
    public string Process(string includedVirtualPath, string javaScriptCode)
    {
        // TODO: I understand this approach is naiive, but it does the trick for now.
        return javaScriptCode.Replace("window.jQuery", "window.$jq1_9 || window.jQuery");
    }
}

If you have Visual Studio 2012 and MVC 4, you will need version 1.1.0 of the System.Web.Optimization assembly, which you can obtain by running the following command in the Nuget Package Manager. At time of writing it installs version 1.1.2 of the package.

Install-Package Microsoft.AspNet.Web.Optimization

Here's the sample JavaScript dummy.js. You can create a copy of it and name it dummy.min.js:

(function ($) {
    "use strict";

    // TODO: Do something interesting...
})(window.jQuery);

Set the debug attribute to false in the following element in Web.config:

<compilation debug="false" targetFramework="4.5" />

Assuming the application's port is 9221, render the bundle in Firefox or Chrome:

http://localhost:9221/bundles/dummy

You will see that when debug is set to true, the transform is applied, as shown below:

(function(){"use strict"})(window.$jq1_9||window.jQuery) 

When it is set to false. It is ignored and only the .min.js file is used:

(function(){"use strict"})(window.jQuery)

1 Answer 1

5

If you add this line:

bundles.FileExtensionReplacementList.Clear();

you will remove the rule for using .min files when bundling is enabled. You will remove all rules, unfortunately, so if you need any of the other ones you'll need to add them manually. Also, this will change the rules for all bundles.

If you just want to disable these replacement rules for just one bundle, you can just set the EnableFileExtensionReplacements property to false on that specific bundle:

var bundle = new ScriptBundle("...");
bundle.EnableFileExtensionReplacements = false;
Sign up to request clarification or add additional context in comments.

1 Comment

I ended up using a technique similar to what you described above. All of this is in a helper method and a Web.config appSettings key called "AllowMinifications" so that I can enable and disable script minification. Bundle bundle = new ScriptBundle("virtual path"); bundle.EnableFileExtensionReplacements = false; To disable minification I did the following: Bundle bundle = new Bundle("virtual path"); bundle.EnableFileExtensionReplacements = false;

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.