Search in sources :

Example 81 with Compiler

use of com.google.javascript.jscomp.Compiler 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 82 with Compiler

use of com.google.javascript.jscomp.Compiler in project closure-compiler by google.

the class RefasterJsScannerTest method testInitialize_missingTemplates.

@Test
public void testInitialize_missingTemplates() throws Exception {
    try {
        Compiler compiler = createCompiler();
        compileTestCode(compiler, "", "");
        createScanner(compiler, "");
        fail("An exception should have been thrown for missing templates.");
    } catch (IllegalStateException expected) {
    }
    try {
        Compiler compiler = createCompiler();
        compileTestCode(compiler, "", "");
        createScanner(compiler, "function notATemplate() {}");
        fail("An exception should have been thrown for missing templates.");
    } catch (IllegalStateException expected) {
    }
    try {
        Compiler compiler = createCompiler();
        compileTestCode(compiler, "", "");
        createScanner(compiler, "function after_foo() {}");
        fail("An exception should have been thrown for missing templates.");
    } catch (IllegalStateException expected) {
    }
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) Test(org.junit.Test)

Example 83 with Compiler

use of com.google.javascript.jscomp.Compiler in project ow by vtst.

the class MainForDebug method compile.

public static void compile(JSModule module) {
    Compiler compiler = CompilerUtils.makeCompiler(CompilerUtils.makePrintingErrorManager(System.out));
    CompilerOptions options = CompilerUtils.makeOptionsForParsingAndErrorReporting();
    compiler.initOptions(options);
    JSSourceFile extern = JSSourceFile.fromCode("externs.js", "");
    compiler.compile(extern, new JSModule[] { module }, options);
    System.out.println(compiler.toSource());
}
Also used : Compiler(com.google.javascript.jscomp.Compiler) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) JSSourceFile(com.google.javascript.jscomp.JSSourceFile)

Example 84 with Compiler

use of com.google.javascript.jscomp.Compiler in project ow by vtst.

the class ClosureCompilerLaunchConfigurationDelegate method launch.

@Override
public void launch(ILaunchConfiguration config, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
    monitor.beginTask(config.getName(), 1);
    monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_prepareCompiler"));
    IReadOnlyStore store = new LaunchConfigurationReadOnlyStore(config);
    List<IResource> resources = record.inputResources.get(store);
    if (resources.isEmpty())
        return;
    // Getting the stores for project configurations
    IProject project = null;
    if (record.useProjectPropertiesForChecks.get(store) || record.useProjectPropertiesForIncludes.get(store)) {
        project = ClosureCompiler.getCommonProject(resources);
        if (project == null)
            throw new CoreException(new Status(Status.ERROR, OwJsClosurePlugin.PLUGIN_ID, messages.getString("ClosureCompilerLaunchConfigurationDelegate_differentProjects")));
    }
    IReadOnlyStore storeForChecks = record.useProjectPropertiesForChecks.get(store) ? new ResourcePropertyStore(project, OwJsClosurePlugin.PLUGIN_ID) : store;
    IReadOnlyStore storeForIncludes = record.useProjectPropertiesForIncludes.get(store) ? new ResourcePropertyStore(project, OwJsClosurePlugin.PLUGIN_ID) : store;
    // Get the output file
    IFile outputFile = getOutputFile(store, resources);
    // Create and configure the compiler
    ClosureCompilerProcess process = new ClosureCompilerProcess(launch);
    Compiler compiler = CompilerUtils.makeCompiler(process.getErrorManager());
    CompilerOptions options = ClosureCompilerOptions.makeForLaunch(storeForChecks, store);
    compiler.initOptions(options);
    // Get the files to compile
    Set<IFile> allFiles, rootFiles;
    List<AbstractJSProject> libraries;
    if (record.manageClosureDependencies.get(store)) {
        // If dependencies are managed, we take all projects containing selected resources,
        // then all their referenced projects.
        // TODO: It should not be allowed to customize the includes in this case, we should always
        // use the project ones.
        Collection<IProject> projects = getProjects(resources);
        Comparator<IProject> comparator = OwJsClosurePlugin.getDefault().getProjectOrderManager().get().reverseOrderComparator();
        ArrayList<IProject> allProjects = ClosureCompiler.getReferencedJavaScriptProjectsRecursively(projects, comparator);
        monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_loadLibraries"));
        libraries = includesProvider.getLibraries(compiler, monitor, allProjects);
        monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_prepareCompiler"));
        allFiles = ClosureCompiler.getJavaScriptFilesOfProjects(allProjects);
        for (IResource resource : resources) {
            if (!(resource instanceof IProject))
                allFiles.addAll(ClosureCompiler.getJavaScriptFiles(resource));
        }
        rootFiles = Utils.getAllContainedFilesWhichAreInSet(resources, allFiles);
    } else {
        // If dependencies are not managed, we take only what has been selected.
        monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_loadLibraries"));
        libraries = includesProvider.getLibraries(compiler, monitor, storeForIncludes);
        monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_prepareCompiler"));
        allFiles = ClosureCompiler.getJavaScriptFiles(resources);
        rootFiles = allFiles;
    }
    // Build the project to compile
    File closureBasePath = ClosureCompiler.getPathOfClosureBase(storeForIncludes);
    Map<IFile, JSUnit> units = makeJSUnits(closureBasePath, allFiles);
    List<JSUnit> rootUnits = new ArrayList<JSUnit>(rootFiles.size());
    for (IFile selectedJsFile : rootFiles) rootUnits.add(units.get(selectedJsFile));
    try {
        JSProject jsProject = makeJSProject(compiler, Lists.newArrayList(units.values()), libraries, closureBasePath);
        List<JSUnit> rootUnitsWithTheirDependencies = jsProject.getSortedDependenciesOf(rootUnits);
        JSModule module = new JSModule("main");
        for (JSUnit unit : rootUnitsWithTheirDependencies) module.add(new CompilerInput(unit.getAst(false)));
        monitor.subTask(messages.getString("ClosureCompilerLaunchConfigurationDelegate_runCompiler"));
        compiler.compileModules(getExterns(compiler, monitor, storeForIncludes), Collections.singletonList(module), options);
        if (outputFile.exists()) {
            outputFile.setContents(new ByteArrayInputStream(compiler.toSource().getBytes("UTF-8")), false, false, monitor);
        } else {
            outputFile.create(new ByteArrayInputStream(compiler.toSource().getBytes("UTF-8")), false, monitor);
        }
        outputFile.setCharset("UTF-8", monitor);
        ClosureFilePropertyRecord.getInstance().generatedByCompiler.set(new ResourcePropertyStore(outputFile, OwJsClosurePlugin.PLUGIN_ID), true);
        process.setTerminated();
        monitor.done();
    } catch (CircularDependencyException e) {
        throw new CoreException(new Status(Status.ERROR, OwJsClosurePlugin.PLUGIN_ID, e.getLocalizedMessage(), e));
    } catch (IOException e) {
        throw new CoreException(new Status(Status.ERROR, OwJsClosurePlugin.PLUGIN_ID, e.getLocalizedMessage(), e));
    }
}
Also used : AbstractJSProject(net.vtst.ow.closure.compiler.deps.AbstractJSProject) JSProject(net.vtst.ow.closure.compiler.deps.JSProject) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) LaunchConfigurationReadOnlyStore(net.vtst.eclipse.easy.ui.properties.stores.LaunchConfigurationReadOnlyStore) CircularDependencyException(com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException) Status(org.eclipse.core.runtime.Status) AbstractCompiler(com.google.javascript.jscomp.AbstractCompiler) ClosureCompiler(net.vtst.ow.eclipse.js.closure.compiler.ClosureCompiler) Compiler(com.google.javascript.jscomp.Compiler) IOException(java.io.IOException) JSUnit(net.vtst.ow.closure.compiler.deps.JSUnit) IProject(org.eclipse.core.resources.IProject) IReadOnlyStore(net.vtst.eclipse.easy.ui.properties.stores.IReadOnlyStore) CompilerInput(com.google.javascript.jscomp.CompilerInput) CoreException(org.eclipse.core.runtime.CoreException) AbstractJSProject(net.vtst.ow.closure.compiler.deps.AbstractJSProject) ByteArrayInputStream(java.io.ByteArrayInputStream) ResourcePropertyStore(net.vtst.eclipse.easy.ui.properties.stores.ResourcePropertyStore) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) ClosureCompilerOptions(net.vtst.ow.eclipse.js.closure.compiler.ClosureCompilerOptions) JSModule(com.google.javascript.jscomp.JSModule) IFile(org.eclipse.core.resources.IFile) SourceFile(com.google.javascript.jscomp.SourceFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource)

Example 85 with Compiler

use of com.google.javascript.jscomp.Compiler in project divolte-collector by divolte.

the class JavaScriptResource method compile.

private static Compiler compile(final String filename, final InputStream javascript, final ImmutableMap<String, Object> scriptConstants, final boolean debugMode) throws IOException {
    final CompilerOptions options = new CompilerOptions();
    COMPILATION_LEVEL.setOptionsForCompilationLevel(options);
    COMPILATION_LEVEL.setTypeBasedOptimizationOptions(options);
    options.setEnvironment(CompilerOptions.Environment.BROWSER);
    options.setLanguageIn(ECMASCRIPT5_STRICT);
    options.setLanguageOut(ECMASCRIPT5_STRICT);
    WarningLevel.VERBOSE.setOptionsForWarningLevel(options);
    // can be related more easily to the original JavaScript source.
    if (debugMode) {
        options.setPrettyPrint(true);
        COMPILATION_LEVEL.setDebugOptionsForCompilationLevel(options);
    }
    options.setDefineReplacements(scriptConstants);
    final SourceFile source = SourceFile.fromInputStream(filename, javascript, StandardCharsets.UTF_8);
    final Compiler compiler = new Compiler();
    final ErrorManager errorManager = new SortingErrorManager(ImmutableSet.of(new Slf4jErrorReportGenerator(compiler)));
    compiler.setErrorManager(errorManager);
    // TODO: Use an explicit list of externs instead of the default browser set, to control compatibility.
    final List<SourceFile> externs = CommandLineRunner.getBuiltinExterns(options.getEnvironment());
    compiler.compile(externs, ImmutableList.of(source), options);
    return compiler;
}
Also used : Compiler(com.google.javascript.jscomp.Compiler)

Aggregations

Compiler (com.google.javascript.jscomp.Compiler)172 Test (org.junit.Test)132 Node (com.google.javascript.rhino.Node)116 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)50 SourceFile (com.google.javascript.jscomp.SourceFile)22 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)16 NoninjectingCompiler (com.google.javascript.jscomp.testing.NoninjectingCompiler)9 TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)8 Result (com.google.javascript.jscomp.Result)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 File (java.io.File)4 BlackHoleErrorManager (com.google.javascript.jscomp.BlackHoleErrorManager)3 JSError (com.google.javascript.jscomp.JSError)3 InputId (com.google.javascript.rhino.InputId)3 GwtIncompatible (com.google.common.annotations.GwtIncompatible)2 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)2 CompilerInput (com.google.javascript.jscomp.CompilerInput)2 JSChunk (com.google.javascript.jscomp.JSChunk)2 JSModule (com.google.javascript.jscomp.JSModule)2