com.google.javascript.jscomp.Compiler.toSource()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 JavaScript  
字(11.2k)|赞(0)|评价(0)|浏览(190)

本文整理了Java中com.google.javascript.jscomp.Compiler.toSource()方法的一些代码示例,展示了Compiler.toSource()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Compiler.toSource()方法的具体详情如下:
包路径:com.google.javascript.jscomp.Compiler
类名称:Compiler
方法名:toSource

Compiler.toSource介绍

[英]Converts the main parse tree back to JS code.
[中]将主解析树转换回JS代码。

代码示例

代码示例来源: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: org.apache.shindig/shindig-gadgets

public CompileResult(Compiler compiler, Result result) {
 content = compiler.toSource();
 externExport = result.externExport;
}

代码示例来源:origin: org.scala-js/closure-compiler-java-6

@Override
final void afterPass(String passName) {
 if (options.printSourceAfterEachPass) {
  String currentJsSource = toSource();
  if (!currentJsSource.equals(this.lastJsSource)) {
   System.out.println();
   System.out.println("// " + passName + " yields:");
   System.out.println("// ************************************");
   System.out.println(currentJsSource);
   lastJsSource = currentJsSource;
  }
 }
}

代码示例来源:origin: com.google.javascript/closure-compiler

/**
 * Generates JavaScript source code for an AST, doesn't generate source
 * map info.
 */
@Override
public String toSource(Node n) {
 initCompilerOptionsIfTesting();
 return toSource(n, null, true);
}

代码示例来源:origin: org.scala-js/closure-compiler-java-6

/**
 * Generates JavaScript source code for an AST, doesn't generate source
 * map info.
 */
@Override
public String toSource(Node n) {
 initCompilerOptionsIfTesting();
 return toSource(n, null, true);
}

代码示例来源:origin: org.scala-js/closure-compiler-java-6

@VisibleForTesting
void writeModuleOutput(Appendable out, JSModule m) throws IOException {
 if (parsedModuleWrappers == null) {
  parsedModuleWrappers = parseModuleWrappers(
    config.moduleWrapper,
    ImmutableList.copyOf(
      compiler.getDegenerateModuleGraph().getAllModules()));
 }
 String fileName = getModuleOutputFileName(m);
 String baseName = new File(fileName).getName();
 writeOutput(out, compiler, compiler.toSource(m),
   parsedModuleWrappers.get(m.getName()).replace("%basename%", baseName),
   "%s", null);
}

代码示例来源: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: org.scala-js/closure-compiler-java-6

@Override
 public String call() throws Exception {
  List<CompilerInput> inputs = module.getInputs();
  int numInputs = inputs.size();
  if (numInputs == 0) {
   return "";
  }
  CodeBuilder cb = new CodeBuilder();
  for (int i = 0; i < numInputs; i++) {
   Node scriptNode = inputs.get(i).getAstRoot(Compiler.this);
   if (scriptNode == null) {
    throw new IllegalArgumentException(
      "Bad module: " + module.getName());
   }
   toSource(cb, i, scriptNode);
  }
  return cb.toString();
 }
});

代码示例来源: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: 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.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.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: 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();
  }
}

代码示例来源:origin: org.scala-js/closure-compiler-java-6

@Override
 public String call() throws Exception {
  Tracer tracer = newTracer("toSource");
  try {
   CodeBuilder cb = new CodeBuilder();
   if (jsRoot != null) {
    int i = 0;
    for (Node scriptNode = jsRoot.getFirstChild();
       scriptNode != null;
       scriptNode = scriptNode.getNext()) {
     toSource(cb, i++, scriptNode);
    }
   }
   return cb.toString();
  } finally {
   stopTracer(tracer, "toSource");
  }
 }
});

相关文章

Compiler类方法