Package com.google.javascript.jscomp

Examples of com.google.javascript.jscomp.Compiler


    InputStream is = new ByteArrayInputStream(content);
    Writer out = new OutputStreamWriter(baos, charset);
    CompilerOptions options = new CompilerOptions();
    CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
    Compiler.setLoggingLevel(Level.OFF);
    Compiler compiler = new Compiler();
    compiler.disableThreads();
    Result result = compiler.compile(new JSSourceFile[] {},
      new JSSourceFile[] { JSSourceFile.fromInputStream("is", is) }, options);
    if (result.success) {
      Pattern pattern = Pattern.compile("^/\\*.*?\\*/\\s?", Pattern.DOTALL);
      Matcher matcher = pattern.matcher(new String(content, charset));
      while (matcher.find()) {
        out.write(matcher.group());
      }
      out.write(compiler.toSource());
      out.flush();
      content = baos.toByteArray();
    }
    is.close();
    out.close();
View Full Code Here


                    List<SourceFile> externs = closureConfig.getExterns();
                    if (closureConfig.getUseDefaultExterns()) {
                        externs.addAll(CommandLineRunner.getDefaultExterns());
                    }

                    Compiler compiler = new Compiler();
                    compiler.compile(externs, Lists.newArrayList(input), options);

                    if (compiler.hasErrors()) {
                        throw new EvaluatorException(compiler.getErrors()[0].description);
                    }

                    writer.append(compiler.toSource());

                    if (closureConfig.getSourceMapFormat() != null) {
                        log.info("Creating the minified file map ["
                                + ((verbose) ? sourceMapResult.getPath() : sourceMapResult.getName()) + "].");

                        sourceMapResult.createNewFile();
                        flushSourceMap(sourceMapResult, minifiedFile.getName(), compiler.getSourceMap());

                        writer.append(System.getProperty("line.separator"));
                        writer.append("//# sourceMappingURL=" + sourceMapResult.getName());
                    }
View Full Code Here

    public JSClosureCompilerWrapper()
    {
        Compiler.setLoggingLevel(Level.INFO);

        compiler_ = new Compiler();

        options_ = new CompilerOptions();
        initOptions();
       
        jsExternsFiles_ = new ArrayList<SourceFile>();
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

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

    }
  }

  public static Result compile(String program, int num) {
    JSSourceFile input = JSSourceFile.fromCode(""+num, program);
    Compiler compiler = new Compiler();
    Result result = compiler.compile(extern, input, options);
    return result;
  }
View Full Code Here

        if (script != null) {
            if (key.minified) {
                CompilationLevel level = CompilationLevel.SIMPLE_OPTIMIZATIONS;
                CompilerOptions options = new CompilerOptions();
                level.setOptionsForCompilationLevel(options);
                com.google.javascript.jscomp.Compiler compiler = new Compiler();
                compiler.setErrorManager(new LoggerErrorManager(java.util.logging.Logger.getLogger(ResourceRequestHandler.class
                        .getName())));
                StringWriter code = new StringWriter();
                IOTools.copy(script, code);
                JSSourceFile[] inputs = new JSSourceFile[] { JSSourceFile.fromCode(sourceName, code.toString()) };
                Result res = compiler.compile(new JSSourceFile[0], inputs, options);
                if (res.success) {
                    script = new StringReader(compiler.toSource());
                } else {
                    StringBuilder msg = new StringBuilder("Handle me gracefully JS errors\n");
                    for (JSError error : res.errors) {
                        msg.append(error.sourceName).append(":").append(error.lineNumber).append(" ").append(error.description)
                                .append("\n");
View Full Code Here

        if (level == null) {
            level = CompilationLevel.SIMPLE_OPTIMIZATIONS;
        }

        //
        Compiler compiler = new Compiler();
        CompilerOptions options = new CompilerOptions();
        level.setOptionsForCompilationLevel(options);
        WarningLevel.QUIET.setOptionsForWarningLevel(options);
        JSSourceFile extern = JSSourceFile.fromCode("extern", "");

        //
        JSSourceFile jsInput;
        try {
            String code = JSSourceFile.fromReader("code", input).getCode();
            jsInput = JSSourceFile.fromCode("jsInput", code);
            compiler.compile(extern, jsInput, options);
            output.write(compiler.toSource());
        } catch (Exception ex) {
            throw new ResourceCompressorException(ex);
        }
    }
View Full Code Here

  }

  public void compile(OutputStream outputStream)
      throws IOException, JavascriptCompilationException {
    PrintStream printStream = new PrintStream(outputStream);
    Compiler compiler = new Compiler(System.err);

    List<JSSourceFile> inputs = filesToJsSourceFiles(inputFiles);
    List<JSSourceFile> externs = CommandLineRunner.getDefaultExterns();
    externs.addAll(filesToJsSourceFiles(externFiles));

    compilerOptions.setManageClosureDependencies(entryPoints);

    Result result = compiler.compile(externs, inputs, compilerOptions);

    if (result.success) {
      printStream.println(compiler.toSource());
    } else {
      for (JSError error : result.errors) {
        printError(printStream, "ERROR",  error);
      }
      for (JSError warning : result.warnings) {
View Full Code Here

    replay(result);
    return result;
  }

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

      protected void printSummary() { /* Do nothing */ }

      @Override
      public void println(CheckLevel arg0, JSError arg1) { /* Do nothing */ }
    };
    return new Compiler(errorManager);
  }
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.