From f7678914d6220cdddbdf55d64ccb642741dfc973 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 25 Jun 2025 08:27:17 +0800 Subject: [PATCH] Add methods to remove bundle files by name or predicate Resolve #23169 ```cs Configure(options => { options.ScriptBundles .Configure(typeof(ManageModel).FullName, configuration => { configuration.RemoveFiles("/Pages/Account/Default.js"); }); }); ``` --- .../Bundling/BundleConfigurationExtensions.cs | 12 ++++++++ .../Bundling/BundleContributorCollection.cs | 30 +++++++++++++++++++ .../BundleContributorCollectionExtensions.cs | 15 +++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) 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 62f7950c5a..f65f8b13e9 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 @@ -23,6 +23,18 @@ public static class BundleConfigurationExtensions return bundleConfiguration; } + public static BundleConfiguration RemoveFiles(this BundleConfiguration bundleConfiguration, params string[] files) + { + bundleConfiguration.Contributors.RemoveBundleFile(files.ToArray()); + return bundleConfiguration; + } + + public static BundleConfiguration RemoveFiles(this BundleConfiguration bundleConfiguration, Func predicate) + { + bundleConfiguration.Contributors.RemoveBundleFile(predicate); + return bundleConfiguration; + } + public static BundleConfiguration AddContributors(this BundleConfiguration bundleConfiguration, params IBundleContributor[] contributors) { Check.NotNull(contributors, nameof(contributors)); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollection.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollection.cs index 5f245c1827..e92538a23f 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollection.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Bundling.Abstractions/Volo/Abp/AspNetCore/Mvc/UI/Bundling/BundleContributorCollection.cs @@ -76,6 +76,36 @@ public class BundleContributorCollection } } + public void RemoveBundleFile(string fileName) + { + RemoveBundleFile([fileName]); + } + + public void RemoveBundleFile(string[] fileNames) + { + var contributors = _contributors + .Where(x => x is BundleFileContributor bundleContributor && + bundleContributor.Files.Any(f => fileNames.Any(name => name.Equals(f.FileName, StringComparison.OrdinalIgnoreCase)))) + .Cast(); + foreach (var contributor in contributors) + { + contributor.Files.RemoveAll(x => fileNames.Any(name => name.Equals(x.FileName, StringComparison.OrdinalIgnoreCase))); + } + } + + public void RemoveBundleFile(Func predicate) + { + var contributors = _contributors + .Where(x => x is BundleFileContributor bundleContributor && + bundleContributor.Files.Any(f => predicate(f.FileName))) + .Cast(); + + foreach (var contributor in contributors) + { + contributor.Files.RemoveAll(x => predicate(x.FileName)); + } + } + public IReadOnlyList GetAll() { return _contributors.ToImmutableList(); 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 addbe7e2cd..74f73fa1f3 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 @@ -1,4 +1,7 @@ -namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling; +using System; +using System.Linq; + +namespace Volo.Abp.AspNetCore.Mvc.UI.Bundling; public static class BundleContributorCollectionExtensions { @@ -16,4 +19,14 @@ public static class BundleContributorCollectionExtensions { contributors.Add(new BundleFileContributor(files)); } + + public static void RemoveFile(this BundleContributorCollection contributors, params string[] files) + { + contributors.RemoveBundleFile(files.ToArray()); + } + + public static void RemoveFile(this BundleContributorCollection contributors, Func predicate) + { + contributors.RemoveBundleFile(predicate); + } }