ASP.net MVC bundle minficiation duplicate variable name conflict

by user2945722   Last Updated January 11, 2019 12:26 PM

I am receiving a JavaScript error due to the ASP.net bundle functionality, the JS code works fine before it gets minified but not after.

The issue Is have a isSelected variable that contains a boolean. But when the code gets minified it shortens this variable name to i. But in the $.each method I create an inline function with two parameters one being i.

It seems the minification logic failed to realize that renaming the variable to i would conflict with the code in my inline function.

Is this a bug with the ASP.net bundling system? If not how am I recommended to solve this, I am concerned that although I could just rename the parameter name that this bug could crop up in other parts of my web application.

Original JS code

function ItemManagerUpdateUi(instance) {
var selectedValue = instance.ItemCombo.GetValue();
var selectedItem = ItemManagerGetSelectedItem(instance);

var isSelected = selectedValue != null;

var isNewSelected = selectedValue == -1;

var applyText = isNewSelected ? "Add" : "Apply";
var text = isNewSelected ? "" : instance.ItemCombo.GetText();

var highestSortOrder = 0;
$.each(instance.Items, function (i, e) {
    if (e.SortOrder > highestSortOrder)
        highestSortOrder = e.SortOrder;
});

var sortOrder = isNewSelected || !isSelected ? highestSortOrder + 1 : selectedItem.SortOrder;

$('#' + instance.EditPanelId).toggle(isSelected);
$('#' + instance.ApplyButtonId).val(applyText);
$('#' + instance.DeleteButtonId).toggle(!isNewSelected); 

var labelWidth = $(instance.ItemCombo.GetMainElement()).closest('.rbox-clearfix').find('.labelStyle').width() + 15;

$('#' + instance.ButtonPanelId).css('margin-left', labelWidth + 'px');

instance.TextControl.SetText(text);
instance.SortOrderControl.SetNumber(sortOrder);

$.each(instance.CustomFields, function (i, e) {

    var value = isNewSelected || !isSelected ? null : selectedItem[e.DataName];

    var dxControl = eval(e.ControlName);

    dxControl.SetValue(value);
    dxControl.SetIsValid(true);
});

if (!isNewSelected)
    $(instance.ItemCombo.GetMainElement()).find('.dms-combo-main-input').removeClass('newActivityItem');

instance.TextControl.SetIsValid(true);
}

JS code after minification

function ItemManagerUpdateUi(n) {
var u = n.ItemCombo.GetValue(), 
f = ItemManagerGetSelectedItem(n), 
i = u != null, 
t = u == -1,
s = t ? "Add" : "Apply",
h = t ? "" : n.ItemCombo.GetText(),
r = 0,
e,
o;

$.each(n.Items, function(n, t) {
    t.SortOrder > r && (r = t.SortOrder)
});
e = t || !i ? r + 1 : f.SortOrder;
$("#" + n.EditPanelId).toggle(i);
$("#" + n.ApplyButtonId).val(s);
$("#" + n.DeleteButtonId).toggle(!t);
o = $(n.ItemCombo.GetMainElement()).closest(".rbox-clearfix").find(".labelStyle").width() + 15;
$("#" + n.ButtonPanelId).css("margin-left", o + "px");
n.TextControl.SetText(h);
n.SortOrderControl.SetNumber(e);
console.log(i);
$.each(n.CustomFields, function(i, e) {
    console.log(i);
    var value = t || !i ? null : f[e.DataName]
      , dxControl = eval(e.ControlName);
    dxControl.SetValue(value);
    dxControl.SetIsValid(!0)
});
t || $(n.ItemCombo.GetMainElement()).find(".dms-combo-main-input").removeClass("newActivityItem");
n.TextControl.SetIsValid(!0)
}


Related Questions



Minify multiple javascript files with webpack

Updated August 12, 2017 09:26 AM