public CompressorResults Compress(string js, string filename, ClosureCompilerJsCompressionOptions packagerOptions)
        {
            var compressorResult = new CompressorResults();
            var options = new CompilerOptions();
            CompilationLevel compilationLevel;
            switch (packagerOptions.CompressionLevel)
            {
                case ClosureCompressionLevel.SimpleOptimizations:
                    compilationLevel = CompilationLevel.SIMPLE_OPTIMIZATIONS;
                    break;
                case ClosureCompressionLevel.AdvancedOptimizations:
                    compilationLevel = CompilationLevel.ADVANCED_OPTIMIZATIONS;
                    break;
                default:
                    compilationLevel = CompilationLevel.WHITESPACE_ONLY;
                    break;
            }
            compilationLevel.setOptionsForCompilationLevel(options);

            var compiler = new Compiler();
            var dummy = JSSourceFile.fromCode("externs.js", "");
            var source = JSSourceFile.fromCode(filename, js);

            var result = compiler.compile(dummy, source, options);

            if (!result.success && packagerOptions.FailOnCompilerErrors)
            {
                if (packagerOptions.FailOnCompilerErrors)
                {
                    throw new ClosureCompilerErrorsException(result.warnings);
                }
                compressorResult.Errors = (from error in result.errors
                                           select new PackagerResultDetail { Line = error.lineNumber, Char = error.getCharno(), Description = error.description, Filename = error.sourceName }).ToList();
            }
            if (result.warnings.Length > 0)
            {
                if (packagerOptions.FailOnCompilerWarnings)
                {
                    throw new ClosureCompilerWarningsException(result.warnings);
                }
                compressorResult.Warnings = (from warning in result.warnings
                                            select new PackagerResultDetail { Line = warning.lineNumber, Char = warning.getCharno(), Description = warning.description, Filename = warning.sourceName }).ToList();
            }

            compressorResult.Output = compiler.toSource();
            return compressorResult;
        }
예제 #2
0
        public CompressorResults Compress(string css, string cssPath)
        {
            var result = new CompressorResults();
            var modifiedCss = "";
            var lastMatch = 0;

            foreach (Match match in EMBED_DETECTOR.Matches(css))
            {
                var path = match.Groups[1];
                if (path.Value.StartsWith("http"))
                    continue; //ignore any non-local images
                var replacementString = _options.GetCdnImagePath(Utils.GetAbsoluteUrlFromPhysicalPath(Utils.GetPhysicalPathFromUrl(path.Value, cssPath, _options.SiteRoot), _options.SiteRoot));
                if (!string.IsNullOrEmpty(replacementString))
                {
                    replacementString = " url('" + replacementString + "')";
                    modifiedCss += css.Substring(lastMatch, match.Index - lastMatch - 1) + replacementString;
                    lastMatch = match.Index + match.Length;
                }
            }
            modifiedCss += css.Substring(lastMatch, css.Length - lastMatch);
            result.Output = modifiedCss;
            return result;
        }