How to exclude object from renaming in short-compiler

I am using clos-util and would like one object not to be renamed to change content in html.
In the first step I want to compile my modules with openlayers 3 | 4 ..
I am not familiar with externs, export or api, so I need help declaring to close-compiler.

snippet.html (not compiled)

  olc.lon=7.11875;olc.lat=51.15345;olc.zoom=12;

      

The snippet declaration in main.js will compile

var olc = {      // namespace controls and constants
  lon         :  2.0,lat: 25.0,rota: 0,zoom: 2, // as default
  debug       :  'force'
};
window['olc'] = olc;

      

Now after compilation - olc.lon is renamed to olc.B,
- olc.lat is renamed to olc.uj,
- olc.rota is renamed to olc.mf,
- olc.zoom is not renamed, I don't know why not, and - olc. debug is not renamed.

Are there protected words like scaling?
How to protect olc.lon from renaming, for example?

+3


source to share


2 answers


Avoiding a closure type system

If you are using an object literal and do not want to add more type information, you can use 'quote'

property names. This results in their values ​​being used directly and not being used as part of the intended type (which can be optimized / replaced as you saw).

// namespace controls and constants
var olc = {
  'lon'  : 2.0,
  'lat'  : 25.0,
  'rota' : 0,
  'zoom' : 2, // as default
  'debug': 'force'
};

      

This optimization is done at the target / project level, not at every function, so the reasons why zoom

and debug

not being replaced may have to do with where / how they and other property names are used elsewhere in the program. You don't have to depend on it, it can change unpredictably. If you need zoom

to remain in writing, you must indicate / defend it as you would lat

and lon

.

Using the Closure Type System

A Closure system can be very useful, and avoiding it like this will prevent it from detecting some potential errors with these values. Instead, you can give the object / value a type in the Closure system that will check for errors, but don't know to rename.

There are unfortunately many ways to do this, many of which depend on subtle combinations of Closure Compiler settings. (The documentation may tell you to use @exports

, but even the ones that don't work in some cases.) Here's the solution I use the most because it works in most settings and is conceptually easy to understand: Define a frontend with properties that need to be stored. and apply it to your object.



You define this type of interface in an "externs file", which can be included in an assembly with a flag --externs

if you use the command line (see "Declaring externs" in the documentation ).

/externs.js

/** @interface */
function ControlsAndConstants() {}
/** @type {number} */
ControlsAndConstants.prototype.lat;
/** @type {number} */
ControlsAndConstants.prototype.lon;
/** @type {number} */
ControlsAndConstants.prototype.rota;
/** @type {number} */
ControlsAndConstants.prototype.zoom;
/** @type {*} */
ControlsAndConstants.prototype.debug;

      

/main.js

// namespace controls and constants
var olc = /** @type {ControlsAndConstants} */ ({
  lon  : 2.0,
  lat  : 25.0,
  rota : 0,
  zoom : 2, // as default
  debug: 'force'
});

window['olc'] = olc;

      

Note that property names are no longer quoted. We now want Closure to understand them rather than ignore them.

Why does it work? The externs file tells Closure "This interface ControlsAndConstants

already exists and is used by code that you are not compiling. Since you cannot optimize this code to rename properties, you need to use the same property names for compatibility." Simple enough!

+2


source


Yes there are protected words, including any existing globals (which means any property of an object window

) and JavaScript keywords and maybe more. Also everything that is included in the standard "external", which is included by default. I need to do more research on why zoom

and debug

in particular not renaming, but this is probably one of those categories.

There are several ways to prevent renaming in the closing compiler (and a search for that phrase will probably yield more advice). One quick way to do this is to put property names in quotes like this:

var olc = {
  'lon': 2.0,
  'lat': 25.0,
  'rota': 0,
  zoom: 2,
  debug:'force'
};
window['olc'] = olc;

      



See the online closure compiler example from the above code .

You can also define your own "externs" to prevent property renaming. See the Documentation about Advanced Compilations and External Versions .

+1


source







All Articles