Search in sources :

Example 1 with CheckLevel

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

the class CompileTask method createCompilerOptions.

private CompilerOptions createCompilerOptions() {
    CompilerOptions options = new CompilerOptions();
    this.compilationLevel.setOptionsForCompilationLevel(options);
    if (this.debugOptions) {
        this.compilationLevel.setDebugOptionsForCompilationLevel(options);
    }
    options.setEnvironment(this.environment);
    options.setPrettyPrint(this.prettyPrint);
    options.setPrintInputDelimiter(this.printInputDelimiter);
    options.setPreferSingleQuotes(this.preferSingleQuotes);
    options.setGenerateExports(this.generateExports);
    options.setLanguageIn(this.languageIn);
    options.setLanguageOut(this.languageOut);
    options.setOutputCharset(this.outputEncoding);
    this.warningLevel.setOptionsForWarningLevel(options);
    options.setManageClosureDependencies(manageDependencies);
    convertEntryPointParameters(options);
    options.setTrustedStrings(true);
    options.setAngularPass(angularPass);
    if (replaceProperties) {
        convertPropertiesMap(options);
    }
    convertDefineParameters(options);
    for (Warning warning : warnings) {
        CheckLevel level = warning.getLevel();
        String groupName = warning.getGroup();
        DiagnosticGroup group = new DiagnosticGroups().forName(groupName);
        if (group == null) {
            throw new BuildException("Unrecognized 'warning' option value (" + groupName + ")");
        }
        options.setWarningLevel(group, level);
    }
    if (!Strings.isNullOrEmpty(sourceMapFormat)) {
        options.setSourceMapFormat(Format.valueOf(sourceMapFormat));
    }
    if (!Strings.isNullOrEmpty(sourceMapLocationMapping)) {
        String[] tokens = sourceMapLocationMapping.split("\\|", -1);
        LocationMapping lm = new LocationMapping(tokens[0], tokens[1]);
        options.setSourceMapLocationMappings(Arrays.asList(lm));
    }
    options.setApplyInputSourceMaps(applyInputSourceMaps);
    if (sourceMapOutputFile != null) {
        File parentFile = sourceMapOutputFile.getParentFile();
        if (parentFile.mkdirs()) {
            log("Created missing parent directory " + parentFile, Project.MSG_DEBUG);
        }
        options.setSourceMapOutputPath(parentFile.getAbsolutePath());
    }
    return options;
}
Also used : LocationMapping(com.google.javascript.jscomp.SourceMap.LocationMapping) CheckLevel(com.google.javascript.jscomp.CheckLevel) DiagnosticGroup(com.google.javascript.jscomp.DiagnosticGroup) CompilerOptions(com.google.javascript.jscomp.CompilerOptions) BuildException(org.apache.tools.ant.BuildException) SourceFile(com.google.javascript.jscomp.SourceFile) File(java.io.File) DiagnosticGroups(com.google.javascript.jscomp.DiagnosticGroups)

Example 2 with CheckLevel

use of com.google.javascript.jscomp.CheckLevel in project structr by structr.

the class MinifiedJavaScriptFile method minify.

static void minify(final MinifiedJavaScriptFile thisFile) throws FrameworkException, IOException {
    logger.info("Running minify: {}", thisFile.getUuid());
    final com.google.javascript.jscomp.Compiler compiler = new com.google.javascript.jscomp.Compiler();
    final SecurityContext securityContext = thisFile.getSecurityContext();
    final CompilerOptions options = new CompilerOptions();
    final CompilationLevel selectedLevel = CompilationLevel.valueOf(thisFile.getOptimizationLevel());
    selectedLevel.setOptionsForCompilationLevel(options);
    compiler.setErrorManager(new BasicErrorManager() {

        @Override
        public void println(final CheckLevel level, final JSError error) {
        }

        @Override
        protected void printSummary() {
            if (getTypedPercent() > 0) {
                if (getErrorCount() + getWarningCount() == 0) {
                    logger.info(SimpleFormat.format("%d error(s), %d warning(s), %.1f%% typed", getErrorCount(), getWarningCount(), getTypedPercent()));
                } else {
                    logger.warn(SimpleFormat.format("%d error(s), %d warning(s), %.1f%% typed", getErrorCount(), getWarningCount(), getTypedPercent()));
                }
            } else if (getErrorCount() + getWarningCount() > 0) {
                logger.warn(SimpleFormat.format("%d error(s), %d warning(s)", getErrorCount(), getWarningCount()));
            }
        }
    });
    compiler.compile(CommandLineRunner.getBuiltinExterns(options.getEnvironment()), MinifiedJavaScriptFile.getSourceFileList(thisFile), options);
    FileHelper.setFileData(thisFile, compiler.toSource().getBytes(), thisFile.getContentType());
    final PropertyMap changedProperties = new PropertyMap();
    changedProperties.put(StructrApp.key(MinifiedJavaScriptFile.class, "warnings"), StringUtils.join(compiler.getWarnings(), System.lineSeparator()));
    changedProperties.put(StructrApp.key(MinifiedJavaScriptFile.class, "errors"), StringUtils.join(compiler.getErrors(), System.lineSeparator()));
    thisFile.setProperties(securityContext, changedProperties);
}
Also used : CompilationLevel(com.google.javascript.jscomp.CompilationLevel) CheckLevel(com.google.javascript.jscomp.CheckLevel) JSError(com.google.javascript.jscomp.JSError) PropertyMap(org.structr.core.property.PropertyMap) BasicErrorManager(com.google.javascript.jscomp.BasicErrorManager) SecurityContext(org.structr.common.SecurityContext) CompilerOptions(com.google.javascript.jscomp.CompilerOptions)

Aggregations

CheckLevel (com.google.javascript.jscomp.CheckLevel)2 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)2 BasicErrorManager (com.google.javascript.jscomp.BasicErrorManager)1 CompilationLevel (com.google.javascript.jscomp.CompilationLevel)1 DiagnosticGroup (com.google.javascript.jscomp.DiagnosticGroup)1 DiagnosticGroups (com.google.javascript.jscomp.DiagnosticGroups)1 JSError (com.google.javascript.jscomp.JSError)1 SourceFile (com.google.javascript.jscomp.SourceFile)1 LocationMapping (com.google.javascript.jscomp.SourceMap.LocationMapping)1 File (java.io.File)1 BuildException (org.apache.tools.ant.BuildException)1 SecurityContext (org.structr.common.SecurityContext)1 PropertyMap (org.structr.core.property.PropertyMap)1