use of com.google.javascript.jscomp.SourceFile in project closure-compiler by google.
the class CompileTask method findJavaScriptFiles.
/**
* Translates an Ant resource collection into the file list format that
* the compiler expects.
*/
private List<SourceFile> findJavaScriptFiles(ResourceCollection rc) {
List<SourceFile> files = new ArrayList<>();
Iterator<Resource> iter = rc.iterator();
while (iter.hasNext()) {
FileResource fr = (FileResource) iter.next();
// Construct path to file, relative to current working directory.
File file = Paths.get("").toAbsolutePath().relativize(fr.getFile().toPath()).toFile();
files.add(SourceFile.fromFile(file, Charset.forName(encoding)));
}
return files;
}
use of com.google.javascript.jscomp.SourceFile in project closure-compiler by google.
the class CompileTask method execute.
@Override
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);
List<SourceFile> externs = findExternFiles(options);
List<SourceFile> sources = findSourceFiles();
if (isStale() || forceRecompile) {
log("Compiling " + sources.size() + " file(s) with " + externs.size() + " extern(s)");
Result result = compiler.compile(externs, sources, options);
if (result.success) {
StringBuilder source = new StringBuilder(compiler.toSource());
if (this.outputWrapperFile != null) {
try {
this.outputWrapper = Files.asCharSource(this.outputWrapperFile, UTF_8).read();
} catch (Exception e) {
throw new BuildException("Invalid output_wrapper_file specified.");
}
}
if (this.outputWrapper != null) {
int pos = this.outputWrapper.indexOf(CommandLineRunner.OUTPUT_MARKER);
if (pos > -1) {
String prefix = this.outputWrapper.substring(0, pos);
source.insert(0, prefix);
// end of outputWrapper
int suffixStart = pos + CommandLineRunner.OUTPUT_MARKER.length();
String suffix = this.outputWrapper.substring(suffixStart);
source.append(suffix);
} else {
throw new BuildException("Invalid output_wrapper specified. " + "Missing '" + CommandLineRunner.OUTPUT_MARKER + "'.");
}
}
if (result.sourceMap != null) {
flushSourceMap(result.sourceMap);
}
writeResult(source.toString());
} else {
throw new BuildException("Compilation failed.");
}
} else {
log("None of the files changed. Compilation skipped.");
}
}
use of com.google.javascript.jscomp.SourceFile in project closure-compiler by google.
the class DebuggerGwtMain method doCompile.
private void doCompile() {
SourceFile externFile = SourceFile.fromCode("externs", externs.getValue());
SourceFile srcFile = SourceFile.fromCode("input0", input0.getValue());
Compiler compiler = new Compiler();
try {
Result result = compiler.compile(externFile, srcFile, options);
updateUi(compiler, result);
} catch (Exception e) {
updateUiException(e);
}
}
use of com.google.javascript.jscomp.SourceFile in project closure-compiler by google.
the class CompileTask method findExternFiles.
private List<SourceFile> findExternFiles(CompilerOptions options) {
List<SourceFile> files = new ArrayList<>();
files.addAll(getBuiltinExterns(options));
for (FileList list : this.externFileLists) {
files.addAll(findJavaScriptFiles(list));
}
return files;
}
use of com.google.javascript.jscomp.SourceFile in project closure-compiler by google.
the class DepsGeneratorTest method testDuplicateProvidesIgnoredIfInClosureDirectory.
/**
* Ensures that DepsGenerator deduplicates dependencies from custom Closure Library branches.
*/
public void testDuplicateProvidesIgnoredIfInClosureDirectory() throws Exception {
// Create a stub Closure Library.
SourceFile fauxClosureDeps = SourceFile.fromCode("dep1.js", "goog.addDependency('foo/a.js', ['a'], []);\n");
SourceFile fauxClosureSrc = SourceFile.fromCode("path/to/closure/foo/a.js", "goog.provide('a');\n");
// Create a source file that depends on the stub Closure Library.
SourceFile userSrc = SourceFile.fromCode("my/package/script.js", "goog.require('a');\n" + "goog.provide('my.package.script');\n");
DepsGenerator worker = new DepsGenerator(ImmutableList.of(fauxClosureDeps), ImmutableList.of(fauxClosureSrc, userSrc), DepsGenerator.InclusionStrategy.ALWAYS, PathUtil.makeAbsolute("./path/to/closure"), errorManager, new ModuleLoader(null, ImmutableList.of("."), ImmutableList.<DependencyInfo>of(), ModuleLoader.PathResolver.ABSOLUTE, ModuleLoader.ResolutionMode.BROWSER));
String output = worker.computeDependencyCalls();
assertWithMessage("Output should have been created.").that(output).isNotEmpty();
assertNoWarnings();
}
Aggregations