Is there any javascript minifier out there (online one) which allows defining a namespace? I mean all these minifiers generates code with short variable names like a,b,c ... which may cause conflicts with other minified javascript.
-
2Don't write your codes in the global scope. It's like a public restroom, you want minimize your exposure. Wrap you code in a self-executing anonymous function.mjc– mjc2012-04-14 04:51:16 +00:00Commented Apr 14, 2012 at 4:51
-
1Most minifiers let you specify which names you don't want changedRuan Mendes– Ruan Mendes2012-04-14 04:53:06 +00:00Commented Apr 14, 2012 at 4:53
-
@mikeycgto: ould you give me a quick suggestion how to achive that?SteMa– SteMa2012-04-14 04:53:28 +00:00Commented Apr 14, 2012 at 4:53
-
markdalgleish.com/2011/03/self-executing-anonymous-functionsmjc– mjc2012-04-14 04:56:08 +00:00Commented Apr 14, 2012 at 4:56
-
2as far as i know, closure compiler needs all scripts that are inter-related to be present for them to be properly minified. otherwise, if you just had half your code minified with closure and the other half depended on it was not minified with it, it will not work. you should minify only for production, and that means all scripts.Joseph– Joseph2012-04-14 04:57:48 +00:00Commented Apr 14, 2012 at 4:57
|
Show 1 more comment
1 Answer
Most (good) minifiers leave globally scoped variables alone since those are the namespace we're in by default. mikeycgto was suggesting that you make sure you keep those down to a minimum:
var page = ( function(){
var scopedVar = "I'm something like private.";
//do some other stuff
return {
usefulThing: function(){
return scopedVar;
}
};
}() );
Running that through a minifier should leave you with a "page" var in the global scope. page.usefulThing is a method (which should also be left alone by the minifier). "scopedVar" may be turned into "a" or "o" or somesuch, but you'll never care. Your API will remain as expected though the internals will be mucked about with.