From b59667a7ca4ef7eddebe18dfc1935bfee0e89ae2 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 29 Jul 2019 15:32:30 +0800 Subject: [PATCH] Resolve #1536 Use minified version of the script/style. --- .../AspNetCore/Mvc/UI/Bundling/BundlerBase.cs | 56 ++++++++++++++++--- .../Mvc/UI/Bundling/Scripts/ScriptBundler.cs | 6 ++ 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs index 50b81d9236..86271f8e64 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs @@ -1,3 +1,4 @@ +using System; using System.Text; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging; @@ -34,19 +35,38 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling Logger.LogDebug("Bundle files:"); foreach (var file in context.ContentFiles) { - var fileContent = GetFileContent(context, file); - Logger.LogDebug($"- {file} ({fileContent.Length} bytes)"); - sb.AppendLine(fileContent); - } + var fileInfo = GetFileInfo(context, file); + var fileContent = fileInfo.ReadAsString(); - var bundleContent = sb.ToString(); + Logger.LogDebug($"- {file} ({fileContent.Length} bytes)"); - if (context.IsMinificationEnabled) - { - Logger.LogInformation($"Minifying {context.BundleRelativePath} ({bundleContent.Length} bytes)"); - bundleContent = Minifier.Minify(bundleContent, context.BundleRelativePath); + if (IsMinFile(fileInfo)) + { + sb.Append(NormalizedCode(fileContent)); + } + else + { + var minFileContent = GetMinFileOrNull(file); + if (minFileContent != null) + { + sb.Append(NormalizedCode(minFileContent)); + } + else + { + if (context.IsMinificationEnabled) + { + Logger.LogInformation($"Minifying {context.BundleRelativePath} ({fileInfo.Length} bytes)"); + sb.Append(NormalizedCode(Minifier.Minify(fileContent, context.BundleRelativePath))); + } + else + { + sb.Append(NormalizedCode(fileContent)); + } + } + } } + var bundleContent = sb.ToString(); Logger.LogInformation($"Bundled {context.BundleRelativePath} ({bundleContent.Length} bytes)"); return new BundleResult(bundleContent); @@ -68,5 +88,23 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling return fileInfo; } + + protected virtual bool IsMinFile(IFileInfo fileInfo) + { + return fileInfo.Name.EndsWith($".min.{FileExtension}", StringComparison.InvariantCultureIgnoreCase); + } + + protected virtual string GetMinFileOrNull(string file) + { + var fileInfo = + WebContentFileProvider.GetFileInfo($"{file.RemovePostFix($".{FileExtension}")}.min.{FileExtension}"); + + return fileInfo.Exists ? fileInfo.ReadAsString() : null; + } + + protected virtual string NormalizedCode(string code) + { + return code; + } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/Scripts/ScriptBundler.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/Scripts/ScriptBundler.cs index e65379fc74..f5447dc8a3 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/Scripts/ScriptBundler.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/Scripts/ScriptBundler.cs @@ -1,3 +1,4 @@ +using System; using Volo.Abp.AspNetCore.Mvc.UI.Minification.Scripts; using Volo.Abp.AspNetCore.VirtualFileSystem; @@ -11,5 +12,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.Scripts : base(webContentFileProvider, minifier) { } + + protected override string NormalizedCode(string code) + { + return code.EnsureEndsWith(';') + Environment.NewLine; + } } } \ No newline at end of file