Package com.google.javascript.jscomp

Examples of com.google.javascript.jscomp.Compiler


        this.level = Args.notNull(level, "level");
    }

    @Override
    public String compress(final String original) {
        final Compiler compiler = new Compiler();

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

        final SourceFile extern = getExterns();

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


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

  private String compilationLevel = CompilationLevel.SIMPLE_OPTIMIZATIONS.toString();

  public void compile(List<File> filesToCompile, String destFileName){
    File destFile = prepareDestFile(destFileName);

    Compiler compiler = new Compiler();
    Result results = compiler.compile(getExterns(), getInputs(filesToCompile), getCompilerOptions());

    logger.debug(results.debugLog);
    for(JSError error : results.errors){
      logger.error("Closure Minifier Error:  " + error.sourceName + "  Description:  " +  error.description);
    }
    for(JSError warning : results.warnings){
      logger.info("Closure Minifier Warning:  " + warning.sourceName + "  Description:  " +  warning.description);
    }

    if (results.success) {
      try {
        Files.write(compiler.toSource(), destFile, Charsets.UTF_8);
      } catch (IOException e) {
        throw new ClosureException("Failed to write minified file to " + destFile, e);
      }
    }else{
      throw new ClosureException("Closure Compiler Failed - See error messages on System.err");
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

    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

    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 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

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

  @Override
  public void process(final Resource resource, final Reader reader, final Writer writer)
      throws IOException {
    final String content = IOUtils.toString(reader);
    final CompilerOptions compilerOptions = optionsPool.getObject();
    final Compiler compiler = newCompiler(compilerOptions);
    try {
      final String fileName = resource == null ? "wro4j-processed-file.js" : resource.getUri();
      final SourceFile[] input = new SourceFile[] {
        SourceFile.fromInputStream(fileName, new ByteArrayInputStream(content.getBytes(getEncoding())))
      };
      SourceFile[] externs = getExterns(resource);
      if (externs == null) {
        // fallback to empty array when null is provided.
        externs = new SourceFile[] {};
      }
      Result result = null;
      result = compiler.compile(Arrays.asList(externs), Arrays.asList(input), compilerOptions);
      if (result.success) {
        writer.write(compiler.toSource());
      } else {
        throw new WroRuntimeException("Compilation has errors: " + Arrays.asList(result.errors));
      }
    } catch (final Exception e) {
      onException(e);
View Full Code Here

    return encoding;
  }

  private Compiler newCompiler(final CompilerOptions compilerOptions) {
    Compiler.setLoggingLevel(Level.SEVERE);
    final Compiler compiler = new Compiler();
    compilationLevel.setOptionsForCompilationLevel(compilerOptions);
    // make it play nice with GAE
    compiler.disableThreads();
    compiler.initOptions(compilerOptions);
    return compiler;
  }
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.