Skip to content

Commit d2f34d6

Browse files
authored
Enable trimming for Microsoft.Extensions packages (#24986)
* Enable trimming for Microsoft.Extensions packages Fixes #24634 * Update Microsoft.NET.Sdk.BlazorWebAssembly.csproj * Apply suggestions from code review * Fixup test
1 parent 187eacd commit d2f34d6

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

src/Components/WebAssembly/Sdk/integrationtests/WasmPublishIntegrationTest.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ public async Task Publish_WithScopedCss_Works()
141141
serviceWorkerPath: Path.Combine("serviceworkers", "my-service-worker.js"),
142142
serviceWorkerContent: "// This is the production service worker",
143143
assetsManifestPath: "custom-service-worker-assets.js");
144-
145-
VerifyTypeGranularTrimming(result, blazorPublishDirectory);
146144
}
147145

148146
[Fact]
@@ -906,16 +904,17 @@ static string ParseWebFormattedHash(string webFormattedHash)
906904

907905
private void VerifyTypeGranularTrimming(MSBuildResult result, string blazorPublishDirectory)
908906
{
909-
var loggingAssemblyPath = Path.Combine(blazorPublishDirectory, "_framework", "Microsoft.Extensions.Logging.Abstractions.dll");
910-
Assert.FileExists(result, loggingAssemblyPath);
907+
var componentsShimAssemblyPath = Path.Combine(blazorPublishDirectory, "_framework", "Microsoft.AspNetCore.Razor.Test.ComponentShim.dll");
908+
Assert.FileExists(result, componentsShimAssemblyPath);
909+
910+
// RouteView is referenced by the app, so we expect it to be preserved
911+
Assert.AssemblyContainsType(result, componentsShimAssemblyPath, "Microsoft.AspNetCore.Components.RouteView");
911912

912-
// ILogger is referenced by the app, so we expect it to be preserved
913-
Assert.AssemblyContainsType(result, loggingAssemblyPath, "Microsoft.Extensions.Logging.ILogger");
914-
// LogLevel is referenced by ILogger and therefore must be preserved.
915-
Assert.AssemblyContainsType(result, loggingAssemblyPath, "Microsoft.Extensions.Logging.LogLevel");
913+
// RouteData is referenced by RouteView so we expect it to be preserved.
914+
Assert.AssemblyContainsType(result, componentsShimAssemblyPath, "Microsoft.AspNetCore.Components.RouteData");
916915

917-
// NullLogger is not referenced by the app, and should be trimmed.
918-
Assert.AssemblyDoesNotContainType(result, loggingAssemblyPath, "Microsoft.Extensions.Logging.Abstractions.NullLogger");
916+
// CascadingParameterAttribute is not referenced by the app, and should be trimmed.
917+
Assert.AssemblyDoesNotContainType(result, componentsShimAssemblyPath, "Microsoft.AspNetCore.Components.CascadingParameterAttribute");
919918
}
920919

921920
private static BootJsonData ReadBootJsonData(MSBuildResult result, string path)

src/Components/WebAssembly/Sdk/src/targets/Microsoft.NET.Sdk.BlazorWebAssembly.Current.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ Copyright (c) .NET Foundation. All rights reserved.
395395
<ItemGroup>
396396
<_BlazorTypeGranularAssembly
397397
Include="@(ManagedAssemblyToLink)"
398-
Condition="'%(Extension)' == '.dll' AND ($([System.String]::Copy('%(Filename)').StartsWith('Microsoft.AspNetCore.')) or $([System.String]::Copy('%(Filename)').StartsWith('Microsoft.Extensions.')))">
398+
Condition="'%(Extension)' == '.dll' AND $([System.String]::Copy('%(Filename)').StartsWith('Microsoft.AspNetCore.'))">
399399
<Required>false</Required>
400400
<Preserve>all</Preserve>
401401
</_BlazorTypeGranularAssembly>

src/Components/WebAssembly/Sdk/testassets/blazorwasm/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public static void Main(string[] args)
88
{
99
GC.KeepAlive(typeof(System.Text.Json.JsonSerializer));
1010
GC.KeepAlive(typeof(RazorClassLibrary.Class1));
11-
GC.KeepAlive(typeof(Microsoft.Extensions.Logging.ILogger));
1211
#if REFERENCE_classlibrarywithsatelliteassemblies
1312
GC.KeepAlive(typeof(classlibrarywithsatelliteassemblies.Class1));
1413
#endif

0 commit comments

Comments
 (0)