Search in sources :

Example 1 with BasicErrorManager

use of com.google.javascript.jscomp.BasicErrorManager 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

BasicErrorManager (com.google.javascript.jscomp.BasicErrorManager)1 CheckLevel (com.google.javascript.jscomp.CheckLevel)1 CompilationLevel (com.google.javascript.jscomp.CompilationLevel)1 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)1 JSError (com.google.javascript.jscomp.JSError)1 SecurityContext (org.structr.common.SecurityContext)1 PropertyMap (org.structr.core.property.PropertyMap)1