Browse Source

#299 Ensure that different bundles not adding the same file to the page.

pull/301/head
Halil ibrahim Kalkan 8 years ago
parent
commit
0b5d58cb55
  1. 23
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs
  2. 6
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/SimpleBundleContributor.cs
  3. 19
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleContributorTagHelper.cs
  4. 17
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleContributorTagHelperService.cs
  5. 6
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleFileTagHelperService.cs
  6. 16
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleTagHelperServiceBase.cs
  7. 4
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpScriptBundleTagHelperService.cs
  8. 4
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpStyleBundleTagHelperService.cs
  9. 7
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperConsts.cs
  10. 38
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/BundleTagHelperItem.cs
  11. 16
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Resources/IWebRequestBundleCoordinator.cs
  12. 29
      src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Resources/WebRequestBundleCoordinator.cs
  13. 10
      src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/HighlightJs/HighlightJsScriptContributor.cs
  14. 2
      src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/Lodash/LodashScriptContributor.cs

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

@ -26,6 +26,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
private readonly IServiceProvider _serviceProvider; private readonly IServiceProvider _serviceProvider;
private readonly IDynamicFileProvider _dynamicFileProvider; private readonly IDynamicFileProvider _dynamicFileProvider;
private readonly IBundleCache _bundleCache; private readonly IBundleCache _bundleCache;
private readonly IWebRequestResources _requestResources;
public BundleManager( public BundleManager(
IOptions<BundlingOptions> options, IOptions<BundlingOptions> options,
@ -35,7 +36,8 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
IServiceProvider serviceProvider, IServiceProvider serviceProvider,
IDynamicFileProvider dynamicFileProvider, IDynamicFileProvider dynamicFileProvider,
IBundleCache bundleCache, IBundleCache bundleCache,
IHybridWebRootFileProvider webRootFileProvider) IHybridWebRootFileProvider webRootFileProvider,
IWebRequestResources requestResources)
{ {
_hostingEnvironment = hostingEnvironment; _hostingEnvironment = hostingEnvironment;
_scriptBundler = scriptBundler; _scriptBundler = scriptBundler;
@ -43,6 +45,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
_dynamicFileProvider = dynamicFileProvider; _dynamicFileProvider = dynamicFileProvider;
_bundleCache = bundleCache; _bundleCache = bundleCache;
WebRootFileProvider = webRootFileProvider; WebRootFileProvider = webRootFileProvider;
_requestResources = requestResources;
_styleBundler = styleBundler; _styleBundler = styleBundler;
_options = options.Value; _options = options.Value;
} }
@ -59,17 +62,19 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
protected virtual IReadOnlyList<string> GetBundleFiles(BundleConfigurationCollection bundles, string bundleName, IBundler bundler) protected virtual IReadOnlyList<string> GetBundleFiles(BundleConfigurationCollection bundles, string bundleName, IBundler bundler)
{ {
var bundleRelativePath = _options.BundleFolderName.EnsureEndsWith('/') + bundleName + "." + bundler.FileExtension; var files = _requestResources.TryAdd(CreateFileList(bundles, bundleName));
var cacheItem = _bundleCache.GetOrAdd(bundleRelativePath, () => if (!IsBundlingEnabled())
{ {
var files = CreateFileList(bundles, bundleName); return files;
}
if (!IsBundlingEnabled()) var bundleRelativePath =
{ _options.BundleFolderName.EnsureEndsWith('/') +
return new BundleCacheItem(files); bundleName + "." + files.JoinAsString("|").ToMd5() + "." + bundler.FileExtension;
}
var cacheItem = _bundleCache.GetOrAdd(bundleRelativePath, () =>
{
var cacheValue = new BundleCacheItem( var cacheValue = new BundleCacheItem(
new List<string> new List<string>
{ {
@ -187,7 +192,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
contributor.ConfigureBundle(context); contributor.ConfigureBundle(context);
} }
return context.Files; return context.Files; //TODO: Distinct?
} }
} }

6
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/SimpleBundleContributor.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{ {
@ -13,7 +14,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
public override void ConfigureBundle(BundleConfigurationContext context) public override void ConfigureBundle(BundleConfigurationContext context)
{ {
context.Files.AddRange(Files); foreach (var file in Files)
{
context.Files.AddIfNotContains(file);
}
} }
} }
} }

19
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleContributorTagHelper.cs

@ -0,0 +1,19 @@
using System;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
[HtmlTargetElement("abp-bundle-contributor", TagStructure = TagStructure.NormalOrSelfClosing, ParentTag = "abp-style-bundle")]
[HtmlTargetElement("abp-bundle-contributor", TagStructure = TagStructure.NormalOrSelfClosing, ParentTag = "abp-script-bundle")]
public class AbpBundleContributorTagHelper : AbpTagHelper<AbpBundleContributorTagHelper, AbpBundleContributorTagHelperService>
{
public Type Type { get; set; }
public AbpBundleContributorTagHelper(AbpBundleContributorTagHelperService service)
: base(service)
{
}
}
}

17
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleContributorTagHelperService.cs

@ -0,0 +1,17 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
public class AbpBundleContributorTagHelperService : AbpTagHelperService<AbpBundleContributorTagHelper>
{
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.SuppressOutput();
var files = (List<BundleTagHelperItem>)context.Items[AbpTagHelperConsts.ContextBundleItemListKey];
files.Add(new BundleTagHelperItem(TagHelper.Type));
}
}
}

6
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleFileTagHelperService.cs

@ -6,14 +6,12 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{ {
public class AbpBundleFileTagHelperService : AbpTagHelperService<AbpBundleFileTagHelper> public class AbpBundleFileTagHelperService : AbpTagHelperService<AbpBundleFileTagHelper>
{ {
public const string ContextFileListKey = "AbpBundleFileTagHelperService.BundleFiles";
public override void Process(TagHelperContext context, TagHelperOutput output) public override void Process(TagHelperContext context, TagHelperOutput output)
{ {
output.SuppressOutput(); output.SuppressOutput();
var files = (List<string>)context.Items[ContextFileListKey]; var files = (List<BundleTagHelperItem>)context.Items[AbpTagHelperConsts.ContextBundleItemListKey];
files.Add(TagHelper.Src); files.Add(new BundleTagHelperItem(TagHelper.Src));
} }
} }
} }

16
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleTagHelperServiceBase.cs

@ -24,10 +24,10 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
output.TagName = null; output.TagName = null;
var bundleName = TagHelper.Name; var bundleName = TagHelper.Name;
var files = await GetFileList(context, output); var files = await GetBundleItems(context, output);
if (bundleName.IsNullOrEmpty()) if (bundleName.IsNullOrEmpty())
{ {
bundleName = GenerateBundleName(context, output, files); bundleName = GenerateBundleName(files);
} }
CreateBundle(bundleName, files); CreateBundle(bundleName, files);
@ -48,21 +48,21 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
} }
} }
protected abstract void CreateBundle(string bundleName, List<string> files); protected abstract void CreateBundle(string bundleName, List<BundleTagHelperItem> bundleItems);
protected abstract IReadOnlyList<string> GetBundleFiles(string bundleName); protected abstract IReadOnlyList<string> GetBundleFiles(string bundleName);
protected abstract void AddHtmlTag(TagHelperContext context, TagHelperOutput output, string file); protected abstract void AddHtmlTag(TagHelperContext context, TagHelperOutput output, string file);
protected virtual string GenerateBundleName(TagHelperContext context, TagHelperOutput output, List<string> fileList) protected virtual string GenerateBundleName(List<BundleTagHelperItem> bundleItems)
{ {
return fileList.JoinAsString("|").ToMd5(); return bundleItems.JoinAsString("|").ToMd5();
} }
protected virtual async Task<List<string>> GetFileList(TagHelperContext context, TagHelperOutput output) protected virtual async Task<List<BundleTagHelperItem>> GetBundleItems(TagHelperContext context, TagHelperOutput output)
{ {
var fileList = new List<string>(); var fileList = new List<BundleTagHelperItem>();
context.Items[AbpBundleFileTagHelperService.ContextFileListKey] = fileList; context.Items[AbpTagHelperConsts.ContextBundleItemListKey] = fileList;
await output.GetChildContentAsync(); //TODO: Suppress child execution! await output.GetChildContentAsync(); //TODO: Suppress child execution!
return fileList; return fileList;
} }

4
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpScriptBundleTagHelperService.cs

@ -17,11 +17,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
} }
protected override void CreateBundle(string bundleName, List<string> files) protected override void CreateBundle(string bundleName, List<BundleTagHelperItem> bundleItems)
{ {
BundleManager.CreateScriptBundle( BundleManager.CreateScriptBundle(
bundleName, bundleName,
configuration => configuration.AddFiles(files.ToArray()) configuration => bundleItems.ForEach(bi => bi.AddToConfiguration(configuration))
); );
} }

4
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpStyleBundleTagHelperService.cs

@ -17,11 +17,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
} }
protected override void CreateBundle(string bundleName, List<string> files) protected override void CreateBundle(string bundleName, List<BundleTagHelperItem> bundleItems)
{ {
BundleManager.CreateStyleBundle( BundleManager.CreateStyleBundle(
bundleName, bundleName,
configuration => configuration.AddFiles(files.ToArray()) configuration => bundleItems.ForEach(bi => bi.AddToConfiguration(configuration))
); );
} }

7
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperConsts.cs

@ -0,0 +1,7 @@
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
public static class AbpTagHelperConsts
{
public const string ContextBundleItemListKey = "AbpBundleFileTagHelperService.BundleFiles";
}
}

38
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/BundleTagHelperItem.cs

@ -0,0 +1,38 @@
using System;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers
{
public class BundleTagHelperItem
{
public string File { get; }
public Type Type { get; }
public BundleTagHelperItem(string file)
{
File = file;
}
public BundleTagHelperItem(Type type)
{
Type = type;
}
public override string ToString()
{
return File ?? Type.FullName ?? "?";
}
public void AddToConfiguration(BundleConfiguration configuration)
{
if (File != null)
{
configuration.AddFiles(File);
}
else if (Type != null)
{
configuration.AddContributors(Type);
}
}
}
}

16
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Resources/IWebRequestBundleCoordinator.cs

@ -0,0 +1,16 @@
using System.Collections.Generic;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{
public interface IWebRequestResources
{
/// <summary>
/// Adds resouces to to current web request except the ones added before.
/// </summary>
/// <param name="resources">Candidate resources to be added</param>
/// <returns>Resources actually added</returns>
List<string> TryAdd(IEnumerable<string> resources);
bool TryAdd(string resource);
}
}

29
src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Resources/WebRequestBundleCoordinator.cs

@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Linq;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Mvc.UI.Resources
{
public class WebRequestResources : IWebRequestResources, IScopedDependency
{
protected List<string> Resources { get; }
public WebRequestResources()
{
Resources = new List<string>();
}
public virtual List<string> TryAdd(IEnumerable<string> resources)
{
var newFiles = resources.Except(Resources).ToList();
Resources.AddRange(newFiles);
return newFiles;
}
public bool TryAdd(string resource)
{
return Resources.AddIfNotContains(resource);
}
}
}

10
src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/HighlightJs/HighlightJsScriptContributor.cs

@ -13,11 +13,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.HighlightJs
context.Files.AddIfNotContains("/libs/highlight.js/highlight.js"); context.Files.AddIfNotContains("/libs/highlight.js/highlight.js");
//TODO: Add related languages by configuration (these can be default!) //TODO: Add related languages by configuration (these can be default!)
context.Files.AddIfNotContains("/libs/highlight.js/languages/cs.js"); //context.Files.AddIfNotContains("/libs/highlight.js/languages/cs.js");
context.Files.AddIfNotContains("/libs/highlight.js/languages/css.js"); //context.Files.AddIfNotContains("/libs/highlight.js/languages/css.js");
context.Files.AddIfNotContains("/libs/highlight.js/languages/javascript.js"); //context.Files.AddIfNotContains("/libs/highlight.js/languages/javascript.js");
context.Files.AddIfNotContains("/libs/highlight.js/languages/json.js"); //context.Files.AddIfNotContains("/libs/highlight.js/languages/json.js");
context.Files.AddIfNotContains("/libs/highlight.js/languages/xml.js"); //context.Files.AddIfNotContains("/libs/highlight.js/languages/xml.js");
} }
} }
} }

2
src/Volo.Abp.AspNetCore.Mvc.UI.Packages/Volo/Abp/AspNetCore/Mvc/UI/Packages/Lodash/LodashScriptContributor.cs

@ -10,7 +10,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Packages.Lodash
{ {
public override void ConfigureBundle(BundleConfigurationContext context) public override void ConfigureBundle(BundleConfigurationContext context)
{ {
context.Files.AddIfNotContains("/libs/loadash/lodash.min.js"); context.Files.AddIfNotContains("/libs/lodash/lodash.min.js");
} }
} }
} }

Loading…
Cancel
Save