1

An if statement that is obviously always false, eg. if (false), closure removes the statement.

My code looks like this:

if (settings.lang === "en"){
    lib.doSomething();
}

settings.lang is a constant.

/** 
 * @type {string}
 * @const 
 */ 
settings.lang = "fr" ;  

So when it equals "fr" the compiler could remove the if and the definition of lib.doSomething at compile time. But it doesn't. Is there any way to get it to do that?

Before you ask why I don't just delete that code: for other clients, settings.lang is set to en.

ColBeseder
  • 3,579
  • 3
  • 28
  • 45
  • 1
    what is your compilation level? have you tried passing it as a @define? – lennel Jun 17 '13 at 17:50
  • And you may have to show more of your source code. A quick check at closure-compiler.appspot.com shows that the optimization you are after works as expected. – Chad Killingsworth Jun 17 '13 at 20:27
  • I thought define might be the answer. I define goog.DEBUG, but goog.LANG throws a plovr warning. I can;t find documentation on using @define for custom properties. – ColBeseder Jun 18 '13 at 07:01
  • https://developers.google.com/closure/compiler/docs/js-for-compiler @define instead of @constant. You give it a value in code and can override it in the compiler with `--define='YOUR_DEFINE=false'` What kind of warning does plovr give you? – HMR Jun 18 '13 at 08:52

3 Answers3

2

1) Make sure "settings" is properly defined:

/** @const */
var settings = {};

2) Make sure "settings" lang is properly defined:

/** @const */ 
settings.lang = "fr" ;

3) Make sure the value is referenced after it is defined:

if (settings.lang == "en") ...

In advanced mode this will be inlined and removed, if settings is not used in a way that prevents property collapsing (for instance, passing "settings" as a parameter to a function will cause the value to escape and be uncollapsible).

This is simplified, if you use @define:

/** @const */
var settings = {};

/** @define {string} */
settings.lang = "fr";

You should get a warning if the definition of the define is not valid in some way.

John
  • 5,443
  • 15
  • 21
1

It sounds like you're using some compilation mode (most likely the 'SIMPLE' compilation mode, as that is the default in plovr) other than the 'ADVANCED' compilation mode. The advanced compilation mode is the one that does dead code removal.

Technetium
  • 5,902
  • 2
  • 43
  • 54
1

Is goog.LANG defined with goog.define? Last I checked Plovr was not compatible with goog.define (it doesn't have the latest compiler) make sure you are starting with a compatible version of the Closure Library.

John
  • 5,443
  • 15
  • 21