From 9bc219e4fa68a8b305dbdcc424086f3fd303d80a Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Wed, 13 Jun 2018 17:03:12 +0300 Subject: [PATCH] Created BundlingOptions.Mode. --- .../Mvc/UI/Bundling/BundleManager.cs | 41 +++++++++++++++++-- .../AspNetCore/Mvc/UI/Bundling/BundlerBase.cs | 11 +++-- .../Mvc/UI/Bundling/BundlerContext.cs | 5 ++- .../Mvc/UI/Bundling/BundlingMode.cs | 27 ++++++++++++ .../Mvc/UI/Bundling/BundlingOptions.cs | 5 +++ .../Mvc/UI/Bundling/IBundlerContext.cs | 2 + .../System/StringExtensions_Tests.cs | 4 +- 7 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingMode.cs diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs index 99b265ad9a..3171b09056 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs @@ -63,7 +63,13 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling return files; } - var bundleResult = bundler.Bundle(new BundlerContext(bundleRelativePath, files)); + var bundleResult = bundler.Bundle( + new BundlerContext( + bundleRelativePath, + files, + IsMinficationEnabled() + ) + ); SaveBundleResult(bundleRelativePath, bundleResult); @@ -86,6 +92,9 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling fileName ) ); + + //TODO: Saving to file option? + //var bundleFilePath = Path.Combine(_hostingEnvironment.WebRootPath, bundleRelativePath); //DirectoryHelper.CreateIfNotExists(Path.GetDirectoryName(bundleFilePath)); //File.WriteAllText(bundleFilePath, bundleResult.Content, Encoding.UTF8); @@ -103,8 +112,34 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling protected virtual bool IsBundlingEnabled() { - return true; - //return !_hostingEnvironment.IsDevelopment(); + switch (_options.Mode) + { + case BundlingMode.None: + return false; + case BundlingMode.Bundle: + case BundlingMode.BundleAndMinify: + return true; + case BundlingMode.Auto: + return !_hostingEnvironment.IsDevelopment(); + default: + throw new AbpException($"Unhandled {nameof(BundlingMode)}: {_options.Mode}"); + } + } + + protected virtual bool IsMinficationEnabled() + { + switch (_options.Mode) + { + case BundlingMode.None: + case BundlingMode.Bundle: + return false; + case BundlingMode.BundleAndMinify: + return true; + case BundlingMode.Auto: + return !_hostingEnvironment.IsDevelopment(); + default: + throw new AbpException($"Unhandled {nameof(BundlingMode)}: {_options.Mode}"); + } } protected virtual List CreateFileList(BundleConfigurationCollection bundles, string bundleName) diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs index 05c7b7d1b9..3fb2316e1e 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs @@ -28,9 +28,14 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling sb.AppendLine(GetFileContent(context, file)); } - return new BundleResult( - Minifier.Minify(sb.ToString(), context.BundleRelativePath) - ); + var bundleContent = sb.ToString(); + + if (context.IsMinificationEnabled) + { + bundleContent = Minifier.Minify(bundleContent, context.BundleRelativePath); + } + + return new BundleResult(bundleContent); } protected virtual string GetFileContent(IBundlerContext context, string file) diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerContext.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerContext.cs index 7af5cda1cd..8be57a6344 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerContext.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerContext.cs @@ -8,10 +8,13 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling public IReadOnlyList ContentFiles { get; } - public BundlerContext(string bundleRelativePath, IReadOnlyList contentFiles) + public bool IsMinificationEnabled { get; } + + public BundlerContext(string bundleRelativePath, IReadOnlyList contentFiles, bool isMinificationEnabled) { BundleRelativePath = bundleRelativePath; ContentFiles = contentFiles; + IsMinificationEnabled = isMinificationEnabled; } } } \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingMode.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingMode.cs new file mode 100644 index 0000000000..3e68c3c2a6 --- /dev/null +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingMode.cs @@ -0,0 +1,27 @@ +namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling +{ + public enum BundlingMode + { + /// + /// No bundling or minification. + /// + None, + + /// + /// Automatically determine the mode. + /// - Uses for development time. + /// - Uses for other environments. + /// + Auto, + + /// + /// Bundled but not minified. + /// + Bundle, + + /// + /// Bundled and minified. + /// + BundleAndMinify + } +} \ No newline at end of file diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingOptions.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingOptions.cs index 009cb2d4e1..b762e87631 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingOptions.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlingOptions.cs @@ -13,6 +13,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling /// public string BundleFolderName { get; } = "__bundles"; + /// + /// Default: auto. + /// + public BundlingMode Mode { get; set; } = BundlingMode.Auto; + public BundlingOptions() { StyleBundles = new BundleConfigurationCollection(); diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/IBundlerContext.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/IBundlerContext.cs index e43c4b5ba8..cdd70a6f4f 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/IBundlerContext.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/IBundlerContext.cs @@ -7,5 +7,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling string BundleRelativePath { get; } IReadOnlyList ContentFiles { get; } + + bool IsMinificationEnabled { get; } } } \ No newline at end of file diff --git a/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs b/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs index 3246372e21..503abf7156 100644 --- a/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs +++ b/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs @@ -201,7 +201,7 @@ namespace System { var bytes = str.GetBytes(); bytes.ShouldNotBeNull(); - bytes.Length.ShouldBeGreaterThan(0); + bytes.Length.ShouldBeGreaterThanOrEqualTo(str.Length); Encoding.UTF8.GetString(bytes).ShouldBe(str); } @@ -212,7 +212,7 @@ namespace System { var bytes = str.GetBytes(Encoding.ASCII); bytes.ShouldNotBeNull(); - bytes.Length.ShouldBeGreaterThan(0); + bytes.Length.ShouldBeGreaterThanOrEqualTo(str.Length); Encoding.ASCII.GetString(bytes).ShouldBe(str); }