Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Feature Request: Wrap generated script contents in an IIFE #352

Closed
mpetrovich opened this Issue Oct 30, 2012 · 3 comments

Comments

Projects
None yet
3 participants
Contributor

mpetrovich commented Oct 30, 2012

The script generated with rake release generates a script that, if used without modification, does not play well with JS minifiers/obfuscators like Uglify.

Example:

/*jshint eqnull:true*/
this.Handlebars = {};

When run through a minifier like Uglify, will become:

this.a = {};

As a result, later references to Handlebars as a global variable (vs. as a property on window) will throw reference errors.

A solution is to wrap the entire script contents in an IIFE:

(function(window, undefined) {
    window.Handlebars = {};
    //...
})(window);
Owner

wycats commented Oct 30, 2012

Frankly, this bug seems like a major bug in uglify. Can you point me to some rationale?

Contributor

mpetrovich commented Oct 30, 2012

You may be correct in that Uglify shouldn't be obfuscating properties on this if it's actually window, but wrapping the entire source insulates you from even having to worry about situations like this. What's the rationale for not wrapping everything in an IIFE? I'm wondering simply because this IIFE pattern is used by many other libraries (including jQuery).

Collaborator

kpdecker commented Feb 16, 2013

Ignoring the uglify behavior (I didn't think that it would attempt to mangle anything hanging off an object...) there are some size and scoping benefits to creating one all encompassing IIFE. I've made this change and it should land in the next release.

@kpdecker kpdecker closed this Feb 16, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment