3

If I take this code and compile it (advanced optimizations)

/**@constructor*/
function MyObject() {
    this.test = 4
    this.toString = function () {return 'test object'}
}
window['MyObject'] = MyObject

I get this code

window.MyObject=function(){this.test=4;this.toString=function(){return"test object"}};

Is there any way I can remove the toString function using the Closure Compiler?

0

3 Answers 3

4

toString is implicitly callable, so unless the Closure compiler can prove that the result of MyObject is never coerced to a string it has to preserve it.

You can always mark it as explicit debug code:

this.test = 4;
if (goog.DEBUG) {
  this.toString = function () { return "test object"; };
}

then in your non-debug build, compile with

goog.DEBUG = false;

See http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.source.html which does

/**
 * @define {boolean} DEBUG is provided as a convenience so that debugging code
 * that should not be included in a production js_binary can be easily stripped
 * by specifying --define goog.DEBUG=false to the JSCompiler. For example, most
 * toString() methods should be declared inside an "if (goog.DEBUG)" conditional
 * because they are generally used for debugging purposes and it is difficult
 * for the JSCompiler to statically determine whether they are used.
 */
goog.DEBUG = true;
3
  • I am trying to use this, but how do I specify this to the compiler in closure-compiler.appspot.com/home ? I tried setting goog.DEBUG-false; but it doesn't work... :(
    – edbras
    Commented Feb 22, 2011 at 22:15
  • @edbras, you can use the -D flag to closure compiler to set it. From the help: ` --define (--D, -D) VAL : Override the value of a variable annotated @define. The format is <name>[=<val>], where <name> is the name of a @define variable and <val> is a boolean,number, or a single-quoted string that contains no single quotes. If [=<val>] is omitted, the variable is marked true` Commented Feb 23, 2011 at 1:00
  • I have the same problem. Trying to make the compiler strip console.log calls with SIMPLE optimization. For me goog.DEBUG does not work in the online service. Commented Aug 21, 2012 at 16:15
0

The answer is surprising simple. I was researching this and didn't find the correct answer, so I am adding it here. The solution is to use a JSDoc annotation (see https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler#const-const-type):

/* @const */
const debug = false;

Now anywhere in your code (also inside of nested functions) you can do the following:

if (debug) console.log("hello world");

Or in your case invalidate a complete block

if (debug) {
    /* your code to remove */    
}

If you set debug to false the Closure compiler can remove it because it knows you declared debug a constant a hence it won't change and the code will never be executed if gated with your debug variable.

1
  • yes but you have to change manually the value of the variable everywhere. The correct answer was correct. The only problem is that -D option (@define) doesn't work in ES6 modules. I mean, cmon it's 2020 and Google cannot make define in modules. For different build targets, I've written a small script that would patch the value of the variable before proceeding to compilation, that's the only solution right now.
    – zavr
    Commented Sep 8, 2020 at 14:50
0

Because the @define doesn't work in modules, I wrote a patch that can be run before compilation. It goes:

import { c } from '@artdeco/erte'
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'

const [,,version] = process.argv

const PATH = join(__dirname, 'index.js')
let f = readFileSync(PATH, 'utf-8')

const isFree = version == '--free'

if (isFree) {
  f = f.replace("\nimport isFree from './paid'", "\n// import isFree from './paid'")
  f = f.replace("\n// import isFree from './free'", "\nimport isFree from './free'")
  console.log('Set version to %s', c('free', 'red'))
} else {
  f = f.replace("\n// import isFree from './paid'", "\nimport isFree from './paid'")
  f = f.replace("\nimport isFree from './free'", "\n// import isFree from './free'")
  console.log('Set version to %s', c('paid', 'magenta'))
}

writeFileSync(PATH, f)

Usage: node ./src/version/patch --free node ./src/version/patch --paid

And the actual ./src/version/index.js that's being patched:

// import isFree from './free'
import isFree from './paid'

With './free':

export default true

With './paid':

export default true

And based on that, you can export a variable from index.js:

export const free = isFree

So this was to allow compiling paid and free packages, but you could extend this code to adjust for debug/production version.

Still, this should be done with -D (@define) but apparently it's very difficult for a trillion-dollar company that Google is.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.