Pregunta

I am using a jQuery plugin and running it through the Microsoft Ajax Minifier. My scripts work well for me, but now I am running into an issue with this plugin. The issue is that the plugin calls a function by its name using a string:

var s = (getCachedSortType(table.config.parsers, c) == "text") ? ((order == 0) ? "sortText" : "sortTextDesc") : ((order == 0) ? "sortNumeric" : "sortNumericDesc");

Note the "sortNumeric" and "sortNumericDesc". This calls these functions:

function sortNumeric(a, b) {
            return a - b;
        }

function sortNumericDesc(a, b) {
            return b - a;
        }

This is the only location these functions are called so the IDE VS2010 doesn't think that the functions are being called from anywhere... They are conditionally via the code above.

**Here is the Problem**

When this gets minified, the string name of the function stays, but the function gets removed because it does not thing its getting referenced.

Is there any way to adjust the settings minifier to not do this?

I have also seen it change the names of the functions so

function testFunctionName(a,b)

Would become

function a

This would also cause a problem for situations like mine... Please note, that I know it is bad code design to hard code function names like this. Like I said it is a plug-in that I am using. I would accept a solution that would call the function out right instead of by string, but I am always hesitant to modify plug-ins.

¿Fue útil?

Solución

Use -unused:keep switch to retain unused functions. This, naturally, will prevent minifier from removing really unused code.

Use -rename switch to assign permanent names to functions that you call by name.

Otros consejos

From documentation:

-evals:(ignore|immediate|safeall) specifies how eval statements are to be treated. This is an important switch to be aware of. By default Ajax Minifier will ignore any eval statements, which can break your minified code if it contains eval statements that reference named local variables or functions. This is because by default Ajax Minifier will also rename local variables and function, but it doesn’t modify the text passed to the eval function, so those references may break. If you minify code and it stops working properly, check the code for eval statements. If there are any, try specifying one of the other two –evals switch options. The “immediate” options will not rename any variables or functions within the same scope as any eval call; the “safeall” option will not rename any variables or functions not only in the same scope as any eval call, but also in any parent scopes. This will seriously impair the minification of your code, but should ensure that any calls to the eval function will work as expected. The default setting is ignoreall.

Then try -evals:immediate and if your code is still broken you have to use -evals:safeall (even if this will make your JavaScript files bigger).

UPDATE
If you're not using eval then you have to skip function renaming at all:

-rename:(all|localization|none) specifies whether or not to automatically rename local variables and functions. Global variables and functions are not automatically renamed, nor are property names. If “localization” is specified, only variables that do not start with “L_” will be renamed. The default value is all.

Just add -rename:none.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top