diff --git a/src/Nancy.BundleIt/Bundles.cs b/src/Nancy.BundleIt/Bundles.cs index fe85d0b..99198e6 100644 --- a/src/Nancy.BundleIt/Bundles.cs +++ b/src/Nancy.BundleIt/Bundles.cs @@ -38,7 +38,7 @@ public static Bundles Instance internal Dictionary _style_tag_bundles = new Dictionary(); internal Dictionary _script_compressed_bundles = new Dictionary(); internal Dictionary _style_compressed_bundles = new Dictionary(); - + public Bundle AddScripts(string bundlename, List files) { bundlename = bundlename.ToUpper(); @@ -83,7 +83,7 @@ internal string GetBundleTags(string name, eBundleType type) return _style_tag_bundles[n]; } - + return string.Empty; } @@ -130,7 +130,7 @@ void BuildDebugBundles() var bundlefiles = _script_files.Where(f => f.bundle_name == bname).ToList(); bundlestring = new StringBuilder(); LoadDebugBundles(bname, bundlefiles, bundlestring, eBundleType.script); - _script_tag_bundles.Add(bname, bundlestring.ToString()); + AddOrUpdateDictionary(bname, bundlestring.ToString(), _script_tag_bundles); } // styles @@ -140,7 +140,7 @@ void BuildDebugBundles() var bundlefiles = _style_files.Where(f => f.bundle_name == bname).ToList(); bundlestring = new StringBuilder(); LoadDebugBundles(bname, bundlefiles, bundlestring, eBundleType.style); - _style_tag_bundles.Add(bname, bundlestring.ToString()); + AddOrUpdateDictionary(bname, bundlestring.ToString(), _style_tag_bundles); } } @@ -150,7 +150,8 @@ void LoadDebugBundles(string bundlename, List files, StringBuilder foreach (var s in files) { // load referenced bundle files - if(!string.IsNullOrEmpty(s.bundle_ref_name)){ + if (!string.IsNullOrEmpty(s.bundle_ref_name)) + { // get files by bundle name List refbundlefiles = null; @@ -164,14 +165,14 @@ void LoadDebugBundles(string bundlename, List files, StringBuilder } else { - if(type == eBundleType.script) + if (type == eBundleType.script) bundlestring.Append(string.Format(_script_tag_standard_template, s.debugrelativepath) + "\n"); - - if(type == eBundleType.style) + + if (type == eBundleType.style) bundlestring.Append(string.Format(_style_tag_standard_template, s.debugrelativepath) + "\n"); } - + } } @@ -189,10 +190,10 @@ void BuildReleaseBundles() // check for cdn's SplitBundlesWithCDNs(BundleResolvedFiles); - + // generate release bundles GenerateReleaseBundles(BundleResolvedFiles, eBundleType.script); - + // generate bundle tags GenerateReleaseBundleTags(BundleResolvedFiles, eBundleType.script); } @@ -214,7 +215,7 @@ void BuildReleaseBundles() // generate bundle tags GenerateReleaseBundleTags(BundleResolvedFiles, eBundleType.style); } - + } void LoadReleaseBundles(string bundlename, List files, List bundle_resolved_files, eBundleType type) @@ -265,7 +266,7 @@ void SplitBundlesWithCDNs(List BundleResolvedFiles) f.sub_bundle_num = i; prevsub = i; } - + } } @@ -341,7 +342,7 @@ void GenerateReleaseBundleTags(List BundleResolvedFiles, eBundleTy { tags = new StringBuilder(); var unique_resolved_bundles = BundleResolvedFiles.Where(b => b.bundle_name == bundle.bundle_name).DistinctBy(b2 => b2.resolved_bundle_name); - + foreach (var rbundle in unique_resolved_bundles) { if (rbundle.IsCDN) @@ -360,19 +361,20 @@ void GenerateReleaseBundleTags(List BundleResolvedFiles, eBundleTy } if (type == eBundleType.script) - _script_tag_bundles.Add(bundle.bundle_name, tags.ToString()); + AddOrUpdateDictionary(bundle.bundle_name, tags.ToString(), _script_tag_bundles); if (type == eBundleType.style) - _style_tag_bundles.Add(bundle.bundle_name, tags.ToString()); + AddOrUpdateDictionary(bundle.bundle_name, tags.ToString(), _style_tag_bundles); } } string LoadFile(string filepath) { - if(!File.Exists(filepath)){ + if (!File.Exists(filepath)) + { - if(ConfigSettings.Instance.ThrowExceptionWhenFileMissing) + if (ConfigSettings.Instance.ThrowExceptionWhenFileMissing) throw new FileNotFoundException("Could not find file '" + filepath + "'.", filepath); else return string.Empty; @@ -398,7 +400,7 @@ string YUI_JS(string filesource, ConfigSettings.YUICompressionSettings.Js JsSett return jscompressor.Compress(filesource); } - catch(Exception ex) + catch (Exception ex) { var msg = ex.Message; return filesource; @@ -423,6 +425,16 @@ string YUI_CSS(string filesource, ConfigSettings.YUICompressionSettings.CSS CssS } } - + private void AddOrUpdateDictionary(string key, string value, Dictionary dictionary) + { + if (dictionary.ContainsKey(key)) + { + dictionary[key] = value; + } + else + { + dictionary.Add(key, value); + } + } } -} +} \ No newline at end of file