mirror of https://github.com/abpframework/abp.git
9 changed files with 220 additions and 3 deletions
@ -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) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
@ -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); |
|||
} |
|||
} |
|||
} |
|||
@ -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) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -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; |
|||
} |
|||
} |
|||
} |
|||
@ -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) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -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; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue