minify
minify copied to clipboard
evaluate incorrectly evaluates variables to undefined
Describe the bug
When minifying angularjs I ran into an issue where valid variables were defined to undefined, and then void 0
To Reproduce
function ngOptionsPostLink(scope, selectElement, attr, ctrls)
{
// if ngModel is not defined, we don't need to do anything
var ngModelCtrl = ctrls[1];
if (!ngModelCtrl) return;
var selectCtrl = ctrls[0];
var multiple = attr.multiple;
// The emptyOption allows the application developer to provide their own custom "empty"
// option when the viewValue does not match any of the option values.
var emptyOption;
for (var i = 0, children = selectElement.children(), ii = children.length; i < ii; i++)
{
if (children[i].value === '')
{
emptyOption = children.eq(i);
break;
}
}
return emptyOption;
}
Actual Output
function ngOptionsPostLink(scope, selectElement, attr, ctrls)
{
var ngModelCtrl = ctrls[1];
if (ngModelCtrl)
for (var selectCtrl = ctrls[0], multiple = attr.multiple, i = 0, children = selectElement.children(), ii = (void 0).length; void 0 > i; i++)
if ('' === (void 0)[i].value)
{
(void 0).eq(i);
break
}
return emptyOption
}
Expected Output
function ngOptionsPostLink(scope, selectElement, attr, ctrls)
{
var ngModelCtrl = ctrls[1];
if (ngModelCtrl)
{
for (var emptyOption, selectCtrl = ctrls[0], multiple = attr.multiple, i = 0, children = selectElement.children(), ii = children.length; i < ii; i++)
if ('' === children[i].value)
{
emptyOption = children.eq(i);
break
}
return emptyOption
}
}
Configuration 7.0.0 (@babel/core 7.0.0) Babel cli and via grunt.
{
"presets": [
["minify", {
"builtIns": false,
"mangle": false,
"removeConsole": true,
"removeDebugger": true,
"evaluate": true
}]
]
}
I figured out that the error occures only when both plugins transform-merge-sibling-variables and minify-constant-folding are enabled.
Currently i use workaround that forces minify-constant-folding to run before transform-merge-sibling-variables:
{
"presets": [
["minify", {
"evaluate": false
}]
],
"plugins": [
["minify-constant-folding"]
]
}
However i'm not sure if the plugins used in minify preset could be rearranged in arbitrary order. In general case plugin order matters.