Browse Source

Added basic logging for bundling.

pull/301/head
Halil ibrahim Kalkan 8 years ago
parent
commit
d257f1a63d
  1. 76
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs
  2. 11
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs

76
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs

@ -5,6 +5,8 @@ using System.Linq;
using System.Text; using System.Text;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling.Scripts; using Volo.Abp.AspNetCore.Mvc.UI.Bundling.Scripts;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling.Styles; using Volo.Abp.AspNetCore.Mvc.UI.Bundling.Styles;
@ -17,17 +19,17 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{ {
public class BundleManager : IBundleManager, ITransientDependency public class BundleManager : IBundleManager, ITransientDependency
{ {
protected IHybridWebRootFileProvider WebRootFileProvider { get; } public ILogger<BundleManager> Logger { get; set; }
//TODO: Make all protected readonly to allow easily extend the bundlemanager protected readonly BundlingOptions Options;
private readonly BundlingOptions _options; protected readonly IHybridWebRootFileProvider WebRootFileProvider;
private readonly IHostingEnvironment _hostingEnvironment; protected readonly IHostingEnvironment HostingEnvironment;
private readonly IScriptBundler _scriptBundler; protected readonly IScriptBundler ScriptBundler;
private readonly IStyleBundler _styleBundler; protected readonly IStyleBundler StyleBundler;
private readonly IServiceProvider _serviceProvider; protected readonly IServiceProvider ServiceProvider;
private readonly IDynamicFileProvider _dynamicFileProvider; protected readonly IDynamicFileProvider DynamicFileProvider;
private readonly IBundleCache _bundleCache; protected readonly IBundleCache BundleCache;
private readonly IWebRequestResources _requestResources; protected readonly IWebRequestResources RequestResources;
public BundleManager( public BundleManager(
IOptions<BundlingOptions> options, IOptions<BundlingOptions> options,
@ -40,30 +42,32 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
IHybridWebRootFileProvider webRootFileProvider, IHybridWebRootFileProvider webRootFileProvider,
IWebRequestResources requestResources) IWebRequestResources requestResources)
{ {
_hostingEnvironment = hostingEnvironment; HostingEnvironment = hostingEnvironment;
_scriptBundler = scriptBundler; ScriptBundler = scriptBundler;
_serviceProvider = serviceProvider; ServiceProvider = serviceProvider;
_dynamicFileProvider = dynamicFileProvider; DynamicFileProvider = dynamicFileProvider;
_bundleCache = bundleCache; BundleCache = bundleCache;
WebRootFileProvider = webRootFileProvider; WebRootFileProvider = webRootFileProvider;
_requestResources = requestResources; RequestResources = requestResources;
_styleBundler = styleBundler; StyleBundler = styleBundler;
_options = options.Value; Options = options.Value;
Logger = NullLogger<BundleManager>.Instance;
} }
public virtual IReadOnlyList<string> GetStyleBundleFiles(string bundleName) public virtual IReadOnlyList<string> GetStyleBundleFiles(string bundleName)
{ {
return GetBundleFiles(_options.StyleBundles, bundleName, _styleBundler); return GetBundleFiles(Options.StyleBundles, bundleName, StyleBundler);
} }
public virtual IReadOnlyList<string> GetScriptBundleFiles(string bundleName) public virtual IReadOnlyList<string> GetScriptBundleFiles(string bundleName)
{ {
return GetBundleFiles(_options.ScriptBundles, bundleName, _scriptBundler); return GetBundleFiles(Options.ScriptBundles, bundleName, ScriptBundler);
} }
protected virtual IReadOnlyList<string> GetBundleFiles(BundleConfigurationCollection bundles, string bundleName, IBundler bundler) protected virtual IReadOnlyList<string> GetBundleFiles(BundleConfigurationCollection bundles, string bundleName, IBundler bundler)
{ {
var files = _requestResources.TryAdd(CreateFileList(bundles, bundleName)); var files = RequestResources.TryAdd(CreateFileList(bundles, bundleName));
if (!IsBundlingEnabled()) if (!IsBundlingEnabled())
{ {
@ -71,10 +75,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
} }
var bundleRelativePath = var bundleRelativePath =
_options.BundleFolderName.EnsureEndsWith('/') + Options.BundleFolderName.EnsureEndsWith('/') +
bundleName + "." + files.JoinAsString("|").ToMd5() + "." + bundler.FileExtension; bundleName + "." + files.JoinAsString("|").ToMd5() + "." + bundler.FileExtension;
var cacheItem = _bundleCache.GetOrAdd(bundleRelativePath, () => var cacheItem = BundleCache.GetOrAdd(bundleRelativePath, () =>
{ {
var cacheValue = new BundleCacheItem( var cacheValue = new BundleCacheItem(
new List<string> new List<string>
@ -115,8 +119,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
cacheValue.WatchDisposeHandles.Clear(); cacheValue.WatchDisposeHandles.Clear();
} }
_bundleCache.Remove(bundleRelativePath); BundleCache.Remove(bundleRelativePath);
_dynamicFileProvider.Delete("/wwwroot/" + bundleRelativePath); //TODO: get rid of wwwroot! DynamicFileProvider.Delete("/wwwroot/" + bundleRelativePath); //TODO: get rid of wwwroot!
}, null); }, null);
cacheValue.WatchDisposeHandles.Add(watchDisposeHandle); cacheValue.WatchDisposeHandles.Add(watchDisposeHandle);
@ -128,7 +132,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{ {
var fileName = bundleRelativePath.Substring(bundleRelativePath.IndexOf('/') + 1); var fileName = bundleRelativePath.Substring(bundleRelativePath.IndexOf('/') + 1);
_dynamicFileProvider.AddOrUpdate( DynamicFileProvider.AddOrUpdate(
new InMemoryFileInfo( new InMemoryFileInfo(
Encoding.UTF8.GetBytes(bundleResult.Content), Encoding.UTF8.GetBytes(bundleResult.Content),
"/wwwroot/" + bundleRelativePath, //TODO: get rid of wwwroot! "/wwwroot/" + bundleRelativePath, //TODO: get rid of wwwroot!
@ -139,17 +143,17 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
public virtual void CreateStyleBundle(string bundleName, Action<BundleConfiguration> configureAction) public virtual void CreateStyleBundle(string bundleName, Action<BundleConfiguration> configureAction)
{ {
_options.StyleBundles.TryAdd(bundleName, configureAction); Options.StyleBundles.TryAdd(bundleName, configureAction);
} }
public virtual void CreateScriptBundle(string bundleName, Action<BundleConfiguration> configureAction) public virtual void CreateScriptBundle(string bundleName, Action<BundleConfiguration> configureAction)
{ {
_options.ScriptBundles.TryAdd(bundleName, configureAction); Options.ScriptBundles.TryAdd(bundleName, configureAction);
} }
protected virtual bool IsBundlingEnabled() protected virtual bool IsBundlingEnabled()
{ {
switch (_options.Mode) switch (Options.Mode)
{ {
case BundlingMode.None: case BundlingMode.None:
return false; return false;
@ -157,15 +161,15 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
case BundlingMode.BundleAndMinify: case BundlingMode.BundleAndMinify:
return true; return true;
case BundlingMode.Auto: case BundlingMode.Auto:
return !_hostingEnvironment.IsDevelopment(); return !HostingEnvironment.IsDevelopment();
default: default:
throw new AbpException($"Unhandled {nameof(BundlingMode)}: {_options.Mode}"); throw new AbpException($"Unhandled {nameof(BundlingMode)}: {Options.Mode}");
} }
} }
protected virtual bool IsMinficationEnabled() protected virtual bool IsMinficationEnabled()
{ {
switch (_options.Mode) switch (Options.Mode)
{ {
case BundlingMode.None: case BundlingMode.None:
case BundlingMode.Bundle: case BundlingMode.Bundle:
@ -173,15 +177,15 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
case BundlingMode.BundleAndMinify: case BundlingMode.BundleAndMinify:
return true; return true;
case BundlingMode.Auto: case BundlingMode.Auto:
return !_hostingEnvironment.IsDevelopment(); return !HostingEnvironment.IsDevelopment();
default: default:
throw new AbpException($"Unhandled {nameof(BundlingMode)}: {_options.Mode}"); throw new AbpException($"Unhandled {nameof(BundlingMode)}: {Options.Mode}");
} }
} }
protected virtual List<string> CreateFileList(BundleConfigurationCollection bundles, string bundleName) protected virtual List<string> CreateFileList(BundleConfigurationCollection bundles, string bundleName)
{ {
using (var scope = _serviceProvider.CreateScope()) using (var scope = ServiceProvider.CreateScope())
{ {
var contributors = new List<BundleContributor>(); var contributors = new List<BundleContributor>();
var context = new BundleConfigurationContext(scope.ServiceProvider); var context = new BundleConfigurationContext(scope.ServiceProvider);

11
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundlerBase.cs

@ -1,5 +1,7 @@
using System.Text; using System.Text;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.AspNetCore.Mvc.UI.Minification; using Volo.Abp.AspNetCore.Mvc.UI.Minification;
using Volo.Abp.AspNetCore.VirtualFileSystem; using Volo.Abp.AspNetCore.VirtualFileSystem;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -8,6 +10,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{ {
public abstract class BundlerBase : IBundler, ITransientDependency public abstract class BundlerBase : IBundler, ITransientDependency
{ {
public ILogger<BundlerBase> Logger { get; set; }
protected IHybridWebRootFileProvider WebRootFileProvider { get; } protected IHybridWebRootFileProvider WebRootFileProvider { get; }
protected IMinifier Minifier { get; } protected IMinifier Minifier { get; }
@ -15,12 +19,16 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{ {
WebRootFileProvider = webRootFileProvider; WebRootFileProvider = webRootFileProvider;
Minifier = minifier; Minifier = minifier;
Logger = NullLogger<BundlerBase>.Instance;
} }
public abstract string FileExtension { get; } public abstract string FileExtension { get; }
public BundleResult Bundle(IBundlerContext context) public BundleResult Bundle(IBundlerContext context)
{ {
Logger.LogDebug($"Bundling {context.BundleRelativePath}");
var sb = new StringBuilder(); var sb = new StringBuilder();
foreach (var file in context.ContentFiles) foreach (var file in context.ContentFiles)
@ -32,9 +40,12 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
if (context.IsMinificationEnabled) if (context.IsMinificationEnabled)
{ {
Logger.LogDebug($"Minifying {context.BundleRelativePath}");
bundleContent = Minifier.Minify(bundleContent, context.BundleRelativePath); bundleContent = Minifier.Minify(bundleContent, context.BundleRelativePath);
} }
Logger.LogDebug($"Bundled {context.BundleRelativePath}");
return new BundleResult(bundleContent); return new BundleResult(bundleContent);
} }

Loading…
Cancel
Save