Search in sources :

Example 11 with SourceFile

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;
}
Also used : ArrayList(java.util.ArrayList) FileResource(org.apache.tools.ant.types.resources.FileResource) Resource(org.apache.tools.ant.types.Resource) FileResource(org.apache.tools.ant.types.resources.FileResource) SourceFile(com.google.javascript.jscomp.SourceFile) SourceFile(com.google.javascript.jscomp.SourceFile) File(java.io.File)

Example 12 with SourceFile

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.");
    }
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) BuildException(org.apache.tools.ant.BuildException) SourceFile(com.google.javascript.jscomp.SourceFile) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) Result(com.google.javascript.jscomp.Result)

Example 13 with SourceFile

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);
    }
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) SourceFile(com.google.javascript.jscomp.SourceFile) Result(com.google.javascript.jscomp.Result)

Example 14 with SourceFile

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;
}
Also used : FileList(org.apache.tools.ant.types.FileList) ArrayList(java.util.ArrayList) SourceFile(com.google.javascript.jscomp.SourceFile)

Example 15 with SourceFile

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();
}
Also used : SourceFile(com.google.javascript.jscomp.SourceFile)

Aggregations

SourceFile (com.google.javascript.jscomp.SourceFile)32 Compiler (com.google.javascript.jscomp.Compiler)9 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 Result (com.google.javascript.jscomp.Result)3 StaticSourceFile (com.google.javascript.rhino.StaticSourceFile)3 EntryPoint (com.google.gwt.core.client.EntryPoint)2 LazyParsedDependencyInfo (com.google.javascript.jscomp.LazyParsedDependencyInfo)2 SourceMapInput (com.google.javascript.jscomp.SourceMapInput)2 ParserRunner (com.google.javascript.jscomp.parsing.ParserRunner)2 Node (com.google.javascript.rhino.Node)2 File (java.io.File)2 LinkedHashMap (java.util.LinkedHashMap)2 JSExtern (net.vtst.ow.closure.compiler.deps.JSExtern)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ErrorManager (com.google.javascript.jscomp.ErrorManager)1 JsAst (com.google.javascript.jscomp.JsAst)1 Config (com.google.javascript.jscomp.parsing.Config)1 Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)1