0

正如标题所说,我想通过 Java API 以编程方式将定义传递给 Google Closure Compiler。

这是我当前的代码:

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());
}
4

1 回答 1

0

You want to populate the define replacements map.

options.getDefineReplacements().put("myDefineVarName", value);

The value has to be a Node that is a boolean, numeric, or string literal. To create a value for a boolean literal use a value like

new Node(Token.TRUE)

where both Node and Token are from the package com.google.javascript.jscomp.rhino.

I believe Token.STRING and Token.NUMBER are the token types for the other kinds of values but don't quote me on that.

于 2011-07-09T21:06:29.007 回答