As the title says, I’d like to pass defines programmatically via the Java API to the Google Closure Compiler.
This is my current code:
com.google.javascript.jscomp.Compiler.setLoggingLevel(Level.INFO);
com.google.javascript.jscomp.Compiler compiler = new com.google.javascript.jscomp.Compiler();
CompilerOptions options = new CompilerOptions();
CompilationLevel.ADVANCED_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
List<JSSourceFile> externs = new ArrayList<JSSourceFile>();
externs.add(JSSourceFile.fromFile(extern_src));
List<JSSourceFile> primary = new ArrayList<JSSourceFile>();
primary.add(JSSourceFile.fromFile(tmp));
compiler.compile(externs, primary, options);
for (JSError message : compiler.getWarnings()) {
System.err.println("Warning message: " + message.toString());
}
for (JSError message : compiler.getErrors()) {
System.err.println("Error message: " + message.toString());
}
You want to populate the define replacements map.
The value has to be a
Nodethat is a boolean, numeric, or string literal.To create a value for a boolean literal use a value like
where both
NodeandTokenare from the packagecom.google.javascript.jscomp.rhino.I believe
Token.STRINGandToken.NUMBERare the token types for the other kinds of values but don’t quote me on that.