WHITESPACE_ONLY needs COMPILED=true

511 views
Skip to first unread message

Erik Neumann

unread,
May 1, 2014, 6:49:05 PM5/1/14
to closure-comp...@googlegroups.com
I'm using WHITESPACE_ONLY compilation mode;  it works just fine provided I manually change the first line of the resulting javascript file to "var COMPILED=true;"  instead of "var COMPILED=false;".

If I leave it with COMPILED=false, then the following error occurs where it seems to be trying to load a non-existent deps.js file
. GET file:///Users/erikn/Documents/Programming/jssimlab/build/sims/experimental/deps.js net::ERR_FILE_NOT_FOUND CurvedTestApp_en.js:12
. goog.writeScriptTag_ CurvedTestApp_en.js:12
. goog.importScript_ CurvedTestApp_en.js:11
. (anonymous function) CurvedTestApp_en.js:14

Here is Line 14 of CurvedTestApp_en.js:
else return null};goog.findBasePath_();if(!goog.global.CLOSURE_NO_DEPS)goog.importScript_(goog.basePath+"deps.js")}

The Bolin "Closure The Definitive Guide" book states about the COMPILED constant:  "The value of this variable should never be set directly—it defaults to false and will be redefined at compile time by the Compiler, if appropriate."

So, I'm wondering what to do here:

1. Create a script as part of my build process to replace "COMPILED=false" with "COMPILED=true" in the compiled javascript.  But are there other bad consequences of this?

2. Perhaps there is a compiler flag to set the COMPILED constant?  (I tried using "--define=COMPILED=true" but this didn't work).

3.  I tried adding to the compile command:
--compiler_flags="--define=goog.global.CLOSURE_NO_DEPS=true" \
but this seems to have no effect.

4.  ???

FYI, I'm using WHITESPACE_ONLY mode because I'm making a command line interface available to my system, so I need unmangled names to survive compilation.  

I might be able to use SIMPLE_OPTIMIZATIONS if I can figure out how to trick the compiler to not minify some local variables inside of the function where commands are evaluated.  For example, in a graphing calculator application I need the local variable "x" to remain "x", so that  eval("Math.sin(x)") can succeed.  (The user would have typed in "Math.sin(x)" to the command line text field).  I can make "x" into a global variable to prevent renaming, but that's not very safe -- I don't really want to add "x" as a global.

Is anyone else using WHITESPACE_ONLY compilation mode and actually running the resulting code?

Thanks for any ideas on these issues.
--ErikN

Dimitris Vardoulakis

unread,
May 1, 2014, 7:18:17 PM5/1/14
to closure-comp...@googlegroups.com
On Thu, May 1, 2014 at 3:49 PM, Erik Neumann <er...@myphysicslab.com> wrote:
I'm using WHITESPACE_ONLY compilation mode;  it works just fine provided I manually change the first line of the resulting javascript file to "var COMPILED=true;"  instead of "var COMPILED=false;".

If I leave it with COMPILED=false, then the following error occurs where it seems to be trying to load a non-existent deps.js file
. GET file:///Users/erikn/Documents/Programming/jssimlab/build/sims/experimental/deps.js net::ERR_FILE_NOT_FOUND CurvedTestApp_en.js:12
. goog.writeScriptTag_ CurvedTestApp_en.js:12
. goog.importScript_ CurvedTestApp_en.js:11
. (anonymous function) CurvedTestApp_en.js:14

Here is Line 14 of CurvedTestApp_en.js:
else return null};goog.findBasePath_();if(!goog.global.CLOSURE_NO_DEPS)goog.importScript_(goog.basePath+"deps.js")}

The Bolin "Closure The Definitive Guide" book states about the COMPILED constant:  "The value of this variable should never be set directly—it defaults to false and will be redefined at compile time by the Compiler, if appropriate."

So, I'm wondering what to do here:

1. Create a script as part of my build process to replace "COMPILED=false" with "COMPILED=true" in the compiled javascript.  But are there other bad consequences of this?

2. Perhaps there is a compiler flag to set the COMPILED constant?  (I tried using "--define=COMPILED=true" but this didn't work).


(I didn't look at why the file-not-found error is happening.)

I think the syntax for the define flags is: --define='COMPILED=true'

 

3.  I tried adding to the compile command:
--compiler_flags="--define=goog.global.CLOSURE_NO_DEPS=true" \
but this seems to have no effect.

4.  ???

FYI, I'm using WHITESPACE_ONLY mode because I'm making a command line interface available to my system, so I need unmangled names to survive compilation.  

I might be able to use SIMPLE_OPTIMIZATIONS if I can figure out how to trick the compiler to not minify some local variables inside of the function where commands are evaluated.  For example, in a graphing calculator application I need the local variable "x" to remain "x", so that  eval("Math.sin(x)") can succeed.  (The user would have typed in "Math.sin(x)" to the command line text field).  I can make "x" into a global variable to prevent renaming, but that's not very safe -- I don't really want to add "x" as a global.

Is anyone else using WHITESPACE_ONLY compilation mode and actually running the resulting code?

Thanks for any ideas on these issues.
--ErikN

--

---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-d...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Dimitris

John Lenz

unread,
May 1, 2014, 7:22:39 PM5/1/14
to closure-compiler

If you look in base.js the library looks to see if another global value is set to determine if it should try to load sources.

--

Erik Neumann

unread,
May 1, 2014, 9:32:04 PM5/1/14
to closure-comp...@googlegroups.com
I propose that COMPILED should be true after compiling with WHITESPACE_ONLY.  Tell me if the following is wrong (this is from looking in base.js and also from the Bolin book):

While compiling with WHITESPACE_ONLY, the compiler reads in all the code and all the dependencies, then it writes out the code -- but only what's needed by the file being compiled, and in correct dependency ordering (and of course minus whitespace and comments).

The main effect of the COMPILED flag is to do with loading dependencies via goog.requires and goog.provides.  This is not needed for WHITESPACE_ONLY compiled code, because such code has all it's dependencies already in the single compiled file, and all in the correct dependency ordering.

Therefore, having COMPILED=false is incorrect for WHITESPACE_ONLY compiled code.  It causes the whole dependency loading scheme to look for source files that don't exist.

--ErikN

Nick Santos

unread,
May 1, 2014, 9:37:27 PM5/1/14
to closure-compiler
by definition, WHITESPACE_ONLY cannot make any code changes.

can you explain why the CLOSURE_NO_DEPS hook does not work for you?
it's designed to address the exact use case you're describing (see the
comment at the top of base.js about this)

Erik Neumann

unread,
May 2, 2014, 2:27:09 AM5/2/14
to closure-comp...@googlegroups.com
Hi Nick,
I'm not saying the compiler is making code changes (beyond the whitespace and comment deletion), but it is definitely rearranging the code from multiple files into a single file, in a particular order.  Which I think means that all the dependency stuff is not needed -- the code has been collated by the compiler into the correct order by using the various goog.require statements and such.  Which means the COMPILED flag should be true.

Also of interest: about 40% of the compiled file consists of goog.addDependency statements.  I deleted all of these and my file size went from 1.2 MB to 677 KB, and it still runs perfectly well as long as COMPILED = true.

Regarding the CLOSURE_NO_DEPS hook:

I might not be defining CLOSURE_NO_DEPS correctly, but AFAICT it isn't working.  The symptom is that the program just doesn't run, but no error messages appear in the Console (using Chrome browser).  But after changing COMPILED to true, the program runs perfectly.  

I can try to chase down further what is going wrong with setting CLOSURE_NO_DEPS to true with COMPILED set to false, if that is of interest.  (I can try to figure out why nothing happens, but also no error messages).

Here are 3 attempts to use CLOSURE_NO_DEPS:


Attempt 1.  
Manually add this at top of the WHITESPACE_ONLY compiled .js file:

var CLOSURE_NO_DEPS=true;

but leave COMPILED=false
Result is that nothing happens.  No error message, but the program doesn't run.

To confirm that CLOSURE_NO_DEPS is being set to true, I added code to print the value of goog.global.CLOSURE_NO_DEPS right before it is used, and it is indeed set to "true" at that point.
Here is the code:
  // Allow projects to manage the deps files themselves.
  window.console.log("at goog.DEPENDENCIES_ENABLED: goog.global.CLOSURE_NO_DEPS="+goog.global.CLOSURE_NO_DEPS);
  if (!goog.global.CLOSURE_NO_DEPS) {
    goog.importScript_(goog.basePath + 'deps.js');
  }

(Again, changing to COMPILED=true; and it works perfectly.)


Attempt 2. 
next, I try adding the "--define" option to my compile statement:

--compiler_flags="--define='goog.global.CLOSURE_NO_DEPS=true'" \

Note that I successfully use this compiler feature all the time, here are the neighboring lines that I know work in my shell script:
--compiler_flags="--define=goog.DEBUG=$debug" \
--compiler_flags="--define=goog.LOCALE='$locale'" \

Looking in the resulting compiled code, there is no setting of CLOSURE_NO_DEPS appearing in the compiled js file, and when I run the program I see that CLOSURE_NO_DEPS is undefined and it is trying to import deps.js:

at goog.DEPENDENCIES_ENABLED: goog.global.CLOSURE_NO_DEPS=undefined CurvedTestApp_en.js:14
GET file:///Users/erikn/Documents/Programming/jssimlab/build/sims/experimental/deps.js net::ERR_FILE_NOT_FOUND CurvedTestApp_en.js:12
goog.writeScriptTag_ CurvedTestApp_en.js:12
goog.importScript_ CurvedTestApp_en.js:11
(anonymous function) CurvedTestApp_en.js:14
 

Attempt 3. 
I see there is also a mechanism in goog.define that looks in CLOSURE_UNCOMPILED_DEFINES or CLOSURE_DEFINES. So I manually add to the top of my WHITESPACE_ONLY compiled js file:

var CLOSURE_UNCOMPILED_DEFINES = {'CLOSURE_NO_DEPS': true};
window.console.log("test: "+CLOSURE_UNCOMPILED_DEFINES['CLOSURE_NO_DEPS']);

When I run the program I see "test: true", but then the same previous result where goog.global.CLOSURE_NO_DEPS=undefined

I also tried the above with:
var CLOSURE_UNCOMPILED_DEFINES = {'goog.global.CLOSURE_NO_DEPS': true};
but get the same result.


Conclusion: setting CLOSURE_NO_DEPS=true does not work in this situation.  Whereas setting COMPILED=true works like a charm.

--ErikN
Reply all
Reply to author
Forward
0 new messages