Karl
Karl

Reputation: 1854

How to prevent Closure Compiler from creating a particular variable name in the global name space?

I'm familiar with the concept of using string literals and exporting to prevent Closure from renaming variables, but how do I prevent Closure from using a variable name that is used as a global variable by other code (which I did not write)?

Example, below was created for a member function of a closure:

  function $() {
  var a;
  if(1 > N) {
        return-1
  }
  a = Math.pow(1 + Q, F);
  return .....
  }

Above gets clobbered when jQuery is loaded.

I'm using the command line compiler and this is my command line:

java -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --formatting=pretty_print --output_wrapper PGS --js common.v2.0.raw.js --module common_min:1 --js page_code.raw.js --module page_code_min:1:common_min

I thought the output_wrapper option is used to solve this problem, but either I'm misunderstanding its purpose or I'm misusing it.

TIA.

Upvotes: 4

Views: 1878

Answers (1)

Chad Killingsworth
Chad Killingsworth

Reputation: 14411

The answer is externs.

Externs define symbols in external code. They have two main purposes:

  • provide definitions and type information for external symbols so your code can call them without errors/warnings.
  • prevent the compiler from using symbol names defined outside of your code

In your case you can include an existing extern for jQuery. There is one for each major version in the project contrib folder.

Upvotes: 3

Related Questions