Browse Source

Lazy bundle additional configuration (for page bundles).

pull/301/head
Halil ibrahim Kalkan 8 years ago
parent
commit
76ecd83ae7
  1. 115
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationCollection.cs
  2. 4
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs
  3. 6
      src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs

115
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationCollection.cs

@ -1,68 +1,127 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using JetBrains.Annotations;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{ {
public class BundleConfigurationCollection public class BundleConfigurationCollection
{ {
private readonly ConcurrentDictionary<string, BundleConfiguration> _bundleContributors; private readonly ConcurrentDictionary<string, BundleConfiguration> _bundles;
private readonly ConcurrentDictionary<string, List<Action<BundleConfiguration>>> _lazyBundleConfigurationActions;
public BundleConfigurationCollection() public BundleConfigurationCollection()
{ {
_bundleContributors = new ConcurrentDictionary<string, BundleConfiguration>(); _bundles = new ConcurrentDictionary<string, BundleConfiguration>();
_lazyBundleConfigurationActions = new ConcurrentDictionary<string, List<Action<BundleConfiguration>>>();
} }
public BundleConfiguration Add(string bundleName) /// <summary>
/// Adds a new bundle with given <paramref name="bundleName"/>.
/// Throws <see cref="AbpException"/> if there is already a bundle with the same name.
/// </summary>
/// <param name="bundleName">Bundle name.</param>
/// <param name="configureAction">Initial configuration action.</param>
/// <returns>Returns this object for chained calls.</returns>
public BundleConfigurationCollection Add([NotNull] string bundleName, [CanBeNull] Action<BundleConfiguration> configureAction = null)
{ {
if (_bundleContributors.ContainsKey(bundleName)) if (!TryAdd(bundleName, configureAction))
{ {
throw new AbpException($"There is already a bundle added with given {nameof(bundleName)}: {bundleName}"); throw new AbpException($"There is already a bundle added with given {nameof(bundleName)}: {bundleName}");
} }
return _bundleContributors.AddOrUpdate(bundleName, new BundleConfiguration(bundleName), (n, c) => c); return this;
} }
public BundleConfiguration Get(string bundleName) /// <summary>
/// Tries to add a new bundle with given <paramref name="bundleName"/>.
/// Returns false if it's already created before.
/// <paramref name="configureAction"/> is not called if it's already added before.
/// </summary>
/// <param name="bundleName">Bundle name.</param>
/// <param name="configureAction">Initial configuration action.</param>
/// <returns>Returns true if added. Returns false if it's already added before.</returns>
public bool TryAdd([NotNull] string bundleName, [CanBeNull] Action<BundleConfiguration> configureAction = null)
{ {
CheckBundle(bundleName); Check.NotNull(bundleName, nameof(bundleName));
return _bundleContributors[bundleName]; if (_bundles.ContainsKey(bundleName))
} {
return false;
}
public BundleConfiguration GetOrNull(string bundleName) var bundle = new BundleConfiguration(bundleName);
{ configureAction?.Invoke(bundle);
if (!_bundleContributors.TryGetValue(bundleName, out var bundleConfiguration))
if (_lazyBundleConfigurationActions.TryGetValue(bundleName, out var configurationActions))
{ {
return null; lock (configurationActions)
{
configurationActions.ForEach(c => c.Invoke(bundle));
}
} }
return bundleConfiguration; if (!_bundles.TryAdd(bundleName, bundle))
} {
return false;
}
public BundleConfiguration GetOrAdd(string bundleName) if (configurationActions != null)
{ {
return GetOrAdd(bundleName, c => { }); lock (configurationActions)
{
configurationActions.Clear();
}
}
return true;
} }
internal BundleConfiguration GetOrAdd(string bundleName, Action<BundleConfiguration> configureAction) /// <summary>
/// Configures an existing bundle.
/// This method also works for lazy bundles (those are created using razor tag helpers).
/// </summary>
/// <param name="bundleName">Bundle name</param>
/// <param name="configureAction">Configure action</param>
/// <returns>Returns this object for chained calls.</returns>
public BundleConfigurationCollection Configure([NotNull] string bundleName, [NotNull] Action<BundleConfiguration> configureAction)
{ {
return _bundleContributors.GetOrAdd( Check.NotNull(bundleName, nameof(bundleName));
bundleName, Check.NotNull(configureAction, nameof(configureAction));
() =>
if (_bundles.TryGetValue(bundleName, out var bundle))
{
configureAction.Invoke(bundle);
}
else
{
var configurationActions = _lazyBundleConfigurationActions
.GetOrAdd(bundleName, () => new List<Action<BundleConfiguration>>());
lock (configurationActions)
{ {
var configuration = new BundleConfiguration(bundleName); configurationActions.Add(configureAction);
configureAction.Invoke(configuration); }
return configuration; }
});
return this;
} }
private void CheckBundle(string bundleName) /// <summary>
/// Gets a bundle.
/// </summary>
/// <param name="bundleName">The bundle name</param>
/// <returns>The bundle configuration</returns>
public BundleConfiguration Get([NotNull] string bundleName)
{ {
if (!_bundleContributors.ContainsKey(bundleName)) Check.NotNull(bundleName, nameof(bundleName));
if (!_bundles.TryGetValue(bundleName, out var bundle))
{ {
throw new AbpException($"There is no bundle with given {nameof(bundleName)}: {bundleName}"); throw new AbpException($"There is no bundle with given {nameof(bundleName)}: {bundleName}");
} }
return bundle;
} }
} }
} }

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

@ -139,12 +139,12 @@ 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.GetOrAdd(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.GetOrAdd(bundleName, configureAction); _options.ScriptBundles.TryAdd(bundleName, configureAction);
} }
protected virtual bool IsBundlingEnabled() protected virtual bool IsBundlingEnabled()

6
src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs

@ -25,13 +25,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared
{ {
options options
.StyleBundles .StyleBundles
.Add(StandardBundles.Styles.Global) .Add(StandardBundles.Styles.Global, bundle => { bundle.AddContributors(typeof(SharedThemeGlobalStyleContributor)); });
.AddContributors(typeof(SharedThemeGlobalStyleContributor));
options options
.ScriptBundles .ScriptBundles
.Add(StandardBundles.Scripts.Global) .Add(StandardBundles.Scripts.Global, bundle => bundle.AddContributors(typeof(SharedThemeGlobalScriptContributor)));
.AddContributors(typeof(SharedThemeGlobalScriptContributor));
}); });
services.AddAssemblyOf<AbpAspNetCoreMvcUiThemeSharedModule>(); services.AddAssemblyOf<AbpAspNetCoreMvcUiThemeSharedModule>();

Loading…
Cancel
Save