Esempio n. 1
0
  /** core method */
  private void writeJsTo(Writer writer) throws IOException {
    Compiler compiler;
    CompilerOptions options;
    List<SourceFile> externals;
    List<SourceFile> sources;
    Result result;
    boolean first;
    int i;
    Node node;

    compiler = new Compiler(new LoggerErrorManager());
    options = new CompilerOptions();
    options.setOutputCharset(Engine.ENCODING);
    sources = new ArrayList<>();
    externals = new ArrayList<>();
    for (i = 0; i < nodes.size(); i++) {
      node = nodes.get(i);
      sources.add(
          SourceFile.fromCode(
              location(node) + /* to get unique names, which is checked by the compiler: */ "_" + i,
              readString(node)));
    }
    result = compiler.compile(externals, sources, options);
    if (!result.success) {
      if (result.errors.length < 1) {
        throw new IllegalStateException();
      }
      throw new IOException(
          result.errors[0].sourceName
              + ":"
              + result.errors[0].lineNumber
              + ":"
              + result.errors[0].description);
    }
    if (overallMinimize) {
      writer.write(compiler.toSource());
    } else {
      first = true;
      for (SourceFile source : sources) {
        if (first) {
          first = false;
        } else {
          writer.write(LF);
        }
        if (!overallMinimize) {
          writer.write(type.comment(source.getName()));
        }
        writer.write(source.getCode());
      }
    }
  }
  private CompilerOptions createCompilerOptions() {
    CompilerOptions options = new CompilerOptions();

    this.compilationLevel.setOptionsForCompilationLevel(options);
    if (this.debugOptions) {
      this.compilationLevel.setDebugOptionsForCompilationLevel(options);
    }

    options.prettyPrint = this.prettyPrint;
    options.printInputDelimiter = this.printInputDelimiter;
    options.generateExports = this.generateExports;

    options.setLanguageIn(this.languageIn);
    options.setOutputCharset(this.outputEncoding);

    this.warningLevel.setOptionsForWarningLevel(options);
    options.setManageClosureDependencies(manageDependencies);
    convertEntryPointParameters(options);
    options.setTrustedStrings(true);

    if (replaceProperties) {
      convertPropertiesMap(options);
    }

    convertDefineParameters(options);

    for (Warning warning : warnings) {
      CheckLevel level = warning.getLevel();
      String groupName = warning.getGroup();
      DiagnosticGroup group = new DiagnosticGroups().forName(groupName);
      if (group == null) {
        throw new BuildException("Unrecognized 'warning' option value (" + groupName + ")");
      }
      options.setWarningLevel(group, level);
    }

    if (!Strings.isNullOrEmpty(sourceMapFormat)) {
      options.sourceMapFormat = Format.valueOf(sourceMapFormat);
    }

    if (sourceMapOutputFile != null) {
      File parentFile = sourceMapOutputFile.getParentFile();
      if (parentFile.mkdirs()) {
        log("Created missing parent directory " + parentFile, Project.MSG_DEBUG);
      }
      options.sourceMapOutputPath = parentFile.getAbsolutePath();
    }
    return options;
  }
  /**
   * Minimize the output using google closure compiler.
   *
   * @param output The input file to minimize.
   * @throws IOException If something goes wrong.
   * @throws MojoFailureException If something goes wrong.
   */
  private void minimize(final File output) throws IOException, MojoFailureException {
    final CompilerOptions options = new CompilerOptions();
    options.setCodingConvention(new ClosureCodingConvention());
    options.setOutputCharset("UTF-8");
    options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING);
    CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);

    Compiler.setLoggingLevel(Level.SEVERE);
    Compiler compiler = new Compiler();
    compiler.disableThreads();
    compiler.initOptions(options);

    Result result =
        compiler.compile(
            Collections.<SourceFile>emptyList(),
            Arrays.asList(SourceFile.fromFile(output)),
            options);
    if (result.success) {
      FileUtils.fileWrite(output, compiler.toSource());
    } else {
      JSError[] errors = result.errors;
      throw new MojoFailureException(errors[0].toString());
    }
  }