Java Code Examples for com.google.javascript.jscomp.Compiler#toSource()

The following examples show how to use com.google.javascript.jscomp.Compiler#toSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClosureCompiler.java    From AngularBeans with GNU Lesser General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 2
Source File: ClosureCompiler.java    From caja with Apache License 2.0 5 votes vote down vote up
public static String build(Task task, List<File> inputs) {
  List<SourceFile> externs;
  try {
    externs = CommandLineRunner.getDefaultExterns();
  } catch (IOException e) {
    throw new BuildException(e);
  }

  List<SourceFile> jsInputs = new ArrayList<SourceFile>();
  for (File f : inputs) {
    jsInputs.add(SourceFile.fromFile(f));
  }

  CompilerOptions options = new CompilerOptions();
  CompilationLevel.ADVANCED_OPTIMIZATIONS
      .setOptionsForCompilationLevel(options);
  WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
  for (DiagnosticGroup dg : diagnosticGroups) {
    options.setWarningLevel(dg, CheckLevel.ERROR);
  }

  options.setCodingConvention(new GoogleCodingConvention());

  Compiler compiler = new Compiler();
  MessageFormatter formatter =
      options.errorFormat.toFormatter(compiler, false);
  AntErrorManager errorManager = new AntErrorManager(formatter, task);
  compiler.setErrorManager(errorManager);

  Result r = compiler.compile(externs, jsInputs, options);
  if (!r.success) {
    return null;
  }

  String wrapped = "(function(){" + compiler.toSource() + "})();\n";
  return wrapped;
}
 
Example 3
Source File: SourceMapTestCase.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected RunResult compile(
    String js1, String fileName1, String js2, String fileName2) {
  Compiler compiler = new Compiler();
  CompilerOptions options = getCompilerOptions();

  // Turn on IDE mode to get rid of optimizations.
  options.ideMode = true;

  List<SourceFile> inputs =
      ImmutableList.of(SourceFile.fromCode(fileName1, js1));

  if (js2 != null && fileName2 != null) {
    inputs = ImmutableList.of(
        SourceFile.fromCode(fileName1, js1),
        SourceFile.fromCode(fileName2, js2));
  }

  Result result = compiler.compile(EXTERNS, inputs, options);

  assertTrue("compilation failed", result.success);
  String source = compiler.toSource();

  StringBuilder sb = new StringBuilder();
  try {
    result.sourceMap.validate(true);
    result.sourceMap.appendTo(sb, "testcode");
  } catch (IOException e) {
    throw new RuntimeException("unexpected exception", e);
  }

  RunResult rr = new RunResult();
  rr.generatedSource = source;
  rr.sourceMap = result.sourceMap;
  rr.sourceMapFileContent = sb.toString();
  return rr;
}
 
Example 4
Source File: JsOptimizerUtil.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
/**
	 * @param code
	 *            JavaScript source code to compile.
	 * @return The compiled version of the code.
	 */
	public static String compile(String code) {
		
//		System.out.println("------------------------");
//		System.out.println(code);
//		System.out.println("------------------------");
		
		Compiler compiler = new Compiler();

		CompilerOptions options = new CompilerOptions();
		// Advanced mode is used here, but additional options could be set, too.
		CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);

		// To get the complete set of externs, the logic in
		// CompilerRunner.getDefaultExterns() should be used here.
		SourceFile extern = SourceFile.fromCode("externs.js", "");

		// The dummy input name "input.js" is used here so that any warnings or
		// errors will cite line numbers in terms of input.js.
		SourceFile input = SourceFile.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();
	}
 
Example 5
Source File: GoogleClosureOutputProcessor.java    From spring-soy-view with Apache License 2.0 5 votes vote down vote up
@Override
public void process(final Reader reader, final Writer writer) throws IOException {
    final String originalJsSourceCode = IOUtils.toString(reader);
    try {
        Compiler.setLoggingLevel(Level.SEVERE);
        final Compiler compiler = new Compiler();
        final CompilerOptions compilerOptions = newCompilerOptions();
        compilationLevel.setOptionsForCompilationLevel(compilerOptions);
        //make it play nice with GAE
        compiler.disableThreads();
        compiler.initOptions(compilerOptions);

        final SourceFile input = SourceFile.fromInputStream("dummy.js", new ByteArrayInputStream(originalJsSourceCode.getBytes(getEncoding())));
        final Result result = compiler.compile(Lists.<SourceFile>newArrayList(), Lists.newArrayList(input), compilerOptions);

        logWarningsAndErrors(result);

        boolean origFallback = false;
        if (result.success) {
            final String minifiedJsSourceCode = compiler.toSource();
            if (StringUtils.isEmpty(minifiedJsSourceCode)) {
                origFallback = true;
            } else {
                writer.write(minifiedJsSourceCode);
            }
        } else {
            origFallback = true;
        }
        if (origFallback) {
            writer.write(originalJsSourceCode);
        }
    } finally {
        reader.close();
        writer.close();
    }
}
 
Example 6
Source File: MinifyUtil.java    From minifierbeans with Apache License 2.0 4 votes vote down vote up
public String compressSelectedJavaScript(String inputFilename, String content, MinifyProperty minifyProperty) throws IOException {
    Compiler compiler = new Compiler();
    CompilerOptions options = new CompilerOptions();

    compiler.initOptions(options);

    options.setStrictModeInput(false);
    options.setEmitUseStrict(false);
    options.setTrustedStrings(true);

    CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);

    List<SourceFile> inputs = new ArrayList<SourceFile>();
    inputs.add(SourceFile.fromCode(inputFilename, content));

    StringWriter outputWriter = new StringWriter();

    compiler.compile(CommandLineRunner.getDefaultExterns(), inputs, options);
    outputWriter.flush();

    return compiler.toSource();
}