Vertispan / j2clmavenplugin

Maven plugin to launch new J2CL compilation
https://vertispan.github.io/j2clmavenplugin/
Apache License 2.0
53 stars 27 forks source link

DRAFT ServiceLoader implementation for J2CL+Closure #245

Open niloc132 opened 12 months ago

niloc132 commented 12 months ago

Adds a custom compiler pass that optimizes a particular pattern of JS code, with a simple integration test that confirms that the fully-optimized output contains only the expected implementation, and no strings remain from the unused implementation.

Before merging, the JS example should be rewritten to handmade java (plus required shim JS), to specify an expected pattern to follow. Should also consider a "service loader version" to ensure that any given implementation confirms to these expectations, should they need to evolve further.

By itself, this will not work with a multi-stage build - another custom pass could be required, or requiring the use of goog.LOCALE to drive the selected property.

Partial #244

niloc132 commented 12 months ago

At this point we have a working build for plain JS. For reference, here's the final output for the current sample, correctly picking just one implementation, removing the other, and inlining the results of the constant expressions:

(function(){globalThis.g=function(){};setTimeout(function(){return console.log("Hello, Java!")},100);}).call(this);

Note that globalThis.g=function(){} appears to be leftovers from a pass that builds a $J2CL_PRESERVE$ extern function - according to closure-compiler documentation this node should be removed, but clearly isn't. Definition in j2cl: https://github.com/Vertispan/j2cl/blob/aa5f8c2eec98b5314071f96572a9002599754279/jre/java/java/lang/jre.js#L55-L59

The next step for this PR is to establish a Java (with some shim JS, probably will be generated) pattern that still compiles out nicely.