Example #1
0
        private RequireBundles GetBundles(JObject document)
        {
            var bundles = new RequireBundles();
            bundles.BundleEntries = new List<RequireBundle>();
            if (document != null && document["bundles"] != null)
            {
                bundles.BundleEntries = document["bundles"].Select(
                    r =>
                        {
                            var bundle = new RequireBundle();
                            var prop = (JProperty)r;
                            bundle.Name = prop.Name;
                            if (prop.Value is JArray)
                            {
                                bundle.IsVirtual = false;
                                var arr = prop.Value as JArray;
                                bundle.BundleItems = arr.Select(x => new BundleItem { ModuleName = x.ToString() } ).ToList();    
                                
                            }
                            else if(prop.Value is JObject)
                            {
                                var obj = prop.Value as JObject;
                                bundle.OutputPath = obj["outputPath"] != null ? obj["outputPath"].ToString() : null;
                                var inclArr = obj["includes"] as JArray;
                                if (inclArr != null)
                                {
                                    bundle.Includes = inclArr.Select(x => x.ToString()).ToList();
                                }

                                var itemsArr = obj["items"] as JArray;
                                if (itemsArr != null)
                                {
                                    bundle.BundleItems = itemsArr.Select(
                                        x =>
                                            {
                                                var result = new BundleItem();
                                                if (x is JObject)
                                                {
                                                    result.ModuleName = x["path"] != null ? x["path"].ToString() : null;
                                                    result.CompressionType = x["compression"] != null ? x["compression"].ToString() : null;
                                                }
                                                else if (x is JValue && x.Type == JTokenType.String)
                                                {
                                                    result.ModuleName = x.ToString();
                                                }

                                                return result;
                                            }).ToList();
                                }

                                var isVirtual = obj["virtual"];
                                bundle.IsVirtual = isVirtual is JValue 
                                                    && isVirtual.Type == JTokenType.Boolean
                                                    && (bool)((JValue)isVirtual).Value;
                            }

                            return bundle;
                        })
                    .ToList();
            }

            return bundles;
        }
        public void ReadBundleWithOutputPath()
        {
            var config = ReadJson(new TestFileReader());
            var bundle = new RequireBundle { Name = "jqueryBundle", OutputPath = @"Bundles\full.min.js" };
            var expected = ConfigurationCreators.CreateCollectionWithBundles(bundle);

            CustomAssert.JsonEquals(expected, config);
        }
        public void ReadBundleWithSpecifiedVirtual()
        {
            var config = ReadJson(new TestFileReader());
            var bundle = new RequireBundle { Name = "jqueryBundle", IsVirtual = true };
            var expected = ConfigurationCreators.CreateCollectionWithBundles(bundle);

            CustomAssert.JsonEquals(expected, config);
        }
        public void ReadBundleWithExpandedItems()
        {
            var config = ReadJson(new TestFileReader());
            var bundle = new RequireBundle
                             {
                                 Name = "full",
                                 BundleItems =
                                     new List<BundleItem>
                                         {
                                             new BundleItem
                                                 {
                                                     ModuleName = "bootstrap",
                                                     CompressionType = "none"
                                                 },
                                             new BundleItem
                                                 {
                                                     ModuleName = "amplify",
                                                     CompressionType = "standard"
                                                 }
                                         }
                             };
            var expected = ConfigurationCreators.CreateCollectionWithBundles(bundle);

            CustomAssert.JsonEquals(expected, config);
        }
        public void ReadBundleWithIncludes()
        {
            var config = ReadJson(new TestFileReader());
            var bundle = new RequireBundle
                             {
                                 Name = "full",
                                 Includes = new List<string>
                                                {
                                                    "jqueryBundle", "jqvalUnobtrusive", "jqValidate"
                                                }
                             };
            var expected = ConfigurationCreators.CreateCollectionWithBundles(bundle);

            CustomAssert.JsonEquals(expected, config);
        }
        public void ReadBundleWithCompactedItems()
        {
            var config = ReadJson(new TestFileReader());
            var bundle = new RequireBundle
                             {
                                 Name = "jqueryBundle",
                                 BundleItems =
                                     new List<BundleItem>
                                         {
                                             new BundleItem
                                                 {
                                                     ModuleName = "jquery"
                                                 }
                                         }
                             };
            var expected = ConfigurationCreators.CreateCollectionWithBundles(bundle);

            CustomAssert.JsonEquals(expected, config);
        }
        public void UnifyBundleItemsForSameId()
        {
            var bundleA = new RequireBundle
                              {
                                  Name = "bundleA",
                                  BundleItems = new List<BundleItem> { new BundleItem { ModuleName = "jquery" } }
                              };
            var bundleB = new RequireBundle()
                              {
                                  Name = "bundleA",
                                  BundleItems = new List<BundleItem> { new BundleItem { ModuleName = "amplify" } }
                              };

            var firstConfig = ConfigurationCreators.CreateCollectionWithBundles(bundleA);
            var secondConfig = ConfigurationCreators.CreateCollectionWithBundles(bundleB);

            var merger = ConfigurationCreators.CreateBundleProcessingConfigMerger(firstConfig, secondConfig);
            var merged = merger.GetMerged();

            var expected = ConfigurationCreators.CreateEmptyCollection();
            expected.Bundles.BundleEntries = new List<RequireBundle>
                                                 {
                                                     new RequireBundle
                                                         {
                                                             Name = "bundleA",
                                                             ParsedIncludes = true,
                                                             BundleItems = new List<BundleItem>
                                                                               {
                                                                                   new BundleItem { ModuleName = "jquery", RelativePath = "jquery"},
                                                                                   new BundleItem { ModuleName = "amplify", RelativePath = "amplify" }
                                                                               }
                                                         }
                                                 };

            CustomAssert.JsonEquals(expected, merged);
        }
        public void SetVirtualToFalseForResultingBundleIfAnyHasFalse()
        {
            var bundleA = new RequireBundle()
            {
                Name = "bundleA",
                IsVirtual = true
            };
            var secondBundleA = new RequireBundle
            {
                Name = "bundleA",
                IsVirtual = false
            };

            var firstConfig = ConfigurationCreators.CreateCollectionWithBundles(bundleA);
            var secondConfig = ConfigurationCreators.CreateCollectionWithBundles(secondBundleA);

            var merger = ConfigurationCreators.CreateBundleProcessingConfigMerger(firstConfig, secondConfig);
            var merged = merger.GetMerged();

            var expected = ConfigurationCreators.CreateEmptyCollection();
            expected.Bundles.BundleEntries = new List<RequireBundle>
                                                 {
                                                     new RequireBundle
                                                         {
                                                             Name = "bundleA",
                                                             ParsedIncludes = true,
                                                             IsVirtual = false
                                                         }
                                                 };

            CustomAssert.JsonEquals(expected, merged);
        }
        public void OverrideOutputPathForSameId()
        {
            var bundleA = new RequireBundle()
            {
                Name = "bundleA",
                OutputPath = "bundleA"
            };
            var secondBundleA = new RequireBundle
            {
                Name = "bundleA",
                OutputPath = "bundleAgain"
            };

            var firstConfig = ConfigurationCreators.CreateCollectionWithBundles(bundleA);
            var secondConfig = ConfigurationCreators.CreateCollectionWithBundles(secondBundleA);

            var merger = ConfigurationCreators.CreateBundleProcessingConfigMerger(firstConfig, secondConfig);
            var merged = merger.GetMerged();

            var expected = ConfigurationCreators.CreateEmptyCollection();
            expected.Bundles.BundleEntries = new List<RequireBundle>
                                                 {
                                                     new RequireBundle
                                                         {
                                                             Name = "bundleA",
                                                             ParsedIncludes = true,
                                                             OutputPath = "bundleAgain"
                                                         }
                                                 };

            CustomAssert.JsonEquals(expected, merged);
        }
        public void CreateSingleBundleListForDifferentIds()
        {
            var bundleA = new RequireBundle
                              {
                                  Name = "bundleA",
                              };
            var bundleB = new RequireBundle()
                              {
                                  Name = "bundleB",
                              };

            var firstConfig = ConfigurationCreators.CreateCollectionWithBundles(bundleA);
            var secondConfig = ConfigurationCreators.CreateCollectionWithBundles(bundleB);

            var merger = ConfigurationCreators.CreateBundleProcessingConfigMerger(firstConfig, secondConfig);
            var merged = merger.GetMerged();

            var expected = ConfigurationCreators.CreateCollectionWithBundles(bundleA, bundleB);

            CustomAssert.JsonEquals(expected, merged);
        }