How do I force the native version of google-closure-compiler to be used?

232 Views Asked by At

I can import the closure-compiler like this:

const ClosureCompiler = require('google-closure-compiler').compiler;

but that uses the java version. I have a limitation on the build machines at work such that I can't make use of java. I was hoping to force the compilation to use the native version. I've tried doing something like this:

const ClosureCompiler = require('google-closure-compiler-osx').compiler;

That seems to result in ClosureCompiler being undefined. I've gone around and around trying to find any documentation on the API exposed to javascript but I keep coming up with nothing.

If anyone has an idea about how to force native compilation rather than java compilation it would be much appreciated.

1

There are 1 best solutions below

0
Maynard On

I think I've made some progress on this by looking at what's going on in the cli.js and util.js files inside node_modules/google-closer-compiler.

This pattern appears to be working:

const ClosureCompiler = require('google-closure-compiler').compiler,
    compilerInstance = new ClosureCompiler({... setup opts ...});

// Force native compilation
let compilerPlatform = 'linux';
switch (process.platform) {
    case 'win32': compilerPlatform = 'windows'; break;
    case 'darwin': compilerPlatform = 'osx'; break;
}
compilerInstance.JAR_PATH = null;
compilerInstance.javaPath = require('google-closure-compiler-' + compilerPlatform);

compilerInstance.run((exitCode, stdOut, stdErr) => {
    ... do some stuff ...
});