public void Reset() { lock (sync) { isEnabled = false; var settings = options.Value; var bundles = settings.Bundles ?? new Dictionary <string, string[]>(); bundles = bundles.Keys.ToDictionary(k => k, k => (bundles[k] ?? Array.Empty <string>()) .Select(u => BundleUtils.DoReplacements(u, settings.Replacements)) .Where(u => !string.IsNullOrEmpty(u)) .ToArray()); bundleIncludes = BundleUtils.ExpandBundleIncludes(bundles, "dynamic://CssBundle.", "css"); if (bundles.Count == 0 || settings.Enabled != true) { bundleKeyBySourceUrl = null; bundleKeysBySourceUrl = null; bundleKeys = null; return; } bundleKeyBySourceUrl = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); bundleKeysBySourceUrl = new Dictionary <string, HashSet <string> >(StringComparer.OrdinalIgnoreCase); bundleKeys = new HashSet <string>(StringComparer.OrdinalIgnoreCase); bool minimize = settings.Minimize == true; var noMinimize = new HashSet <string>(StringComparer.OrdinalIgnoreCase); if (settings.NoMinimize != null) { noMinimize.AddRange(settings.NoMinimize); } foreach (var pair in bundles) { var sourceFiles = pair.Value; if (sourceFiles == null || sourceFiles.Length == 0) { continue; } var bundleKey = pair.Key; var bundleName = "CssBundle." + bundleKey; var bundleParts = new List <Func <string> >(); var scriptNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase); void registerInBundle(string sourceFile) { if (bundleKey.IndexOf('/', StringComparison.Ordinal) < 0 && !bundleKeyBySourceUrl.ContainsKey(sourceFile)) { bundleKeyBySourceUrl[sourceFile] = bundleKey; } if (!bundleKeysBySourceUrl.TryGetValue(sourceFile, out HashSet <string> bundleKeys)) { bundleKeys = new HashSet <string>(StringComparer.OrdinalIgnoreCase); bundleKeysBySourceUrl[sourceFile] = new HashSet <string>(); } bundleKeys.Add(bundleKey); } foreach (var sourceFile in sourceFiles) { if (sourceFile.IsNullOrEmpty()) { continue; } if (sourceFile.StartsWith("dynamic://", StringComparison.OrdinalIgnoreCase)) { registerInBundle(sourceFile); var scriptName = sourceFile[10..];
public CssBundleManager() { var settings = Config.Get <CssBundlingSettings>(); var bundles = JsonConfigHelper.LoadConfig <Dictionary <string, string[]> >( HostingEnvironment.MapPath("~/Content/site/CssBundles.json")); bundles = bundles.Keys.ToDictionary(k => k, k => (bundles[k] ?? new string[0]) .Select(u => BundleUtils.DoReplacements(u, settings.Replacements)) .Where(u => !string.IsNullOrEmpty(u)) .ToArray()); bundleIncludes = BundleUtils.ExpandBundleIncludes(bundles, "dynamic://CssBundle.", "css"); if (bundles.Count == 0 || settings.Enabled != true) { return; } bundleKeyBySourceUrl = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); bundleKeysBySourceUrl = new Dictionary <string, HashSet <string> >(StringComparer.OrdinalIgnoreCase); bundleByKey = new Dictionary <string, ConcatenatedScript>(StringComparer.OrdinalIgnoreCase); bool minimize = settings.Minimize == true; var noMinimize = new HashSet <string>(StringComparer.OrdinalIgnoreCase); if (settings.NoMinimize != null) { noMinimize.AddRange(settings.NoMinimize); } foreach (var pair in bundles) { var sourceFiles = pair.Value; if (sourceFiles == null || sourceFiles.Length == 0) { continue; } var bundleKey = pair.Key; var bundleName = "CssBundle." + bundleKey; var bundleParts = new List <Func <string> >(); var scriptNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase); Action <string> registerInBundle = delegate(string sourceFile) { if (bundleKey.IndexOf('/') < 0 && !bundleKeyBySourceUrl.ContainsKey(sourceFile)) { bundleKeyBySourceUrl[sourceFile] = bundleKey; } HashSet <string> bundleKeys; if (!bundleKeysBySourceUrl.TryGetValue(sourceFile, out bundleKeys)) { bundleKeys = new HashSet <string>(StringComparer.OrdinalIgnoreCase); bundleKeysBySourceUrl[sourceFile] = new HashSet <string>(); } bundleKeys.Add(bundleKey); }; foreach (var sourceFile in sourceFiles) { if (sourceFile.IsNullOrEmpty()) { continue; } if (sourceFile.StartsWith("dynamic://", StringComparison.OrdinalIgnoreCase)) { registerInBundle(sourceFile); var scriptName = sourceFile.Substring(10); scriptNames.Add(scriptName); bundleParts.Add(() => { if (recursionCheck != null) { if (recursionCheck.Contains(scriptName) || recursionCheck.Count > 100) { return(String.Format(errorLines, String.Format("Caught infinite recursion with dynamic scripts '{0}'!", String.Join(", ", recursionCheck)))); } } else { recursionCheck = new HashSet <string>(StringComparer.OrdinalIgnoreCase); } recursionCheck.Add(scriptName); try { var code = DynamicScriptManager.GetScriptText(scriptName); if (code == null) { return(String.Format(errorLines, String.Format("Dynamic script with name '{0}' is not found!", scriptName))); } if (minimize && !scriptName.StartsWith("Bundle.", StringComparison.OrdinalIgnoreCase)) { try { var result = NUglify.Uglify.Css(code); if (!result.HasErrors) { code = result.Code; } } catch (Exception ex) { ex.Log(); } } var scriptUrl = VirtualPathUtility.ToAbsolute("~/DynJS.axd/" + scriptName); return(RewriteUrlsToAbsolute(scriptUrl, code)); } finally { recursionCheck.Remove(scriptName); } }); continue; } string sourceUrl = BundleUtils.ExpandVersionVariable(sourceFile); sourceUrl = VirtualPathUtility.ToAbsolute(sourceUrl); if (sourceUrl.IsNullOrEmpty()) { continue; } registerInBundle(sourceUrl); bundleParts.Add(() => { var sourcePath = HostingEnvironment.MapPath(sourceUrl); if (!File.Exists(sourcePath)) { return(String.Format(errorLines, String.Format("File {0} is not found!", sourcePath))); } string code = null; if (minimize && !noMinimize.Contains(sourceFile) && !sourceFile.EndsWith(".min.css", StringComparison.OrdinalIgnoreCase)) { if (settings.UseMinCSS == true) { var minPath = Path.ChangeExtension(sourcePath, ".min.css"); if (File.Exists(minPath)) { sourcePath = minPath; using (StreamReader sr = new StreamReader(File.OpenRead(sourcePath))) code = sr.ReadToEnd(); } } if (code == null) { using (StreamReader sr = new StreamReader(File.OpenRead(sourcePath))) code = sr.ReadToEnd(); try { var result = NUglify.Uglify.Css(code); if (!result.HasErrors) { code = result.Code; } } catch (Exception ex) { ex.Log(); } } } else { using (StreamReader sr = new StreamReader(File.OpenRead(sourcePath))) code = sr.ReadToEnd(); } code = RewriteUrlsToAbsolute(sourceUrl, code); return(code); }); } var bundle = new ConcatenatedScript(bundleParts, "\n\n", checkRights: () => { foreach (var scriptName in scriptNames) { if (recursionCheck != null) { if (recursionCheck.Contains(scriptName) || recursionCheck.Count > 100) { throw new InvalidOperationException(String.Format( "Caught infinite recursion with dynamic scripts '{0}'!", String.Join(", ", recursionCheck))); } } else { recursionCheck = new HashSet <string>(StringComparer.OrdinalIgnoreCase); } recursionCheck.Add(scriptName); try { DynamicScriptManager.CheckScriptRights(scriptName); } finally { recursionCheck.Remove(scriptName); } } }); DynamicScriptManager.Register(bundleName, bundle); bundleByKey[bundleKey] = bundle; } isEnabled = true; }