本文整理汇总了Java中com.google.javascript.jscomp.Compiler.compile方法的典型用法代码示例。如果您正苦于以下问题:Java Compiler.compile方法的具体用法?Java Compiler.compile怎么用?Java Compiler.compile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.javascript.jscomp.Compiler
的用法示例。
在下文中一共展示了Compiler.compile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: execute
import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
public void execute() {
if (this.outputFile == null) {
throw new BuildException("outputFile attribute must be set");
}
Compiler.setLoggingLevel(Level.OFF);
CompilerOptions options = createCompilerOptions();
Compiler compiler = createCompiler(options);
JSSourceFile[] externs = findExternFiles();
JSSourceFile[] sources = findSourceFiles();
log("Compiling " + sources.length + " file(s) with " +
externs.length + " extern(s)");
Result result = compiler.compile(externs, sources, options);
if (result.success) {
writeResult(compiler.toSource());
}
}
示例2: create
import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
/**
* Creates a symbolic executor which will instrument the given file and then,
* when run, symbolically execute starting by calling entryFunction with the
* given inputs.
*
* @param filename a JavaScript file which will be instrumented and then
* symbolically executed
* @param loader a FileLoader to help load files
* @param newInputGenerator the object that will generate new inputs from old
* ones
* @param maxNumInputs symbolic execution will stop after this many inputs are
* generated. This isn't a strict maximum, though, because new inputs
* are generated in batches, and we stop after the first <em>batch</em>
* puts us at or past this limit.
* @param entryFunction the function at which symbolic execution will start
* @param inputs the initial inputs to use when calling entryFunction
*/
public static SymbolicExecutor create(String filename, FileLoader loader,
NewInputGenerator newInputGenerator, int maxNumInputs,
String entryFunction, String... inputs) {
CompilerOptions options = new CompilerOptions();
// Enable instrumentation
options.symbolicExecutionInstrumentation = true;
options.prettyPrint=true;
// Compile the program; this is where the instrumentation happens.
Compiler compiler = new Compiler();
compiler.compile(new ArrayList<JSSourceFile>(),
Lists.newArrayList(JSSourceFile.fromFile(loader.getPath(filename))),
options);
// Add the call to symbolicExecution.execute
String code = String.format("%s;symbolicExecution.execute(this,%s,inputs)",
compiler.toSource(), entryFunction);
return create(code, loader, newInputGenerator, inputs, maxNumInputs);
}
示例3: execute
import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
public void execute() {
if (this.outputFile == null) {
throw new BuildException("outputFile attribute must be set");
}
Compiler.setLoggingLevel(Level.OFF);
CompilerOptions options = createCompilerOptions();
Compiler compiler = createCompiler(options);
JSSourceFile[] externs = findExternFiles();
JSSourceFile[] sources = findSourceFiles();
log("Compiling " + sources.length + " file(s) with " +
externs.length + " extern(s)");
Result result = compiler.compile(externs, sources, options);
if (result.success) {
writeResult(compiler.toSource());
} else {
throw new BuildException("Compilation failed.");
}
}
示例4: 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);
}
}
示例5: 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" );
}
示例6: 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() : "");
}
示例7: 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() : "");
}
示例8: 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();
}
示例9: minify
import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
/**
* Minifies the JS contents from the given merged file into the minified file
*
* <p>
* Minification is performed using the Google Closure compiler, using
* com.google.javascript.jscomp.CompilationLevel#WHITESPACE_ONLY and EcmaScript5 language level
* </p>
*
* @see ThemeFilesProcessor#minify(java.io.File, java.io.File)
* @see com.google.javascript.jscomp.Compiler
*/
@Override
protected void minify(File mergedFile, File minifiedFile) throws IOException {
InputStream in = null;
OutputStream out = null;
OutputStreamWriter writer = null;
InputStreamReader reader = null;
LOG.info("Populating minified JS file: " + minifiedFile.getPath());
try {
out = new FileOutputStream(minifiedFile);
writer = new OutputStreamWriter(out);
in = new FileInputStream(mergedFile);
reader = new InputStreamReader(in);
CompilerOptions options = new CompilerOptions();
CompilationLevel.WHITESPACE_ONLY.setOptionsForCompilationLevel(options);
options.setLanguageIn(CompilerOptions.LanguageMode.ECMASCRIPT6);
options.setExtraAnnotationNames(ignoredAnnotations());
SourceFile input = SourceFile.fromInputStream(mergedFile.getName(), in);
List<SourceFile> externs = Collections.emptyList();
Compiler compiler = new Compiler();
compiler.compile(externs, Arrays.asList(input), options);
writer.append(compiler.toSource());
writer.flush();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
示例10: 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();
}
示例11: main
import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
Compiler compiler = new Compiler(System.err);
compiler.disableThreads(); // Makes errors easier to track down.
List<SourceFile> inputs = ImmutableList.of(
SourceFile.fromInputStream("stdin", System.in, StandardCharsets.UTF_8)
);
CompilerOptions options = new CompilerOptions();
Result result = compiler.compile(
Collections.<SourceFile>emptyList(), inputs, options);
dump(compiler.getRoot(), 0, System.out);
System.out.append("\n");
}
示例12: testReactWarningsGuard
import com.google.javascript.jscomp.Compiler; //导入方法依赖的package包/类
@Test public void testReactWarningsGuard() {
Compiler compiler = new Compiler();
CompilerOptions options = new CompilerOptions();
options.addWarningsGuard(new ReactWarningsGuard());
// Should not warn about non-standard JSDoc.
List<SourceFile> inputs = ImmutableList.of(
SourceFile.fromCode("/src/react.js", "/** @providesModule React */"));
List<SourceFile> externs = ImmutableList.of(
SourceFile.fromCode("externs", ""));
Result result = compiler.compile(externs, inputs, options);
assertTrue(result.success);
assertEquals(result.errors.length, 0);
assertEquals(result.warnings.length, 0);
}
示例13: 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;
}
示例14: 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();
}
示例15: 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();
}
}