当前位置: 首页>>代码示例>>Java>>正文


Java Compiler.toSource方法代码示例

本文整理汇总了Java中com.google.javascript.jscomp.Compiler.toSource方法的典型用法代码示例。如果您正苦于以下问题:Java Compiler.toSource方法的具体用法?Java Compiler.toSource怎么用?Java Compiler.toSource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.javascript.jscomp.Compiler的用法示例。


在下文中一共展示了Compiler.toSource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: map

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
@Override
public Resource<String> map(Resource<String> resource) {
    try {
        CompilerOptions options = new CompilerOptions();
        config.getCompilationLevel().setOptionsForCompilationLevel(options);
        options.setOutputCharset("UTF-8");
        options.setLanguageIn(config.getLanguage());

        SourceFile input = SourceFile.fromInputStream("dummy.js",
                new ByteArrayInputStream(resource.getContent().getBytes(Charsets.UTF_8)));
        List<SourceFile> externs = config.getExterns();

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

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

        return new Resource<String>(resource.getMimeType(), compiler.toSource(), StringHasher.instance);
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}
 
开发者ID:rjnichols,项目名称:visural-web-resources,代码行数:25,代码来源:ClosureMinifyJavascript.java

示例2: invoke

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
CompiledScript invoke() throws Exception {
  Compiler compiler = new Compiler();

  compiler.setLoggingLevel( Level.OFF );

  SourceFile input = SourceFile.fromFile( srcFile );
  input.setOriginalPath( relSrcFilePath );

  Result res = compiler.compile( EMPTY_EXTERNS_SOURCE_FILE, input, options );
  if ( res.success ) {
    String name = srcFile.getName();

    code = new StringBuilder( compiler.toSource() );
    code.append( "\n//# sourceMappingURL=" ).append( name ).append( ".map" );

    sourcemap = new StringBuilder();
    SourceMap sm = compiler.getSourceMap();
    sm.appendTo( sourcemap, name );

    return this;
  }

  throw new Exception( "Failed script compilation" );
}
 
开发者ID:pentaho,项目名称:pentaho-osgi-bundles,代码行数:25,代码来源:WebjarsURLConnection.java

示例3: compile

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
public CompileResult compile(Path path, String code) {
  Compiler compiler = compiler();
  Result result =
      compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options());
  String source = compiler.toSource();
  StringBuilder sourceMap = new StringBuilder();
  if (result.sourceMap != null) {
    try {
      result.sourceMap.appendTo(sourceMap, path.toString());
    } catch (IOException e) {
      // impossible, and not a big deal even if it did happen.
    }
  }
  boolean transpiled = !result.transpiledFiles.isEmpty();
  if (result.errors.length > 0) {
    throw new TranspilationException(compiler, result.errors, result.warnings);
  }
  return new CompileResult(
      source,
      transpiled,
      transpiled ? sourceMap.toString() : "");
}
 
开发者ID:google,项目名称:closure-compiler,代码行数:23,代码来源:BaseTranspiler.java

示例4: compile

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
public CompileResult compile(Path path, String code) {
  Compiler compiler = compiler();
  Result result =
      compiler.compile(EXTERNS, SourceFile.fromCode(path.toString(), code), options());
  String source = compiler.toSource();
  StringBuilder sourceMap = new StringBuilder();
  if (result.sourceMap != null) {
    try {
      result.sourceMap.appendTo(sourceMap, path.toString());
    } catch (IOException e) {
      // impossible, and not a big deal even if it did happen.
    }
  }
  boolean transformed = transformed(result);
  return new CompileResult(
      source, result.errors, transformed, transformed ? sourceMap.toString() : "");
}
 
开发者ID:google,项目名称:closure-compiler,代码行数:18,代码来源:CompilerBasedTransformer.java

示例5: contractEval

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
/**
 * Undoes the constant folding previously done.
 */
private String contractEval(AnalyzerCallback callback, Compiler comp, Set<String> map, String resVar) {
    Node stringRoot = getParentOfFirstInterestingNode(comp).getLastChild();

    // LHS of the assignment to the result of the eval
    String res;
    if (resVar == null)
        res = "";
    else
        res = resVar + " = ";
    
    // log("Map of holes: " + map.toString());
    boolean unevalSucceed = contractEvalHelper(callback, comp, stringRoot, map);
    // The helper modifies the AST.
    comp.reportCodeChange();

    if (!unevalSucceed)
        return null;

    String result = res + comp.toSource();
    
    // log("Retval:" + result);
    return result;
}
 
开发者ID:cursem,项目名称:ScriptCompressor,代码行数:27,代码来源:Unevalizer.java

示例6: compile

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
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();
}
 
开发者ID:steeleforge,项目名称:aemin,代码行数:18,代码来源:HtmlLibraryManagerDelegateImpl.java

示例7: compile

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
/**
	 * @param code
	 *            JavaScript source code to compile.
	 * @return The compiled version of the code.
	 */
	public static String compile(String code) {
		
//		System.out.println("------------------------");
//		System.out.println(code);
//		System.out.println("------------------------");
		
		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.
		SourceFile extern = SourceFile.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.
		SourceFile input = SourceFile.fromCode("input.js", 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();
	}
 
开发者ID:rfxlab,项目名称:analytics-with-rfx,代码行数:34,代码来源:JsOptimizerUtil.java

示例8: compile

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
public String compile(String code) {
	Compiler compiler = new Compiler();
	compiler.disableThreads();
	
	SourceFile extern = SourceFile.fromCode("externs.js","function alert(x) {}");
	SourceFile input = SourceFile.fromCode("input.js", code);
	
	compiler.compile(extern, input, options);
	return compiler.toSource();
}
 
开发者ID:bessemHmidi,项目名称:AngularBeans,代码行数:11,代码来源:ClosureCompiler.java

示例9: testPropType

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
private void testPropType(String reactPropType, String typeExpression) {
  Compiler compiler = new Compiler();
  CompilerOptions options = new CompilerOptions();
  // So that source dumps still have JSDoc
  options.preserveTypeAnnotations = true;
  compiler.initOptions(options);
  // Avoid extra "use strict" boilerplate in output.
  options.setEmitUseStrict(false);
  compiler.disableThreads(); // Makes errors easier to track down.
  Node reactPropTypeNode = compiler.parse(
      SourceFile.fromCode("/src/test.js", reactPropType))
      .getFirstChild().getFirstChild();
  assertArrayEquals(new JSError[]{}, compiler.getErrors());

  Node typeNode = PropTypesExtractor.convertPropType(reactPropTypeNode)
      .typeNode;

  // Easiest way to stringify the type node is to print it out as JSDoc.
  JSDocInfoBuilder jsDocInfoBuilder = new JSDocInfoBuilder(true);
  jsDocInfoBuilder.recordType(new JSTypeExpression(
      typeNode, "/src/test.js"));
  Node tempNode = IR.var(IR.name("temp"));
  tempNode.setJSDocInfo(jsDocInfoBuilder.build());
  String tempCode = compiler.toSource(tempNode);

  assertEquals("/** @type {" + typeExpression + "} */ var temp", tempCode);
}
 
开发者ID:mihaip,项目名称:react-closure-compiler,代码行数:28,代码来源:PropTypesExtractorTest.java

示例10: build

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
public static String build(Task task, List<File> inputs) {
  List<SourceFile> externs;
  try {
    externs = CommandLineRunner.getDefaultExterns();
  } catch (IOException e) {
    throw new BuildException(e);
  }

  List<SourceFile> jsInputs = new ArrayList<SourceFile>();
  for (File f : inputs) {
    jsInputs.add(SourceFile.fromFile(f));
  }

  CompilerOptions options = new CompilerOptions();
  CompilationLevel.ADVANCED_OPTIMIZATIONS
      .setOptionsForCompilationLevel(options);
  WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
  for (DiagnosticGroup dg : diagnosticGroups) {
    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;
}
 
开发者ID:google,项目名称:caja,代码行数:38,代码来源:ClosureCompiler.java

示例11: process

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
@Override
public String process(File[] srcFiles) throws ProcessException {
    CompilerOptions compilerOptions = new CompilerOptions();
    compilerOptions.setCodingConvention(new ClosureCodingConvention());
    compilerOptions.setOutputCharset("UTF-8");
    compilerOptions.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, CheckLevel.WARNING);
    compilerOptions.setLanguage(CompilerOptions.LanguageMode.ECMASCRIPT5);
    CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(compilerOptions);

    Compiler compiler = new Compiler();
    compiler.disableThreads();
    compiler.initOptions(compilerOptions);

    ArrayList<SourceFile> inputFiles = new ArrayList<SourceFile>();

    for (File srcFile : srcFiles) {
        SourceFile inputFile = SourceFile.fromFile(srcFile);
        inputFiles.add(inputFile);
    }

    Result result = compiler.compile(new ArrayList<SourceFile>(), inputFiles, compilerOptions);

    if (!result.success) {
        throw new ProcessException("Failure when processing javascript files!");
    }

    return compiler.toSource();
}
 
开发者ID:instant-solutions,项目名称:gradle-website-optimizer,代码行数:29,代码来源:JsProcessor.java

示例12: process

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
@Override
public void process(final Reader reader, final Writer writer) throws IOException {
    logger.debug("google closure processing...");
    final String originalJsSourceCode = IOUtils.toString(reader);
    try {
        Compiler.setLoggingLevel(Level.SEVERE);
        final Compiler compiler = new Compiler();
        final CompilerOptions compilerOptions = newCompilerOptions();
        compilationLevel.setOptionsForCompilationLevel(compilerOptions);
        //make it play nice with GAE
        compiler.disableThreads();
        compiler.initOptions(compilerOptions);

        final SourceFile input = SourceFile.fromInputStream("dummy.js", new ByteArrayInputStream(originalJsSourceCode.getBytes(soyViewConf.globalEncoding())));
        final Result result = compiler.compile(Lists.<SourceFile>newArrayList(), Lists.newArrayList(input), compilerOptions);

        logWarningsAndErrors(result);

        boolean origFallback = false;
        if (result.success) {
            final String minifiedJsSourceCode = compiler.toSource();
            if (StringUtils.isEmpty(minifiedJsSourceCode)) {
                origFallback = true;
            } else {
                writer.write(minifiedJsSourceCode);
            }
        } else {
            origFallback = true;
        }
        if (origFallback) {
            writer.write(originalJsSourceCode);
        }
    } finally {
        reader.close();
        writer.close();
    }
}
 
开发者ID:matiwinnetou,项目名称:play-soy-view,代码行数:38,代码来源:GoogleClosureOutputProcessor.java

示例13: compile

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
protected RunResult compile(
    String js1, String fileName1, String js2, String fileName2) {
  Compiler compiler = new Compiler();
  CompilerOptions options = getCompilerOptions();

  // Turn on IDE mode to get rid of optimizations.
  options.ideMode = true;

  List<SourceFile> inputs =
      ImmutableList.of(SourceFile.fromCode(fileName1, js1));

  if (js2 != null && fileName2 != null) {
    inputs = ImmutableList.of(
        SourceFile.fromCode(fileName1, js1),
        SourceFile.fromCode(fileName2, js2));
  }

  Result result = compiler.compile(EXTERNS, inputs, options);

  assertTrue("compilation failed", result.success);
  String source = compiler.toSource();

  StringBuilder sb = new StringBuilder();
  try {
    result.sourceMap.validate(true);
    result.sourceMap.appendTo(sb, "testcode");
  } catch (IOException e) {
    throw new RuntimeException("unexpected exception", e);
  }

  RunResult rr = new RunResult();
  rr.generatedSource = source;
  rr.sourceMap = result.sourceMap;
  rr.sourceMapFileContent = sb.toString();
  return rr;
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:37,代码来源:SourceMapTestCase.java

示例14: runtime

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
public String runtime(String library) {
  Compiler compiler = compiler();
  CompilerOptions options = options();
  options.setForceLibraryInjection(ImmutableList.of(library));
  compiler.compile(EXTERNS, EMPTY, options);
  return compiler.toSource();
}
 
开发者ID:google,项目名称:closure-compiler,代码行数:8,代码来源:BaseTranspiler.java

示例15: updateUi

import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
private void updateUi(Compiler compiler, Result result) {
  rightPane.clear();
  rightPane.add(new HTML("<h4>Output</h4>"));
  String outputCode = compiler.toSource();
  rightPane.add(new Label(outputCode));
  rightPane.add(new HTML("<h4>Warnings</h4>"));
  List<JSError> errors = Arrays.asList(result.errors);
  List<JSError> warnings = Arrays.asList(result.warnings);
  rightPane.add(new Label(Joiner.on("\n\n").join(Iterables.concat(errors, warnings))));
  rightPane.add(new HTML("<h4>AST</h4>"));
  String outputAst = compiler.getRoot().toStringTree();
  rightPane.add(new Label(outputAst));
}
 
开发者ID:google,项目名称:closure-compiler,代码行数:14,代码来源:DebuggerGwtMain.java


注:本文中的com.google.javascript.jscomp.Compiler.toSource方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。