Ejemplo n.º 1
0
 @Override
 void apply(CompilerOptions options, boolean value) {
   options.setLanguageIn(
       value
           ? CompilerOptions.LanguageMode.ECMASCRIPT6
           : CompilerOptions.LanguageMode.ECMASCRIPT5);
 }
  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;
  }
 private String compile(HtmlLibrary library, ClosureOptimizationLevel opt, InputStream js)
     throws IOException {
   CompilationLevel compilationLevel = opt.toCompilationLevel();
   if (null == compilationLevel) {
     // return original input
     return IOUtils.toString(js);
   }
   SourceFile input = SourceFile.fromInputStream(getLibraryName(library), js);
   // TODO externs not supported, should avoid ADVANCED compilation
   SourceFile extern = SourceFile.fromCode("TODO", StringUtils.EMPTY);
   CompilerOptions options = new CompilerOptions();
   compilationLevel.setOptionsForCompilationLevel(options);
   // ES5 assumption to allow getters/setters
   options.setLanguageIn(CompilerOptions.LanguageMode.ECMASCRIPT5);
   Compiler compiler = new Compiler();
   compiler.compile(extern, input, options);
   return compiler.toSource();
 }