Package com.google.javascript.jscomp

Examples of com.google.javascript.jscomp.Compiler


     *
     * @return the error manager containing any errors from the specified file
     */
    public static ErrorManager validate(String name, String content, FormattingOption formattingOptions)
    {
        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.
        JSSourceFile extern[] = {JSSourceFile.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.
        JSSourceFile input[] = {JSSourceFile.fromCode(name, content)};
       
        if (formattingOptions != null) {
            formattingOptions.applyToOptions(options);
        }

        compiler.init(extern, input, options);

        compiler.parse();
        return compiler.getErrorManager();
    }
View Full Code Here


     *
     * @return The compiled version of the code.
     */
    private String slim(String name, String code, boolean isLib, CompilationLevel compLevel)
    {
        Compiler compiler = new Compiler();

        CompilerOptions options = new CompilerOptions();
        if (compLevel != null) {
            // Advanced mode is used here, but additional options could be set, too.
            compLevel.setOptionsForCompilationLevel(options);
        }

        // To get the complete set of externs, the logic in
        // CompilerRunner.getDefaultExterns() should be used here.
        JSSourceFile extern[] = {JSSourceFile.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.
        JSSourceFile input[] = {JSSourceFile.fromCode(name, code)};
       
        if (m_formattingOptions != null) {
            m_formattingOptions.applyToOptions(options);
        }

        compiler.init(extern, input, options);

        compiler.parse();
        m_errMgr = compiler.getErrorManager();
       
        if (m_errMgr.getErrorCount() > 0) {
            /*
             Then there were errors parsing the file and we can't
             prune anything.
             */
            return "";
        }

        Node node = compiler.getRoot();
        if (m_printTree) {
            System.out.println("Tree before pruning:");
            System.out.println(node.toStringTree());
        }
       
        //System.out.println("node before change: " + compiler.toSource());
       
        LOGGER.log(Level.INFO, "starting process...");
        Node n = process(node, isLib);
       
        LOGGER.log(Level.INFO, "Done processing...");
        LOGGER.log(Level.FINE, "m_calls: " + m_calls);
       
        m_funcCount = m_libFuncs.size();
       
        if (isLib) {
            LOGGER.log(Level.INFO, "Starting pruneTree phase 1.");
            pruneTree();
           
            LOGGER.log(Level.INFO, "Starting pruneTree phase 2.");
            pruneTree();
        }
       
        if (m_funcCount > 0) {
            System.out.println("Removed " + (m_funcCount - m_keepers.size()) + " out of " + m_funcCount + " named functions.");
        }
       
        if (m_printTree) {
            System.out.println("Tree after pruning:");
            System.out.println(node.toStringTree());
        }
       
        // The compiler is responsible for generating the compiled code; it is not
        // accessible via the Result.
        return compiler.toSource();
    }
View Full Code Here

     *
     * @return the compiled contents
     */
    public static String plainCompile(String name, String code, CompilationLevel level, FormattingOption formattingOptions)
    {
        Compiler compiler = new Compiler();
       
        compiler.setLoggingLevel(LOGGER.getLevel());
       
        Logger.getLogger("com.google.javascript.jscomp").setUseParentHandlers(false);
        Logger.getLogger("com.google.javascript.jscomp").addHandler(new SlimConsoleHandler());
       
        CompilerOptions options = new CompilerOptions();
        // Advanced mode is used here, but additional options could be set, too.
        level.setOptionsForCompilationLevel(options);
       
        if (formattingOptions != null) {
            formattingOptions.applyToOptions(options);
        }
       
        // To get the complete set of externs, the logic in
        // CompilerRunner.getDefaultExterns() should be used here.
        JSSourceFile extern = JSSourceFile.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.
        if (name == null) {
            name = "System.out.js";
        }
        JSSourceFile input = JSSourceFile.fromCode(name, 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();
    }
View Full Code Here

    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

         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

  {
    // environment for compilation
    final List<SourceFile> externs = CommandLineRunner.getDefaultExterns();

    // create compiler + options
    final Compiler compiler = new Compiler();
    final CompilerOptions options = new CompilerOptions();
    level.setOptionsForCompilationLevel(options);

    // never remove unused stuff:
    // this only would work when we had all javascript for the page bundled together
    // also this will not work due to the dynamic rendering of javascript after page creation
    options.removeUnusedVars = false;
    options.removeUnusedLocalVars = false;
    options.removeUnusedPrototypeProperties = false;
    options.removeUnusedPrototypePropertiesInExterns = false;

    // custom configuration options
    configure(compiler, options, externs);

    // TODO integrate logging into slf4j

    // input sources
    final List<JSSourceFile> inputs = new ArrayList<JSSourceFile>();
    inputs.add(JSSourceFile.fromCode("custom", uncompressed));

    // compile input
    final Result result = compiler.compile(externs, inputs, options);

    if (result.success == false)
    {
      throw new ClosureCompilationException(Arrays.asList(result.errors));
    }
    return compiler.toSource();
  }
View Full Code Here

public class ClosureMinifier implements Minifier {

  @Override
  public InputStream minify(String name, String type, InputStream stream) throws IOException {
    if (type.equals("script")) {
      Compiler compiler = new Compiler();
      CompilerOptions options = new CompilerOptions();
      SourceFile source = SourceFile.fromInputStream(name, stream);
      Result result = compiler.compile(Collections.<SourceFile>emptyList(), Collections.singletonList(source), options);
      if (result.errors.length > 0) {
        StringWriter buffer = new StringWriter();
        PrintWriter writer = new PrintWriter(buffer);
        writer.println("Malformed asset:");
        for (JSError error : result.errors) {
          writer.println(error);
        }
        throw new IOException(buffer.toString());
      }
      String s = compiler.toSource();
      return new ByteArrayInputStream(s.getBytes());
    } else {
      throw new IOException("Can only process scripts and not " + type + " asset");
    }
  }
View Full Code Here

      {
         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

    WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
    for (DiagnosticGroup dg : diagnosticGroups) {
      options.setWarningLevel(dg, CheckLevel.ERROR);
    }

    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

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.