Package com.google.javascript.jscomp

Examples of com.google.javascript.jscomp.Compiler


    List<JsContent> allContent = Lists.newLinkedList(content);
    if (options.isExternExportsEnabled()) {
      allContent.add(EXPORTSYMBOL_CODE);
    }

    Compiler actualCompiler = newCompiler();
    Result result = actualCompiler.compile(
        allExterns,
        convertToJsSource(allContent),
        options);

    if (actualCompiler.hasErrors()) {
      ImmutableList.Builder<String> errors = ImmutableList.builder();
      for (JSError error : actualCompiler.getErrors()) {
        errors.add(error.toString());
      }
      return cacheAndReturnErrorResult(
          builder, cacheKey,
          HttpResponse.SC_NOT_FOUND,
View Full Code Here


  }

  protected CompileResult doCompileContent(JsContent content, CompilerOptions options,
      List<SourceFile> externs) throws CompilerException {

    Compiler compiler = new Compiler(getErrorManager()); // We shouldn't reuse compilers
    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);
    }
View Full Code Here

    }

    Compiler.setLoggingLevel(Level.OFF);

    CompilerOptions options = createCompilerOptions();
    Compiler compiler = createCompiler(options);

    JSSourceFile[] externs = findExternFiles();
    JSSourceFile[] sources = findSourceFiles();

    if (isStale()) {
      log("Compiling " + sources.length + " file(s) with " +
          externs.length + " extern(s)");

      Result result = compiler.compile(externs, sources, options);
      if (result.success) {
        writeResult(compiler.toSource());
      } else {
        throw new BuildException("Compilation failed.");
      }
    } else {
      log("None of the files changed. Compilation skipped.");
View Full Code Here

      }
    }
  }

  private Compiler createCompiler(CompilerOptions options) {
    Compiler compiler = new Compiler();
    MessageFormatter formatter =
        options.errorFormat.toFormatter(compiler, false);
    AntErrorManager errorManager = new AntErrorManager(formatter, this);
    compiler.setErrorManager(errorManager);
    return compiler;
  }
View Full Code Here

  /** Report from the last compilation */
  private Report report;

  public SecureCompiler() {
    compiler = new Compiler();
    options = getSecureCompilerOptions();
  }
View Full Code Here

     *         error occurs during compression.
     */
    public String compress(String jsCode) {
        String result = jsCode;
        try {
            Compiler compiler = new Compiler();

            CompilerOptions options = new CompilerOptions();

            // use the simple compilation level
            CompilationLevel.SIMPLE_OPTIMIZATIONS
                .setOptionsForCompilationLevel(options);

            // add the default externals
            List externsList = getDefaultExterns();
            JSSourceFile[] externs = new JSSourceFile[externsList.size()];
            externsList.toArray(externs);

            // 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[] inputs = new JSSourceFile[1];
            inputs[0] = JSSourceFile.fromCode("input", jsCode);

            // compile() returns a Result, but it is not needed here.
            compiler.compile(externs, inputs, options);

            // The compiler is responsible for generating the compiled code; it
            // is not accessible via the Result.
            result = compiler.toSource();
        } catch (Exception e) {
            log.error("Not able to compress the given JavaScript", e);
        }
        return result;
    }
View Full Code Here

        CompilerOptions options = new CompilerOptions();
        options.setCodingConvention(new ClosureCodingConvention());
        options.setOutputCharset("utf-8");
        options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING);

        Compiler compiler = new Compiler();

        compiler.disableThreads();

        SourceFile input = SourceFile.fromInputStream(resource.toString(), resource.openStream());

        List<SourceFile> inputs = Collections.singletonList(input);

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

        if (result.success)
        {
            return IOUtils.toInputStream(compiler.toSource());
        }

        throw new RuntimeException(String.format("Compilation failed: %s.",
                InternalUtils.join(CollectionFactory.newList(result.errors), ";")));
    }
View Full Code Here

    return options;
  }

  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;

    JSSourceFile[] inputs = { JSSourceFile.fromCode(fileName1, js1) };

    if (js2 != null && fileName2 != null) {
      JSSourceFile[] multiple =  { JSSourceFile.fromCode(fileName1, js1),
                                   JSSourceFile.fromCode(fileName2, js2) };
      inputs = multiple;
    }

    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");
View Full Code Here

    testJsonMLToAstConversion(ast, jsonml, js);
  }

  private void testJsonMLToAstConversion(Node astRoot, JsonML jsonmlRoot,
      String js) {
    Compiler compiler = new Compiler();
    JsonMLAst ast = new JsonMLAst(jsonmlRoot);
    Node resultAstRoot = ast.getAstRoot(compiler);

    String explanation = resultAstRoot.checkTreeEquals(astRoot);
    assertNull("JsonML -> AST converter returned incorect result for " + js
View Full Code Here

   *   and https://code.google.com/p/closure-compiler/wiki/FAQ#How_do_I_call_Closure_Compiler_from_the_Java_API? */
  @Override
  public Reader minify(String settingName, List<InputSource> inputSources) throws ContentProcessingException {
    ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
   
    Compiler compiler = new Compiler( new PrintStream(errorStream) );
    CompilerOptions options = new CompilerOptions();
   
    getCompilationLevelForSettingName(settingName).setOptionsForCompilationLevel(options);
   
    /* we have to use an extern, so create a dummy one */
    SourceFile extern = SourceFile.fromCode("externs.js", "function alert(x) {}");
    SourceFile input;
    try
    {
      input = SourceFile.fromInputStream( "input.js", getSingleInputStreamForInputSources(inputSources) );
    }
    catch (IOException ex)
    {
      throw new ContentProcessingException(ex);
    }
   
    List<Reader> readers = new LinkedList<>();
   
    Result result = compiler.compile(extern, input, options);
    if (result.success)
    {
      logger.debug(Messages.OUTPUT_FROM_MINIFIER, errorStream.toString());
      readers.add( new StringReader(compiler.toSource()) );
    }
    else
    {
      logger.error(Messages.ERROR_WHILE_BUNDLING_MSG, errorStream.toString());
      readers.add( new StringReader(String.format(Messages.ERROR_WHILE_BUNDLING_MSG, errorStream.toString())) );
View Full Code Here

TOP

Related Classes of com.google.javascript.jscomp.Compiler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.