rules_closure
rules_closure copied to clipboard
Property quoting compiler pass
I was reading what @WebReflection wrote in 5 Reasons To Avoid Closure Compiler In Advanced Mode back in 2013 and I struck by an idea that might be interesting.
What if ADVANCED
mode could be specified on a per-library basis?
That way you can link legacy code into your binary without having to turn off minification uber alles for the entire binary. Or using a separate script tag. This should be beneficial to open source users with heterogeneous codebases.
In order for this to work:
- The library rules would have to propagate the set of primitive source files to the binary rule.
- A compiler pass would have to be introduced that rewrites primitive source files to quote stuff.
For example, the following primitive file:
window.jeepers = 'creepers';
function madDogsAndEnglishmen() {
function goOutInTheMiddaySun() {
return 'there';
}
return {hi: goOutInTheMiddaySun()};
}
closure_js_library(
name = "primitive_lib",
srcs = ["primitive.js"],
advanced = False,
)
Would be rewritten to the following mid-compilation when making the closure_js_binary
:
goog.global['window']['jeepers'] = 'creepers';
goog.global['madDogsAndEnglishmen'] = function() {
function goOutInTheMiddaySun() {
return 'there';
}
return {'hi': goOutInTheMiddaySun()};
}
Has something like this been considered before @MatrixFrog? I poked through the jscomp codebase real quick and didn't see any passes that auto-quote properties.
CC: @hochhaus
For renaming in particular I think this would work but there are other optimizations that are part of advanced mode where trying to apply the optimization to just part of the code would be tricky. The Closure Compiler is designed to be a whole-program compiler, and there are lots of passes that assume that to be true. This is unlikely to change anytime soon I think.
I'd be interested to see how many advanced-incompatible libraries could be made advanced-mode compatible by doing something like this, and then be compiled in with the rest of a binary rather than having to be separately compiled through externs. Unfortunately, because of the inlining/dead code removal/other whole program optimizations., the restrictions on such code would necessarily have to be stricter than in SIMPLE mode.