diff --git a/src/Smidge.Core/BundleManager.cs b/src/Smidge.Core/BundleManager.cs index 202b924..be30930 100644 --- a/src/Smidge.Core/BundleManager.cs +++ b/src/Smidge.Core/BundleManager.cs @@ -133,9 +133,7 @@ public bool TryGetValue(string key, out Bundle value) /// /// public IEnumerable GetBundleNames(WebFileType type) - { - return _bundles.Where(x => x.Value.Files.Any(f => f.DependencyType == type)).Select(x => x.Key); - } + => _bundles.Where(x => x.Value.Files.Any(f => f.DependencyType == type)).Select(x => x.Key); /// /// Returns all bundles registered @@ -143,19 +141,14 @@ public IEnumerable GetBundleNames(WebFileType type) /// /// public IEnumerable GetBundles(WebFileType type) - { - return _bundles.Where(x => x.Value.Files.Any(f => f.DependencyType == type)).Select(x => x.Value); - } + => _bundles.Where(x => x.Value.Files.Any(f => f.DependencyType == type)).Select(x => x.Value); /// /// Checks if the bundle exists by name /// /// /// - public bool Exists(string bundleName) - { - return TryGetValue(bundleName, out _); - } + public bool Exists(string bundleName) => TryGetValue(bundleName, out _); /// /// Adds an item to the bundle, if the bundle doesn't exist it will be created @@ -164,8 +157,7 @@ public bool Exists(string bundleName) /// public void AddToBundle(string bundleName, CssFile file) { - Bundle collection; - if (TryGetValue(bundleName, out collection)) + if (TryGetValue(bundleName, out Bundle collection)) { _logger.LogDebug($"Adding {WebFileType.Css} file '{file.FilePath}' to bundle '{bundleName}'"); collection.Files.Add(file); @@ -183,8 +175,7 @@ public void AddToBundle(string bundleName, CssFile file) /// public void AddToBundle(string bundleName, JavaScriptFile file) { - Bundle collection; - if (TryGetValue(bundleName, out collection)) + if (TryGetValue(bundleName, out Bundle collection)) { _logger.LogDebug($"Adding {WebFileType.Js} file '{file.FilePath}' to bundle '{bundleName}'"); collection.Files.Add(file); diff --git a/src/Smidge/SmidgeRequire.cs b/src/Smidge/SmidgeRequire.cs index 5598964..dc063ea 100644 --- a/src/Smidge/SmidgeRequire.cs +++ b/src/Smidge/SmidgeRequire.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Smidge.Models; namespace Smidge @@ -23,10 +24,20 @@ public SmidgeRequire(string bundleName, IBundleManager bundleManager, WebFileTyp public ISmidgeRequire RequiresJs(JavaScriptFile file) { + if (file is null) + { + throw new ArgumentNullException(nameof(file)); + } + if (_type == WebFileType.Css) + { throw new InvalidOperationException("Cannot add css file to a js bundle"); + } + if (_requestHelper.IsExternalRequestPath(file.FilePath)) + { throw new InvalidOperationException("Cannot process an external file as part of a bundle"); + } _bundleManager.AddToBundle(_bundleName, file); return this; @@ -35,12 +46,17 @@ public ISmidgeRequire RequiresJs(JavaScriptFile file) public ISmidgeRequire RequiresJs(params string[] paths) { if (_type == WebFileType.Css) + { throw new InvalidOperationException("Cannot add css file to a js bundle"); + } - foreach (var path in paths) + foreach (var path in paths.Where(p => !string.IsNullOrWhiteSpace(p))) { if (_requestHelper.IsExternalRequestPath(path)) + { throw new InvalidOperationException("Cannot process an external file as part of a bundle"); + } + _bundleManager.AddToBundle(_bundleName, new JavaScriptFile(path)); } return this; @@ -48,10 +64,21 @@ public ISmidgeRequire RequiresJs(params string[] paths) public ISmidgeRequire RequiresCss(CssFile file) { + if (file is null) + { + throw new ArgumentNullException(nameof(file)); + } + if (_type == WebFileType.Js) + { throw new InvalidOperationException("Cannot add js file to a css bundle"); + } + if (_requestHelper.IsExternalRequestPath(file.FilePath)) + { throw new InvalidOperationException("Cannot process an external file as part of a bundle"); + } + _bundleManager.AddToBundle(_bundleName, file); return this; } @@ -59,15 +86,20 @@ public ISmidgeRequire RequiresCss(CssFile file) public ISmidgeRequire RequiresCss(params string[] paths) { if (_type == WebFileType.Js) + { throw new InvalidOperationException("Cannot add js file to a css bundle"); + } - foreach (var path in paths) + foreach (var path in paths.Where(p => !string.IsNullOrWhiteSpace(p))) { if (_requestHelper.IsExternalRequestPath(path)) + { throw new InvalidOperationException("Cannot process an external file as part of a bundle"); + } + _bundleManager.AddToBundle(_bundleName, new CssFile(path)); } return this; } } -} \ No newline at end of file +}