Last active
January 1, 2016 19:19
-
-
Save paulrobertlloyd/8189506 to your computer and use it in GitHub Desktop.
grunt-contrib-uglify is borking my JavaScript. What am I doing wrong?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Before grunt-contrib-uglify | |
*/ | |
(function(doc) { | |
if (document.querySelector) { | |
var toggleClassName = function(element, toggleClass) { | |
var reg = new RegExp('(\\s|^)' + toggleClass + '(\\s|$)'); | |
if (!element.className.match(reg)) { | |
element.className += ' ' + toggleClass; | |
} else { | |
element.className = element.className.replace(reg, ''); | |
} | |
}; | |
doc.querySelector('a[href="#navigation"]').addEventListener('click', function(e) { | |
e.preventDefault(); | |
toggleClassName(doc.body, 'js-menu-active'); | |
}); | |
} | |
}(this.document)); | |
/** | |
* After grunt-contrib-uglify (mangle:false, beautify:true) | |
*/ | |
!function(doc) { | |
if (document.querySelector) { | |
var toggleClassName = function(element, toggleClass) { | |
var reg = new RegExp("(\\s|^)" + toggleClass + "(\\s|$)"); | |
element.className.match(reg) ? element.className = element.className.replace(reg, "") : element.className += " " + toggleClass; | |
}; | |
doc.querySelector('a[href="#navigation"]').addEventListener("click", function(e) { | |
e.preventDefault(), toggleClassName(doc.body, "js-menu-active"); | |
}); | |
} | |
}(this.document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still debugging, but just to explain this line for you:
Firstly, let's break that down onto lines:
This is a ternary operator. It's basically a shorthand for a conditional. These two are equivalent:
Those two are the same. Uglify is rewriting the conditional just to save room (and hence bytes), I imagine. The only other thing it's doing is reversing the conditional. You have:
And it's doing this:
HTH.