Browse Source

Resolve #1536 Use minified version of the script/style.

pull/1545/head
maliming 7 years ago
parent
commit
b59667a7ca
  1. 56
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs
  2. 6
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/Scripts/ScriptBundler.cs

56
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 System.Text;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -34,19 +35,38 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
Logger.LogDebug("Bundle files:"); Logger.LogDebug("Bundle files:");
foreach (var file in context.ContentFiles) foreach (var file in context.ContentFiles)
{ {
var fileContent = GetFileContent(context, file); var fileInfo = GetFileInfo(context, file);
Logger.LogDebug($"- {file} ({fileContent.Length} bytes)"); var fileContent = fileInfo.ReadAsString();
sb.AppendLine(fileContent);
}
var bundleContent = sb.ToString(); Logger.LogDebug($"- {file} ({fileContent.Length} bytes)");
if (context.IsMinificationEnabled) if (IsMinFile(fileInfo))
{ {
Logger.LogInformation($"Minifying {context.BundleRelativePath} ({bundleContent.Length} bytes)"); sb.Append(NormalizedCode(fileContent));
bundleContent = Minifier.Minify(bundleContent, context.BundleRelativePath); }
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)"); Logger.LogInformation($"Bundled {context.BundleRelativePath} ({bundleContent.Length} bytes)");
return new BundleResult(bundleContent); return new BundleResult(bundleContent);
@ -68,5 +88,23 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
return fileInfo; 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;
}
} }
} }

6
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.Mvc.UI.Minification.Scripts;
using Volo.Abp.AspNetCore.VirtualFileSystem; using Volo.Abp.AspNetCore.VirtualFileSystem;
@ -11,5 +12,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.Scripts
: base(webContentFileProvider, minifier) : base(webContentFileProvider, minifier)
{ {
} }
protected override string NormalizedCode(string code)
{
return code.EnsureEndsWith(';') + Environment.NewLine;
}
} }
} }
Loading…
Cancel
Save