Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

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
Closed

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

mpetrovich opened this issue Oct 30, 2012 · 3 comments

Comments

@mpetrovich
Copy link
Contributor

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);
@wycats
Copy link
Collaborator

wycats commented Oct 30, 2012

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

@mpetrovich
Copy link
Contributor Author

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).

@kpdecker
Copy link
Collaborator

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.

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

No branches or pull requests

3 participants