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;
}
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());
}
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));
}
}
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;
}
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();
}
Aggregations