3

I'm thinking of using bundling for my group's ASP.NET MVC3 project, and I can't break it cause I'm a junior dev.

Does JavaScript minification affect JavaScript function names - i.e. can I still call the functions that I minify by the same name from inline code? Also, does it ever effect how code behaves - or is JavaScript functionality and order fully preserved?

1
  • Considering I ran into a situation today where the minified version of JS works but the non-minified version is broken, yes, it can. For my site which uses Bootstrap 3.4.1, the JS for our hamburger button works normally, but breaks when minified. The minified JS for Bootstrap 3.4.0 works though. Commented Oct 10, 2019 at 23:45

3 Answers 3

5

Bundling and Minification is not going to break your code by renaming your function names. The minification optimizes code through removing unnecessary whitespace, removing comments, and shortening variable names to one character.

Say you make this function:

function BigHonkingFunctionName(bigvariablename1, bigvariablename2) {
    //I love this function
    alert(bigvariablename1 + " " + bigvariablename2);
}

The minified result will be:

function BigHonkingFunctionName(n,t){alert(n+" "+t)}

So as you see, it won't impact how you call or use the function.

Reading on Bundling and Minification.

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

3 Comments

I'll caveat this by saying the minification can break functionality depending on how the original source file was written. I just experienced the jQuery.DataTables cookie functionality (datatables.net/new/1.7) breaking because the VS minifier incorrectly re-aliased the already existing alias for document -- for some reason it didn't pick up on the fact that the variable already existed, so it "helpfully" added it within local function scope, which resulted in a null exception.
@drzaus hmmm that may be, however packages like DataTables will include a minified js file. When you include scripts that also have a minified version, the .min.js will be used during release mode so you shouldn't run into any issues in that case. I'd be interested to know if that helped in your case.
weirdly enough it did not; i believe it had to do with the version of dataTables i had -- upgrading to 1.9.4 (with a different pre-minified version) seemed to fix things, which is why I wanted to add the caveat that it depends on the source file and minifier used.
3

Normally Javascript minification does not change function names, and you can use them without worry. Javascript minification usually removes empty-spaces, new-line characters, shortens local variable names, shortens function parameter names.

Though you might get in trouble if your code uses eval statements which are referencing variables defined in its scope of access. Some minifier do not touch eval statements, which can be a bit problematic if the names of variables referenced by eval had been minified by the tool.

For example, following Javascript code block can be minified as following:

Source JS:

function eval_test(var_1, var_2) {
    var_1 = "Updated var_1";
    var_2 = "Updated var_2";

    var a = eval("var_1 * var_2") + "<br>";
}

Minified JS:

function MyFunction(n,t){n="Updated var_1";t="Updated var_2";
var i=eval("var_1 * var_2")+"<br>"}

In this example the eval call will break after minification, because parameters var_1, var_2 of function eval_test are minified to n and t respectively. But the eval statement is still unchanged.

Still, some minfication tools do not minify parameter/variable names, in such a case eval statement will work perfectly fine. But keeping unminified parameter/variable names negatively affects the minification, because your code might not be as compressed/minified as it should.

You can try following online minification tools to test and verify minified output:

http://utilninja.com/Computing/JavascriptMinifier

https://javascript-minifier.com/

https://jscompress.com/

Comments

0

It is a valid concern. Probably depends on how your Javascript files are built, and now the bundler works. A lot of time, the bundlers do a great job of figuring everything out and leaving things in tact that need to be there.

A lot of bundlers do have configuration options to enable or disable options so you maybe able to configure the system to the exact way you want it, or if you run into problems.

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.