How to prevent the Closure compiler from creating a specific variable name in the global namespace?

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

The 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 is reset when jQuery loads.

I am using a 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 output_wrapper used to solve this problem, but I either don’t understand its purpose or am using it incorrectly.

TIA.

+4
source share
1 answer

The answer is externs .

External characters define the characters in the external code. They have two main goals:

  • provides definitions and type information for external characters, so your code can call them without errors / warnings.
  • Prevent the compiler from using character names defined outside of your code.

In your case, you can include an existing extern for jQuery. For each major version, there is one of them in the project folder .

+3
source

Source: https://habr.com/ru/post/1446093/


All Articles