Search in sources :

Example 1 with CompilerOptions

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

the class CompilerUtils method makeOptionsForParsingAndErrorReporting.

/**
   * Create a new compiler options object, with the minimum options for a compiler used either
   * to parse files (in a tolerant way) or to report errors.
   * @return  The new compiler options.
   */
public static CompilerOptions makeOptionsForParsingAndErrorReporting() {
    // These options should remain minimal, because they are used by the stripper.
    CompilerOptions options = new CompilerOptions();
    options.ideMode = true;
    options.setRewriteNewDateGoogNow(false);
    options.setRemoveAbstractMethods(false);
    return options;
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions)

Example 2 with CompilerOptions

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

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

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

the class ClosureCompilerOptions method makeInternal.

// This is based on CommandLineRunner.createOptions() and AbstractCommandLineRunner.setRunOptions()
private static CompilerOptions makeInternal(IReadOnlyStore storeForChecks, IReadOnlyStore storeForCompilationOptions, boolean ideMode) throws CoreException {
    ClosureCompilerLaunchConfigurationRecord record = ClosureCompilerLaunchConfigurationRecord.getInstance();
    // From CommandLineRunner.createOptions()
    CompilerOptions options = new CompilerOptions();
    options.setCodingConvention(new ClosureCodingConvention());
    CompilationLevel level = CompilationLevel.WHITESPACE_ONLY;
    if (storeForCompilationOptions != null) {
        level = record.compilationLevel.get(storeForCompilationOptions);
        level.setOptionsForCompilationLevel(options);
        if (record.generateExports.get(storeForCompilationOptions)) {
            options.setGenerateExports(true);
        }
    }
    WarningLevel wLevel = record.checks.warningLevel.get(storeForChecks);
    wLevel.setOptionsForWarningLevel(options);
    if (storeForCompilationOptions != null) {
        if (record.formattingPrettyPrint.get(storeForCompilationOptions))
            options.prettyPrint = true;
        if (record.formattingPrintInputDelimiter.get(storeForCompilationOptions))
            options.printInputDelimiter = true;
    }
    options.closurePass = record.checks.processClosurePrimitives.get(storeForChecks);
    options.jqueryPass = record.checks.processJQueryPrimitives.get(storeForChecks) && CompilationLevel.ADVANCED_OPTIMIZATIONS == level;
    if (record.checks.processJQueryPrimitives.get(storeForChecks)) {
        options.setCodingConvention(new JqueryCodingConvention());
    }
    /*
    if (!flags.translationsFile.isEmpty()) {
      try {
        options.messageBundle = new XtbMessageBundle(
            new FileInputStream(flags.translationsFile),
            flags.translationsProject);
      } catch (IOException e) {
        throw new RuntimeException("Reading XTB file", e);
      }
    } else if (CompilationLevel.ADVANCED_OPTIMIZATIONS == level) {
      // In SIMPLE or WHITESPACE mode, if the user hasn't specified a
      // translations file, they might reasonably try to write their own
      // implementation of goog.getMsg that makes the substitution at
      // run-time.
      //
      // In ADVANCED mode, goog.getMsg is going to be renamed anyway,
      // so we might as well inline it.
      options.messageBundle = new EmptyMessageBundle();
      
      // From AbstractCommandLineRunner.setRunOptions()
      if (config.warningGuards != null) {
        for (WarningGuardSpec.Entry entry : config.warningGuards.entries) {
          diagnosticGroups.setWarningLevel(options, entry.groupName, entry.level);
        }
      }
      */
    //createDefineOrTweakReplacements(config.define, options, false);
    //options.setTweakProcessing(config.tweakProcessing);
    //createDefineOrTweakReplacements(config.tweak, options, true);
    // Dependency options
    // options.setManageClosureDependencies(false);
    // if (config.closureEntryPoints.size() > 0) {
    //   options.setManageClosureDependencies(config.closureEntryPoints);
    // }
    options.ideMode = ideMode;
    // options.setCodingConvention(config.codingConvention);
    // options.setSummaryDetailLevel(config.summaryDetailLevel);
    // legacyOutputCharset = options.outputCharset = getLegacyOutputCharset();
    // outputCharset2 = getOutputCharset2();
    // inputCharset = getInputCharset();
    // if (config.createSourceMap.length() > 0) {
    //   options.sourceMapOutputPath = config.createSourceMap;
    // }
    // options.sourceMapDetailLevel = config.sourceMapDetailLevel;
    // options.sourceMapFormat = config.sourceMapFormat;
    // if (!config.variableMapInputFile.equals("")) {
    //   options.inputVariableMapSerialized =
    //       VariableMap.load(config.variableMapInputFile).toBytes();
    // }
    // if (!config.propertyMapInputFile.equals("")) {
    //   options.inputPropertyMapSerialized =
    //       VariableMap.load(config.propertyMapInputFile).toBytes();
    // }
    options.setLanguageIn(record.checks.languageIn.get(storeForChecks));
    // if (!config.outputManifests.isEmpty()) {
    //   Set<String> uniqueNames = Sets.newHashSet();
    //   for (String filename : config.outputManifests) {
    //     if (!uniqueNames.add(filename)) {
    //       throw new FlagUsageException("output_manifest flags specify " +
    //           "duplicate file names: " + filename);
    //     }
    //   }
    // }
    // if (!config.outputBundles.isEmpty()) {
    //   Set<String> uniqueNames = Sets.newHashSet();
    //   for (String filename : config.outputBundles) {
    //     if (!uniqueNames.add(filename)) {
    //       throw new FlagUsageException("output_bundle flags specify " +
    //           "duplicate file names: " + filename);
    //     }
    //   }
    // }
    options.setAcceptConstKeyword(record.checks.acceptConstKeyword.get(storeForChecks));
    // options.transformAMDToCJSModules = config.transformAMDToCJSModules;
    // options.processCommonJSModules = config.processCommonJSModules;
    // options.commonJSModulePathPrefix = config.commonJSModulePathPrefix;
    // TODO: Only for ide mode?
    options.setRewriteNewDateGoogNow(false);
    options.setRemoveAbstractMethods(false);
    options.checkTypes = true;
    options.setInferTypes(true);
    options.closurePass = true;
    return options;
}
Also used : JqueryCodingConvention(com.google.javascript.jscomp.JqueryCodingConvention) CompilationLevel(com.google.javascript.jscomp.CompilationLevel) ClosureCodingConvention(com.google.javascript.jscomp.ClosureCodingConvention) WarningLevel(com.google.javascript.jscomp.WarningLevel) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) ClosureCompilerLaunchConfigurationRecord(net.vtst.ow.eclipse.js.closure.launching.compiler.ClosureCompilerLaunchConfigurationRecord)

Example 5 with CompilerOptions

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

the class ClosureBuilder method compileJavaScriptFiles.

// **************************************************************************
// Compilation of JavaScript files
/**
   * Compile a collection of JavaScript files.
   * @param monitor  This method reports one unit of work.
   * @param project  The project the files belong to.
   * @param files  The collection of files to compile.
   * @param force  If true, forces the compilation of all files, even those which are 
   * not modified since their last build.
   * @throws CoreException
   */
private void compileJavaScriptFiles(IProgressMonitor monitor, IProject project, Collection<IFile> files, boolean force) throws CoreException {
    SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, 1);
    int n = files.size(), i = 0;
    subMonitor.beginTask("build_compile", n);
    CompilerOptions options = ClosureCompilerOptions.makeForBuilder(project);
    boolean stripIncludedFiles = getStripIncludedFiles();
    boolean doNotKeepCompilationResultsOfClosedFilesInMemory = getDoNotKeepCompilationResultsOfClosedFilesInMemory();
    boolean doNotCompileClosedFiles = getDoNotCompileClosedFiles();
    for (IFile file : files) {
        ++i;
        boolean fileIsOpen = (editorRegistry.getTextEditor(file) != null);
        Utils.checkCancel(subMonitor);
        monitor.subTask(messages.format("build_compile_file", file.getName()));
        if (fileIsOpen || !doNotCompileClosedFiles) {
            CompilerOptions clonedOptions = i < n ? cloneCompilerOptions(project, options) : options;
            compileJavaScriptFile(file, clonedOptions, fileIsOpen, stripIncludedFiles, doNotKeepCompilationResultsOfClosedFilesInMemory, force);
        }
        subMonitor.worked(1);
    }
    subMonitor.done();
}
Also used : IFile(org.eclipse.core.resources.IFile) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) ClosureCompilerOptions(net.vtst.ow.eclipse.js.closure.compiler.ClosureCompilerOptions) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Aggregations

CompilerOptions (com.google.javascript.jscomp.CompilerOptions)6 Compiler (com.google.javascript.jscomp.Compiler)3 CompilerInput (com.google.javascript.jscomp.CompilerInput)2 JSModule (com.google.javascript.jscomp.JSModule)2 JSSourceFile (com.google.javascript.jscomp.JSSourceFile)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ClosureCompilerOptions (net.vtst.ow.eclipse.js.closure.compiler.ClosureCompilerOptions)2 IFile (org.eclipse.core.resources.IFile)2 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)1 ClosureCodingConvention (com.google.javascript.jscomp.ClosureCodingConvention)1 CompilationLevel (com.google.javascript.jscomp.CompilationLevel)1 JqueryCodingConvention (com.google.javascript.jscomp.JqueryCodingConvention)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 WarningLevel (com.google.javascript.jscomp.WarningLevel)1 CircularDependencyException (com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 IReadOnlyStore (net.vtst.eclipse.easy.ui.properties.stores.IReadOnlyStore)1 LaunchConfigurationReadOnlyStore (net.vtst.eclipse.easy.ui.properties.stores.LaunchConfigurationReadOnlyStore)1