0

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.

6
  • 2
    Don'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. Commented Apr 14, 2012 at 4:51
  • 1
    Most minifiers let you specify which names you don't want changed Commented Apr 14, 2012 at 4:53
  • @mikeycgto: ould you give me a quick suggestion how to achive that? Commented Apr 14, 2012 at 4:53
  • markdalgleish.com/2011/03/self-executing-anonymous-functions Commented Apr 14, 2012 at 4:56
  • 2
    as 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. Commented Apr 14, 2012 at 4:57

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.