本文整理汇总了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);
}
}
示例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" );
}
示例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() : "");
}
示例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() : "");
}
示例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;
}
示例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();
}
示例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();
}
示例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();
}
示例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);
}
示例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;
}
示例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();
}
示例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();
}
}
示例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;
}
示例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();
}
示例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));
}