Browse Source

Tag helpers for bundling

pull/301/head
Halil ibrahim Kalkan 8 years ago
parent
commit
e6d4df6fb2
  1. 17
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpBundleFileTagHelper.cs
  2. 18
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpBundleFileTagHelperService.cs
  3. 15
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpScriptBundleTagHelper.cs
  4. 61
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpScriptBundleTagHelperService.cs
  5. 18
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpStyleBundleTagHelper.cs
  6. 62
      src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpStyleBundleTagHelperService.cs
  7. 14
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollection.cs
  8. 13
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleManager.cs
  9. 5
      src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleManager.cs

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

@ -0,0 +1,17 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling
{
[HtmlTargetElement("abp-bundle-file", TagStructure = TagStructure.NormalOrSelfClosing, ParentTag = "abp-style-bundle")]
[HtmlTargetElement("abp-bundle-file", TagStructure = TagStructure.NormalOrSelfClosing, ParentTag = "abp-script-bundle")]
public class AbpBundleFileTagHelper : AbpTagHelper<AbpBundleFileTagHelper, AbpBundleFileTagHelperService>
{
public string Src { get; set; }
public AbpBundleFileTagHelper(AbpBundleFileTagHelperService service)
: base(service)
{
}
}
}

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

@ -0,0 +1,18 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling
{
public class AbpBundleFileTagHelperService : AbpTagHelperService<AbpBundleFileTagHelper>
{
public const string ContextFileListKey = "AbpBundleFileTagHelperService.BundleFiles";
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.SuppressOutput();
var files = (List<string>)context.Items[ContextFileListKey];
files.Add(TagHelper.Src);
}
}
}

15
src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpScriptBundleTagHelper.cs

@ -0,0 +1,15 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling
{
[HtmlTargetElement("abp-script-bundle", TagStructure = TagStructure.NormalOrSelfClosing)]
public class AbpScriptBundleTagHelper : AbpTagHelper<AbpScriptBundleTagHelper, AbpScriptBundleTagHelperService>
{
public string Name { get; set; }
public AbpScriptBundleTagHelper(AbpScriptBundleTagHelperService service)
: base(service)
{
}
}
}

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

@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling
{
public class AbpScriptBundleTagHelperService : AbpTagHelperService<AbpScriptBundleTagHelper>
{
private readonly IBundleManager _bundleManager;
public AbpScriptBundleTagHelperService(IBundleManager bundleManager)
{
_bundleManager = bundleManager;
}
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
output.TagName = null;
var bundleName = TagHelper.Name;
var files = await GetFileList(context, output);
if (bundleName.IsNullOrEmpty())
{
bundleName = GenerateBundleName(context, output, files);
}
_bundleManager.CreateDynamicScriptBundle(
bundleName,
configuration => configuration.AddFiles(files.ToArray())
);
var bundleFiles = _bundleManager.GetScriptBundleFiles(bundleName);
await output.GetChildContentAsync(); //TODO: Suppress child execution!
output.Content.Clear();
AddLinkTags(context, output, bundleFiles);
}
protected virtual void AddLinkTags(TagHelperContext context, TagHelperOutput output, List<string> files)
{
foreach (var file in files)
{
output.Content.AppendHtml($"<script src=\"{file}\" type=\"text/javascript\"></script>{Environment.NewLine}");
}
}
protected virtual string GenerateBundleName(TagHelperContext context, TagHelperOutput output, List<string> fileList)
{
return fileList.JoinAsString("|").ToMd5();
}
protected virtual async Task<List<string>> GetFileList(TagHelperContext context, TagHelperOutput output)
{
var fileList = new List<string>();
context.Items[AbpBundleFileTagHelperService.ContextFileListKey] = fileList;
await output.GetChildContentAsync();
return fileList;
}
}
}

18
src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Bundling/AbpStyleBundleTagHelper.cs

@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.Threading;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling
{
//TODO: Move to Volo.Abp.AspNetCore.Mvc.UI package.
[HtmlTargetElement("abp-style-bundle", TagStructure = TagStructure.NormalOrSelfClosing)]
public class AbpStyleBundleTagHelper : AbpTagHelper<AbpStyleBundleTagHelper, AbpStyleBundleTagHelperService>
{
public string Name { get; set; }
public AbpStyleBundleTagHelper(AbpStyleBundleTagHelperService service)
: base(service)
{
}
}
}

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

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Volo.Abp.AspNetCore.Mvc.UI.Bundling;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Bundling
{
public class AbpStyleBundleTagHelperService : AbpTagHelperService<AbpStyleBundleTagHelper>
{
private readonly IBundleManager _bundleManager;
public AbpStyleBundleTagHelperService(IBundleManager bundleManager)
{
_bundleManager = bundleManager;
}
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
output.TagName = null;
var bundleName = TagHelper.Name;
var files = await GetFileList(context, output);
if (bundleName.IsNullOrEmpty())
{
bundleName = GenerateBundleName(context, output, files);
}
_bundleManager.CreateDynamicStyleBundle(
bundleName,
configuration => configuration.AddFiles(files.ToArray())
);
var bundleFiles = _bundleManager.GetStyleBundleFiles(bundleName);
await output.GetChildContentAsync(); //TODO: Suppress child execution!
output.Content.Clear();
AddLinkTags(context, output, bundleFiles);
}
protected virtual void AddLinkTags(TagHelperContext context, TagHelperOutput output, List<string> files)
{
foreach (var file in files)
{
output.Content.AppendHtml(
$"<link rel=\"stylesheet\" type=\"text/css\" href=\"{file}\" />{Environment.NewLine}");
}
}
protected virtual string GenerateBundleName(TagHelperContext context, TagHelperOutput output, List<string> fileList)
{
return fileList.JoinAsString("|").ToMd5();
}
protected virtual async Task<List<string>> GetFileList(TagHelperContext context, TagHelperOutput output)
{
var fileList = new List<string>();
context.Items[AbpBundleFileTagHelperService.ContextFileListKey] = fileList;
await output.GetChildContentAsync();
return fileList;
}
}
}

14
src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/BundleCollection.cs

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
{
@ -34,9 +35,16 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
return _bundleContributors[bundleName];
}
public BundleConfiguration GetOrAdd(string bundleName)
public BundleConfiguration GetOrAdd(string bundleName, Action<BundleConfiguration> configureAction = null)
{
return _bundleContributors.GetOrAdd(bundleName, () => new BundleConfiguration(bundleName));
return _bundleContributors.GetOrAdd(
bundleName,
() =>
{
var configuration = new BundleConfiguration(bundleName);
configureAction?.Invoke(configuration);
return configuration;
});
}
public List<string> GetFiles(string bundleName)

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

@ -1,3 +1,4 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
@ -48,5 +49,17 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
)
};
}
public void CreateDynamicStyleBundle(string bundleName, Action<BundleConfiguration> configureAction)
{
//TODO: How to handle concurrency!
_options.StyleBundles.GetOrAdd(bundleName, configureAction);
}
public void CreateDynamicScriptBundle(string bundleName, Action<BundleConfiguration> configureAction)
{
//TODO: How to handle concurrency!
_options.ScriptBundles.GetOrAdd(bundleName, configureAction);
}
}
}

5
src/Volo.Abp.AspNetCore.Mvc.UI/UI/Bundling/IBundleManager.cs

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
@ -7,5 +8,9 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling
List<string> GetStyleBundleFiles(string bundleName);
List<string> GetScriptBundleFiles(string bundleName);
void CreateDynamicStyleBundle(string bundleName, Action<BundleConfiguration> configureAction);
void CreateDynamicScriptBundle(string bundleName, Action<BundleConfiguration> configureAction);
}
}
Loading…
Cancel
Save