Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default define of goog.DEBUG=false for optimized builds #247

Open
niloc132 opened this issue Nov 27, 2023 · 0 comments
Open

Add default define of goog.DEBUG=false for optimized builds #247

niloc132 opened this issue Nov 27, 2023 · 0 comments

Comments

@niloc132
Copy link
Member

Code compiled by j2cl-maven-plugin gets an extra method defined on globalThis, even when everything else is compiled out:

globalThis.g=function(){};

J2CL's jre.js sets up a special function to prevent certain optimizations: https://github.com/Vertispan/j2cl/blob/aa5f8c2eec98b5314071f96572a9002599754279/jre/java/java/lang/jre.js#L55-L59

// Provide a stub for field preserver to make things work without full
// compilation.
if (!COMPILED || goog.DEBUG) {
  globalThis.$J2CL_PRESERVE$ = function(/** ...* */ e) {};
}

See also https://github.com/Vertispan/j2cl/blob/aa5f8c2eec98b5314071f96572a9002599754279/jre/java/java/lang/externs.js#L22-L25 for the externs that define this method as being external.

Closure-compiler specially looks for this value, guards it to ensure only j2cl code can call it:
https://github.com/Vertispan/closure-compiler/blob/ebf577bd36a6f24a27b71e4498371066a831270d/src/com/google/javascript/jscomp/CheckSideEffects.java#L53-L56

In j2cl-maven-plugin, this code is eventually compiled to

if(!COMPILED||goog.DEBUG)globalThis.$J2CL_PRESERVE$=function(e$jscomp$12){};

Then, defines are inlined:

if(!true||true)globalThis.$J2CL_PRESERVE$=function(e$jscomp$12){};

Which will clearly be kept, though it shouldn't be, according to the comment.

To ensure this is removed, we want goog.DEBUG to be set to false. This is probably a good idea generally, but it might also be possible to remove the goog.DEBUG check and just used COMPILED.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant