From 519b300ef2fde8e78c39cd4147c095590363c137 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 18 Oct 2023 09:50:47 +0800 Subject: [PATCH] Add `is-cdn` to script/style taghelper. --- .../Bundling/BundleConfigurationExtensions.cs | 6 ++++ .../BundleContributorCollectionExtensions.cs | 6 ++++ .../AspNetCore/Mvc/UI/Bundling/BundleFile.cs | 14 ++++++-- .../Mvc/UI/Bundling/BundleFileContributor.cs | 2 +- .../UI/Bundling/BundleFileListExtensions.cs | 32 ------------------- .../Mvc/UI/Bundling/BundleManager.cs | 2 +- .../TagHelpers/AbpBundleItemTagHelper.cs | 6 +++- .../TagHelpers/AbpTagHelperResourceService.cs | 6 ++-- .../TagHelpers/AbpTagHelperScriptService.cs | 4 +-- .../TagHelpers/AbpTagHelperStyleService.cs | 4 +-- .../TagHelpers/BundleTagHelperFileItem.cs | 6 ++-- .../SharedThemeGlobalScriptContributor.cs | 22 ++++++------- .../SharedThemeGlobalStyleContributor.cs | 8 ++--- 13 files changed, 55 insertions(+), 63 deletions(-) delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFileListExtensions.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationExtensions.cs index ca628f4d67..8dd51dc5a3 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleConfigurationExtensions.cs @@ -11,6 +11,12 @@ public static class BundleConfigurationExtensions return bundleConfiguration; } + public static BundleConfiguration AddFiles(this BundleConfiguration bundleConfiguration, params BundleFile[] files) + { + bundleConfiguration.Contributors.AddFiles(files); + return bundleConfiguration; + } + public static BundleConfiguration AddCdnFiles(this BundleConfiguration bundleConfiguration, params string[] files) { bundleConfiguration.Contributors.AddCdnFiles(files.Select(x => new BundleFile(x, true)).ToArray()); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollectionExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollectionExtensions.cs index 3c0dfdeab8..22bfe2947c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollectionExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollectionExtensions.cs @@ -6,6 +6,12 @@ public static class BundleContributorCollectionExtensions { contributors.Add(new BundleFileContributor(files)); } + + public static void AddFiles(this BundleContributorCollection contributors, params BundleFile[] files) + { + contributors.Add(new BundleFileContributor(files)); + } + public static void AddCdnFiles(this BundleContributorCollection contributors, params BundleFile[] files) { contributors.Add(new BundleFileContributor(files)); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFile.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFile.cs index 58427b6c40..091a4b3823 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFile.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFile.cs @@ -2,13 +2,21 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling; public class BundleFile { - public string File { get; set; } + public string FileName { get; set; } public bool IsCdn { get; set; } - public BundleFile(string file, bool isCdn = false) + public BundleFile(string fileName, bool isCdn = false) { - File = file; + FileName = fileName; IsCdn = isCdn; } + + /// + /// This method is used to compatible with old code. + /// + public static implicit operator BundleFile(string fileName) + { + return new BundleFile(fileName); + } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFileContributor.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFileContributor.cs index 5107c006a0..df55266110 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFileContributor.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFileContributor.cs @@ -24,7 +24,7 @@ public class BundleFileContributor : BundleContributor { foreach (var file in Files) { - context.Files.AddIfNotContains(x => x.File.Equals(file.File, StringComparison.OrdinalIgnoreCase), () => file); + context.Files.AddIfNotContains(x => x.FileName.Equals(file.FileName, StringComparison.OrdinalIgnoreCase), () => file); } } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFileListExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFileListExtensions.cs deleted file mode 100644 index b3088b293d..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleFileListExtensions.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling; - -/// -/// This class is used to compatible with old code. -/// -public static class BundleFileListExtensions -{ - public static void Add(this List bundleFiles, params string[] files) - { - bundleFiles.AddRange(files.Select(file => new BundleFile(file))); - } - - public static void AddRange(this List bundleFiles, params string[] files) - { - bundleFiles.AddRange(files.Select(file => new BundleFile(file))); - } - - public static void AddIfNotContains(this List bundleFiles, params string[] files) - { - foreach (var file in files) - { - if (!bundleFiles.Any(x => x.File.Equals(file, StringComparison.OrdinalIgnoreCase))) - { - bundleFiles.Add(new BundleFile(file)); - } - } - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs index fb7b3cd489..9e5ca4bdef 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleManager.cs @@ -83,7 +83,7 @@ public class BundleManager : IBundleManager, ITransientDependency { if (!bundleFile.IsCdn) { - localBundleFiles.Add(bundleFile.File); + localBundleFiles.Add(bundleFile.FileName); } else { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleItemTagHelper.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleItemTagHelper.cs index 1240dbef11..81c06df7c3 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleItemTagHelper.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpBundleItemTagHelper.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.AspNetCore.Razor.TagHelpers; using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers; namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers; @@ -17,6 +18,9 @@ public abstract class AbpBundleItemTagHelper : Ab /// public Type? Type { get; set; } + [HtmlAttributeName("is-cdn")] + public bool IsCdn { get; set; } + protected AbpBundleItemTagHelper(TTagHelperService service) : base(service) { @@ -49,7 +53,7 @@ public abstract class AbpBundleItemTagHelper : Ab if (Src != null) { - return new BundleTagHelperFileItem(Src); + return new BundleTagHelperFileItem(new BundleFile(Src, IsCdn)); } throw new AbpException("abp-script tag helper requires to set either src or type!"); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs index 1926cb48cf..5a19dee915 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperResourceService.cs @@ -71,10 +71,10 @@ public abstract class AbpTagHelperResourceService : ITransientDependency } else { - var file = HostingEnvironment.WebRootFileProvider.GetFileInfo(bundleFile.File); + var file = HostingEnvironment.WebRootFileProvider.GetFileInfo(bundleFile.FileName); if (file == null || !file.Exists) { - Logger.LogError($"Could not find the bundle file '{bundleFile.File}' for the bundle '{bundleName}'!"); + Logger.LogError($"Could not find the bundle file '{bundleFile.FileName}' for the bundle '{bundleName}'!"); AddErrorScript(viewContext, tagHelper, context, output, bundleFile, bundleName!); continue; } @@ -98,7 +98,7 @@ public abstract class AbpTagHelperResourceService : ITransientDependency protected virtual void AddErrorScript(ViewContext viewContext, TagHelper tagHelper, TagHelperContext context, TagHelperOutput output, BundleFile file, string bundleName) { - output.Content.AppendHtml($"{Environment.NewLine}"); + output.Content.AppendHtml($"{Environment.NewLine}"); } protected virtual string GenerateBundleName(List bundleItems) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs index 3ff8e05552..ea0f11b777 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperScriptService.cs @@ -47,13 +47,13 @@ public class AbpTagHelperScriptService : AbpTagHelperResourceService _ => false }; - var deferText = (defer || Options.DeferScriptsByDefault || Options.DeferScripts.Any(x => file.File.StartsWith(x, StringComparison.OrdinalIgnoreCase))) + var deferText = (defer || Options.DeferScriptsByDefault || Options.DeferScripts.Any(x => file.FileName.StartsWith(x, StringComparison.OrdinalIgnoreCase))) ? "defer " : string.Empty; var nonceText = (viewContext.HttpContext.Items.TryGetValue(AbpAspNetCoreConsts.ScriptNonceKey, out var nonce) && nonce is string nonceString && !string.IsNullOrEmpty(nonceString)) ? $"nonce=\"{nonceString}\" " : string.Empty; - var src = file.IsCdn ? file.File : viewContext.GetUrlHelper().Content((file.File + "?_v=" + fileInfo!.LastModified.UtcTicks).EnsureStartsWith('~')); + var src = file.IsCdn ? file.FileName : viewContext.GetUrlHelper().Content((file.FileName + "?_v=" + fileInfo!.LastModified.UtcTicks).EnsureStartsWith('~')); output.Content.AppendHtml($"{Environment.NewLine}"); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs index a43221841e..63f2e2304d 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/AbpTagHelperStyleService.cs @@ -51,8 +51,8 @@ public class AbpTagHelperStyleService : AbpTagHelperResourceService _ => false }; - var href = file.IsCdn ? file.File : viewContext.GetUrlHelper().Content((file.File + "?_v=" + fileInfo!.LastModified.UtcTicks).EnsureStartsWith('~')); - if (preload || Options.PreloadStylesByDefault || Options.PreloadStyles.Any(x => file.File.StartsWith(x, StringComparison.OrdinalIgnoreCase))) + var href = file.IsCdn ? file.FileName : viewContext.GetUrlHelper().Content((file.FileName + "?_v=" + fileInfo!.LastModified.UtcTicks).EnsureStartsWith('~')); + if (preload || Options.PreloadStylesByDefault || Options.PreloadStyles.Any(x => file.FileName.StartsWith(x, StringComparison.OrdinalIgnoreCase))) { output.Content.AppendHtml(SecurityHeadersOptions.UseContentSecurityPolicyScriptNonce ? $"{Environment.NewLine}" diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/BundleTagHelperFileItem.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/BundleTagHelperFileItem.cs index 45ab8b64eb..b5f6ca74aa 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/BundleTagHelperFileItem.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling/Volo/Abp/AspNetCore/Mvc/UI/Bundling/TagHelpers/BundleTagHelperFileItem.cs @@ -5,16 +5,16 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling.TagHelpers; public class BundleTagHelperFileItem : BundleTagHelperItem { [NotNull] - public string File { get; } + public BundleFile File { get; } - public BundleTagHelperFileItem([NotNull] string file) + public BundleTagHelperFileItem([NotNull] BundleFile file) { File = Check.NotNull(file, nameof(file)); } public override string ToString() { - return File; + return File.FileName; } public override void AddToConfiguration(BundleConfiguration configuration) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalScriptContributor.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalScriptContributor.cs index 2ac299959a..2ca7c191c8 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalScriptContributor.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalScriptContributor.cs @@ -35,20 +35,20 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Bundling; )] public class SharedThemeGlobalScriptContributor : BundleContributor { - + public override void ConfigureBundle(BundleConfigurationContext context) { - context.Files.AddRange(new[] + context.Files.AddRange(new BundleFile[] { - "/libs/abp/aspnetcore-mvc-ui-theme-shared/ui-extensions.js", - "/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js", - "/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery-form/jquery-form-extensions.js", - "/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/widget-manager.js", - "/libs/abp/aspnetcore-mvc-ui-theme-shared/bootstrap/dom-event-handlers.js", - "/libs/abp/aspnetcore-mvc-ui-theme-shared/bootstrap/modal-manager.js", - "/libs/abp/aspnetcore-mvc-ui-theme-shared/datatables/datatables-extensions.js", - "/libs/abp/aspnetcore-mvc-ui-theme-shared/sweetalert2/abp-sweetalert2.js", - "/libs/abp/aspnetcore-mvc-ui-theme-shared/toastr/abp-toastr.js" + "/libs/abp/aspnetcore-mvc-ui-theme-shared/ui-extensions.js", + "/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js", + "/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery-form/jquery-form-extensions.js", + "/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/widget-manager.js", + "/libs/abp/aspnetcore-mvc-ui-theme-shared/bootstrap/dom-event-handlers.js", + "/libs/abp/aspnetcore-mvc-ui-theme-shared/bootstrap/modal-manager.js", + "/libs/abp/aspnetcore-mvc-ui-theme-shared/datatables/datatables-extensions.js", + "/libs/abp/aspnetcore-mvc-ui-theme-shared/sweetalert2/abp-sweetalert2.js", + "/libs/abp/aspnetcore-mvc-ui-theme-shared/toastr/abp-toastr.js" }); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalStyleContributor.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalStyleContributor.cs index 01f490425c..f959d73f05 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalStyleContributor.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalStyleContributor.cs @@ -27,10 +27,10 @@ public class SharedThemeGlobalStyleContributor : BundleContributor { public override void ConfigureBundle(BundleConfigurationContext context) { - context.Files.AddRange(new[] + context.Files.AddRange(new BundleFile[] { - "/libs/abp/aspnetcore-mvc-ui-theme-shared/datatables/datatables-styles.css", - "/libs/abp/aspnetcore-mvc-ui-theme-shared/date-range-picker/date-range-picker-styles.css" - }); + "/libs/abp/aspnetcore-mvc-ui-theme-shared/datatables/datatables-styles.css", + "/libs/abp/aspnetcore-mvc-ui-theme-shared/date-range-picker/date-range-picker-styles.css" + }); } }