本文整理了Java中com.google.javascript.jscomp.Compiler.compile()
方法的一些代码示例,展示了Compiler.compile()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Compiler.compile()
方法的具体详情如下:
包路径:com.google.javascript.jscomp.Compiler
类名称:Compiler
方法名:compile
[英]Compiles a single source file and a single externs file.
[中]编译单个源文件和单个外部文件。
代码示例来源:origin: BroadleafCommerce/BroadleafCommerce
protected String compileJs(SourceFile input, String filename) throws ResourceMinificationException, IOException {
Compiler compiler = getCompiler();
List<SourceFile> builtinExterns = AbstractCommandLineRunner.getBuiltinExterns(CompilerOptions.Environment.CUSTOM);
Result result = compiler.compile(builtinExterns, Arrays.asList(input), getCompilerOptions());
String compiled = compiler.toSource();
if (!result.success || StringUtils.isBlank(compiled)) {
StringBuilder errorString = new StringBuilder("\n");
if (result.errors != null) {
for (int i = 0; i < result.errors.length; i++) {
errorString.append(result.errors[i].description + "\n");
}
}
throw new ResourceMinificationException("Error minifying js file " + filename + errorString);
}
return compiler.toSource();
}
代码示例来源:origin: jooby-project/jooby
@Override
public String process(final String filename, final String source, final Config conf,
final ClassLoader loader) throws Exception {
final CompilerOptions copts = new CompilerOptions();
copts.setCodingConvention(new ClosureCodingConvention());
copts.setOutputCharset(StandardCharsets.UTF_8);
copts.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING);
CompilationLevel level = level(get("level"));
level.setOptionsForCompilationLevel(copts);
Compiler.setLoggingLevel(Level.SEVERE);
Compiler compiler = new Compiler();
compiler.disableThreads();
compiler.initOptions(copts);
List<SourceFile> externs = externs(copts);
Result result = compiler.compile(externs,
ImmutableList.of(SourceFile.fromCode(filename, source)), copts);
if (result.success) {
return compiler.toSource();
}
List<AssetProblem> errors = Arrays.stream(result.errors)
.map(error -> new AssetProblem(error.sourceName, error.lineNumber, error.getCharno(),
error.description, null))
.collect(Collectors.toList());
throw new AssetException(name(), errors);
}
代码示例来源:origin: com.google.javascript/closure-compiler
/** Compiles a single source file and a single externs file. */
public Result compile(SourceFile extern, SourceFile input, CompilerOptions options) {
return compile(ImmutableList.of(extern), ImmutableList.of(input), options);
}
代码示例来源:origin: org.scala-js/closure-compiler-java-6
public Result compile(
SourceFile extern, SourceFile input, CompilerOptions options) {
return compile(ImmutableList.of(extern), ImmutableList.of(input), options);
}
代码示例来源:origin: org.scala-js/closure-compiler-java-6
private Compiler createCompiler(
List<SourceFile> inputs, List<SourceFile> externs, CompilerOptions compilerOptions) {
Compiler compiler = new Compiler();
compiler.disableThreads();
compiler.compile(externs, inputs, compilerOptions);
return compiler;
}
代码示例来源:origin: com.google.javascript/closure-compiler
public String runtime(String library) {
Compiler compiler = compiler();
CompilerOptions options = options();
options.setForceLibraryInjection(ImmutableList.of(library));
compiler.compile(EXTERNS, EMPTY, options);
return compiler.toSource();
}
代码示例来源:origin: com.google.javascript/closure-compiler
public String runtime(String library) {
Compiler compiler = compiler();
CompilerOptions options = options();
options.setForceLibraryInjection(ImmutableList.of(library));
compiler.compile(EXTERNS, EMPTY, options);
return compiler.toSource();
}
代码示例来源:origin: com.google.javascript/closure-compiler
public CompileResult compile(Path path, String code) {
Compiler compiler = compiler();
Result result =
compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options());
String source = compiler.toSource();
StringBuilder sourceMap = new StringBuilder();
if (result.sourceMap != null) {
try {
result.sourceMap.appendTo(sourceMap, path.toString());
} catch (IOException e) {
// impossible, and not a big deal even if it did happen.
}
}
boolean transformed = transformed(result);
return new CompileResult(
source, result.errors, transformed, transformed ? sourceMap.toString() : "");
}
代码示例来源:origin: org.pustefixframework/pustefix-core
@Override
public void compress(Reader reader, Writer writer) throws CompressorException {
com.google.javascript.jscomp.Compiler compiler = new com.google.javascript.jscomp.Compiler();
compiler.disableThreads();
CompilerOptions options = new CompilerOptions();
CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
try {
SourceFile inputFile = SourceFile.fromReader("input.js", reader);
List<SourceFile> inputFiles = new ArrayList<>();
inputFiles.add(inputFile);
List<SourceFile> externFiles = new ArrayList<>();
compiler.compile(externFiles, inputFiles, options);
writer.write(compiler.toSource());
} catch(IOException x) {
throw new CompressorException("Error while compressing javascript", x);
}
}
代码示例来源:origin: com.google.javascript/closure-compiler
public CompileResult compile(URI path, String code) {
Compiler compiler = compiler();
Result result =
compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options());
String source = compiler.toSource();
StringBuilder sourceMap = new StringBuilder();
if (result.sourceMap != null) {
try {
result.sourceMap.appendTo(sourceMap, path.toString());
} catch (IOException e) {
// impossible, and not a big deal even if it did happen.
}
}
boolean transpiled = !result.transpiledFiles.isEmpty();
if (result.errors.length > 0) {
throw new TranspilationException(compiler, result.errors, result.warnings);
}
return new CompileResult(
source,
transpiled,
transpiled ? sourceMap.toString() : "");
}
代码示例来源:origin: org.scala-js/closure-compiler-java-6
/** Generates the runtime by requesting the "es6_runtime" library from the compiler. */
private static String getEs6Runtime() {
CompilerOptions options = getOptions();
options.setLanguageOut(LanguageMode.ECMASCRIPT3); // change .delete to ['delete']
options.setForceLibraryInjection(ImmutableList.of("es6_runtime"));
Compiler compiler = new Compiler();
// Threads can't be used in small unit tests.
compiler.disableThreads();
SourceFile externs = SourceFile.fromCode("externs", "function Symbol() {}");
SourceFile sourceFile = SourceFile.fromCode("source", "");
compiler.compile(ImmutableList.of(externs), ImmutableList.of(sourceFile), options);
return compiler.toSource();
}
}
代码示例来源:origin: bessemHmidi/AngularBeans
public String compile(String code) {
Compiler compiler = new Compiler();
compiler.disableThreads();
SourceFile extern = SourceFile.fromCode("externs.js","function alert(x) {}");
SourceFile input = SourceFile.fromCode("input.js", code);
compiler.compile(extern, input, options);
return compiler.toSource();
}
}
代码示例来源:origin: com.atlassian.maven.plugins/maven-amps-plugin
public static String compile(String code) {
Compiler compiler = new Compiler();
CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
JSSourceFile extern = JSSourceFile.fromCode("externs.js","function alert(x) {}");
// The dummy input name "input.js" is used here so that any warnings or
// errors will cite line numbers in terms of input.js.
JSSourceFile input = JSSourceFile.fromCode("input.js", code);
// compile() returns a Result, but it is not needed here.
compiler.compile(extern, input, options);
// The compiler is responsible for generating the compiled code; it is not
// accessible via the Result.
return compiler.toSource();
}
代码示例来源:origin: org.wso2.org.apache.shindig/shindig-gadgets
private Compiler mockRealJsCompiler(JSError error, Result res, String toSource) {
Compiler result = createMock(Compiler.class);
expect(result.compile(EasyMock.<List<JSSourceFile>>anyObject(),
EasyMock.<List<JSSourceFile>>anyObject(),
isA(CompilerOptions.class))).andReturn(res);
if (error != null) {
expect(result.hasErrors()).andReturn(true);
expect(result.getErrors()).andReturn(new JSError[] { error });
} else {
expect(result.hasErrors()).andReturn(false);
}
expect(result.getResult()).andReturn(res);
expect(result.toSource()).andReturn(toSource);
replay(result);
return result;
}
代码示例来源:origin: org.scala-js/closure-compiler-java-6
/**
* Compiles a list of inputs.
*/
public <T1 extends SourceFile, T2 extends SourceFile> Result compile(
List<T1> externs, List<T2> inputs, CompilerOptions options) {
// The compile method should only be called once.
Preconditions.checkState(jsRoot == null);
try {
init(externs, inputs, options);
if (hasErrors()) {
return getResult();
}
return compile();
} finally {
Tracer t = newTracer("generateReport");
errorManager.generateReport();
stopTracer(t, "generateReport");
}
}
代码示例来源:origin: org.scala-js/closure-compiler-java-6
/**
* Compiles a list of modules.
*/
public <T extends SourceFile> Result compileModules(List<T> externs,
List<JSModule> modules, CompilerOptions options) {
// The compile method should only be called once.
Preconditions.checkState(jsRoot == null);
try {
initModules(externs, modules, options);
if (hasErrors()) {
return getResult();
}
return compile();
} finally {
Tracer t = newTracer("generateReport");
errorManager.generateReport();
stopTracer(t, "generateReport");
}
}
代码示例来源:origin: org.apache.shindig/shindig-gadgets
private Compiler mockRealJsCompiler(JSError error, Result res, String toSource) {
Compiler result = createMock(Compiler.class);
expect(result.compile(EasyMock.<List<JSSourceFile>>anyObject(),
EasyMock.<List<JSSourceFile>>anyObject(),
isA(CompilerOptions.class))).andReturn(res);
if (error != null) {
expect(result.hasErrors()).andReturn(true);
expect(result.getErrors()).andReturn(new JSError[] { error });
} else {
expect(result.hasErrors()).andReturn(false);
}
expect(result.getResult()).andReturn(res);
expect(result.toSource()).andReturn(toSource);
replay(result);
return result;
}
代码示例来源:origin: org.apache.shindig/shindig-gadgets
protected CompileResult doCompileContent(JsContent content, CompilerOptions options,
List<SourceFile> externs) throws CompilerException {
Compiler compiler = new Compiler(getErrorManager()); // We shouldn't reuse compilers
// disable JS Closure Compiler internal thread
compiler.disableThreads();
SourceFile source = SourceFile.fromCode(content.getSource(), content.get());
Result result = compiler.compile(externs, Lists.newArrayList(source), options);
if (result.errors.length > 0) {
throw new CompilerException(result.errors);
}
return new CompileResult(compiler, result);
}
代码示例来源:origin: com.github.jknack/amd4j-closure
@Override
public CharSequence minify(final Config config, final CharSequence source) {
final CompilerOptions options = new CompilerOptions();
options.setCodingConvention(new ClosureCodingConvention());
options.setOutputCharset("UTF-8");
options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING);
compilationLevel.setOptionsForCompilationLevel(options);
Compiler.setLoggingLevel(Level.SEVERE);
Compiler compiler = new Compiler();
compiler.disableThreads();
compiler.initOptions(options);
String fname = removeExtension(config.getName()) + ".js";
Result result = compiler.compile(defaultExterns,
Arrays.asList(SourceFile.fromCode(fname, source.toString())), options);
if (result.success) {
return compiler.toSource();
}
JSError[] errors = result.errors;
throw new IllegalStateException(errors[0].toString());
}
代码示例来源:origin: de.agilecoders.wicket/bootstrap-extensions
@Override
public String compress(final String original) {
final Compiler compiler = new Compiler();
final CompilerOptions options = new CompilerOptions();
// Advanced mode is used here, but additional options could be set, too.
level.setOptionsForCompilationLevel(options);
// To get the complete set of externs, the logic in
// CompilerRunner.getDefaultExterns() should be used here.
final SourceFile extern = SourceFile.fromCode("externs.js", "function alert(x) {}");
// The dummy input name "input.js" is used here so that any warnings or
// errors will cite line numbers in terms of input.js.
final SourceFile input = SourceFile.fromCode("input.js", original);
// compile() returns a Result, but it is not needed here.
compiler.compile(extern, input, options);
// The compiler is responsible for generating the compiled code; it is not
// accessible via the Result.
return compiler.toSource();
}
}
内容来源于网络,如有侵权,请联系作者删除!