1

What is the expected behaviour of bundling / minification when a local minified version of a file is and is not present?

I assumed that, when a local .min.js file is found it will just use that (when optimized for release) and if debug it will use the other. When optimized for release but no .min.js file is found, I thought it will minify that file accordingly.

However, I have found that even though I have a .min.js file in my scripts folder, that which is output in the bundle (in release) is not the same as that - it has been altered.

I should add that these have been added as ScriptBundles in the Bundle.config and all transforms / ignore lists etc have been left at the defaults.

Question arises in the case of a 3rd party library such as jQuery. I wouldn't want the files they supply to be minified by the bundling process - just to use the minified files that have been supplied.

Thanks.

obaylis
  • 2,904
  • 4
  • 39
  • 66
  • you can minify your version of jquery and the benefit is that when you do so you are sure that the file is there, but also you can add reference to the minified with the risk that someone may remove it and you will loose jquery. The reason you have the full jquery is that you can debug it in dev and deploy the minified, also for advanced user you can change it to do what you want (not advisable but still possible) – Jack M Oct 01 '14 at 15:48

2 Answers2

0

By default when registering script bundles using the ScriptBundle class, all included files will be minified again regardless of .min.js. The reason for this is because ScriptBundle uses a specific IBundleTransform implementation which always minifies the javascript file.

To override this behaviour a dummy implementation can be used:

public class NoTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
    }
}

Registering the bundle:

bundles.Add(new Bundle("~/bundles/jquerynotransform", new NoTransform()).Include("~/Scripts/jquery-{version}.js"));
Zeratul75
  • 36
  • 2
0

Your assumptions are correct.

If the local minified version is present, when bundling occurs, that is the version that will be used. It will be put through the minification again, and you can tell because it removes the licensing comments that jQuery says you must keep (as detailed in How can I preserve comments that matter in MVC 4 style bundling?) and also may rename variables.

If the local minified version is not present, then it will use the unminified version, which will be put through bundling/minification.

So in both cases, it will be put through minification.

Community
  • 1
  • 1
MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
  • Interesting. Thanks for the feedback. In my case it looks as if the minification had broken the jQuery file as I was getting errors that the function 'ready' was undefined. – obaylis Oct 06 '14 at 07:59
  • @obaylis interesting. Can't say I've ever seen that happen. – MikeSmithDev Oct 06 '14 at 11:36
  • It just got me thinking how does the minification process avoid situations like that? In the case of jQuery you wouldn't want any core function names to be minified / changed. – obaylis Oct 06 '14 at 11:43