public ScriptBundleManager() { var settings = Config.Get <ScriptBundlingSettings>(); var bundles = JsonConfigHelper.LoadConfig <Dictionary <string, string[]> >( HostingEnvironment.MapPath("~/Scripts/site/ScriptBundles.json")); bundleIncludes = BundleUtils.ExpandBundleIncludes(bundles, "dynamic://Bundle.", "script"); 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 = "Bundle." + 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.Js(code); if (!result.HasErrors) { code = result.Code; } } catch (Exception ex) { ex.Log(); } } return(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))); } if (minimize && !noMinimize.Contains(sourceFile)) { if (settings.UseMinJS == true) { var minPath = Path.ChangeExtension(sourcePath, ".min.js"); if (File.Exists(minPath)) { sourcePath = minPath; using (StreamReader sr = new StreamReader(File.OpenRead(sourcePath))) return(sr.ReadToEnd()); } } string code; using (StreamReader sr = new StreamReader(File.OpenRead(sourcePath))) code = sr.ReadToEnd(); try { var result = NUglify.Uglify.Js(code); if (result.HasErrors) { return(code); } return(result.Code); } catch (Exception ex) { ex.Log(); return(code); } } using (StreamReader sr = new StreamReader(File.OpenRead(sourcePath))) return(sr.ReadToEnd()); }); } var bundle = new ConcatenatedScript(bundleParts, 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; }
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..];