/// <summary> /// Retrieves the auto prefixer configuration options from the current application configuration. /// </summary> /// <returns> /// The <see cref="AutoPrefixerOptions"/> from the current application configuration. /// </returns> private AutoPrefixerOptions GetAutoPrefixerOptions() { if (this.autoPrefixerOptions != null) { return(this.autoPrefixerOptions); } this.autoPrefixerOptions = new AutoPrefixerOptions { Browsers = this.GetCruncherProcessingSection().AutoPrefixer.Browsers.Split(',').Select(p => HttpUtility.HtmlDecode(p.Trim())).ToList(), Enabled = this.GetCruncherProcessingSection().AutoPrefixer.Enabled, Cascade = this.GetCruncherProcessingSection().AutoPrefixer.Cascade, Safe = this.GetCruncherProcessingSection().AutoPrefixer.Safe }; return(this.autoPrefixerOptions); }
/// <summary> /// Retrieves the auto prefixer configuration options from the current application configuration. /// </summary> /// <returns> /// The <see cref="AutoPrefixerOptions"/> from the current application configuration. /// </returns> private AutoPrefixerOptions GetAutoPrefixerOptions() { if (this.autoPrefixerOptions != null) { return(this.autoPrefixerOptions); } this.autoPrefixerOptions = new AutoPrefixerOptions { Enabled = this.GetCruncherProcessingSection().AutoPrefixer.Enabled, Browsers = this.GetCruncherProcessingSection().AutoPrefixer.Browsers.Split(',').Select(p => HttpUtility.HtmlDecode(p.Trim())).ToList(), Cascade = this.GetCruncherProcessingSection().AutoPrefixer.Cascade, Add = true, Remove = true, Supports = true, Flexbox = true, Grid = true }; return(this.autoPrefixerOptions); }
/// <summary> /// Processes the css request using cruncher and returns the result. /// </summary> /// <param name="context"> /// The current context. /// </param> /// <param name="minify"> /// Whether to minify the output. /// </param> /// <param name="paths"> /// The paths to the resources to crunch. /// </param> /// <returns> /// The <see cref="string"/> representing the processed result. /// </returns> public async Task <string> ProcessCssCrunchAsync(HttpContext context, bool minify, params string[] paths) { string combinedCSS = string.Empty; if (paths != null) { string key = string.Join(string.Empty, paths).ToMd5Fingerprint(); using (await Locker.LockAsync(key)) { combinedCSS = (string)CacheManager.GetItem(key); if (string.IsNullOrWhiteSpace(combinedCSS)) { StringBuilder stringBuilder = new StringBuilder(); CruncherOptions cruncherOptions = new CruncherOptions { MinifyCacheKey = key, Minify = minify, CacheFiles = true, AllowRemoteFiles = CruncherConfiguration.Instance.AllowRemoteDownloads, RemoteFileMaxBytes = CruncherConfiguration.Instance.MaxBytes, RemoteFileTimeout = CruncherConfiguration.Instance.Timeout }; CssCruncher cssCruncher = new CssCruncher(cruncherOptions, context); AutoPrefixerOptions autoPrefixerOptions = CruncherConfiguration.Instance.AutoPrefixerOptions; // Loop through and process each file. foreach (string path in paths) { // Local files. if (PreprocessorManager.Instance.AllowedExtensionsRegex.IsMatch(path)) { List <string> files = new List <string>(); // Try to get the file by absolute/relative path if (!ResourceHelper.IsResourceFilenameOnly(path)) { string cssFilePath = ResourceHelper.GetFilePath( path, cruncherOptions.RootFolder, context); if (File.Exists(cssFilePath)) { files.Add(cssFilePath); } } else { // Get the path from the server. // Loop through each possible directory. foreach (string cssPath in CruncherConfiguration.Instance.CSSPaths) { if (!string.IsNullOrWhiteSpace(cssPath) && cssPath.Trim().StartsWith("~/")) { DirectoryInfo directoryInfo = new DirectoryInfo(context.Server.MapPath(cssPath)); if (directoryInfo.Exists) { IEnumerable <FileInfo> fileInfos = await directoryInfo.EnumerateFilesAsync(path, SearchOption.AllDirectories); files.AddRange(fileInfos.Select(f => f.FullName)); } } } } if (files.Any()) { // We only want the first file. string first = files.FirstOrDefault(); cruncherOptions.RootFolder = Path.GetDirectoryName(first); stringBuilder.Append(await cssCruncher.CrunchAsync(first)); } } else { // Remote files. string remoteFile = this.GetUrlFromToken(path).ToString(); stringBuilder.Append(await cssCruncher.CrunchAsync(remoteFile)); } } combinedCSS = stringBuilder.ToString(); // Apply autoprefixer combinedCSS = cssCruncher.AutoPrefix(combinedCSS, autoPrefixerOptions); if (minify) { combinedCSS = cssCruncher.Minify(combinedCSS); } this.AddItemToCache(key, combinedCSS, cssCruncher.FileMonitors); } } } return(combinedCSS); }
/// <summary> /// Post process the input using auto prefixer. /// </summary> /// <param name="input"> /// The input CSS. /// </param> /// <param name="options"> /// The <see cref="AutoPrefixerOptions"/>. /// </param> /// <returns> /// The <see cref="string"/> containing the post-processed CSS. /// </returns> public string AutoPrefix(string input, AutoPrefixerOptions options) { return(AutoPrefixerPostprocessor.Transform(input, options)); }