Enable trimming for Microsoft.Extensions packages (#24986)

* Enable trimming for Microsoft.Extensions packages

Fixes https://github.com/dotnet/aspnetcore/issues/24634

* Update Microsoft.NET.Sdk.BlazorWebAssembly.csproj

* Apply suggestions from code review

* Fixup test
This commit is contained in:
Pranav K 2020-08-19 09:29:52 -07:00 committed by GitHub
parent 187eacd570
commit d2f34d6d99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 12 deletions

View File

@ -141,8 +141,6 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
serviceWorkerPath: Path.Combine("serviceworkers", "my-service-worker.js"),
serviceWorkerContent: "// This is the production service worker",
assetsManifestPath: "custom-service-worker-assets.js");
VerifyTypeGranularTrimming(result, blazorPublishDirectory);
}
[Fact]
@ -906,16 +904,17 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
private void VerifyTypeGranularTrimming(MSBuildResult result, string blazorPublishDirectory)
{
var loggingAssemblyPath = Path.Combine(blazorPublishDirectory, "_framework", "Microsoft.Extensions.Logging.Abstractions.dll");
Assert.FileExists(result, loggingAssemblyPath);
var componentsShimAssemblyPath = Path.Combine(blazorPublishDirectory, "_framework", "Microsoft.AspNetCore.Razor.Test.ComponentShim.dll");
Assert.FileExists(result, componentsShimAssemblyPath);
// ILogger is referenced by the app, so we expect it to be preserved
Assert.AssemblyContainsType(result, loggingAssemblyPath, "Microsoft.Extensions.Logging.ILogger");
// LogLevel is referenced by ILogger and therefore must be preserved.
Assert.AssemblyContainsType(result, loggingAssemblyPath, "Microsoft.Extensions.Logging.LogLevel");
// RouteView is referenced by the app, so we expect it to be preserved
Assert.AssemblyContainsType(result, componentsShimAssemblyPath, "Microsoft.AspNetCore.Components.RouteView");
// NullLogger is not referenced by the app, and should be trimmed.
Assert.AssemblyDoesNotContainType(result, loggingAssemblyPath, "Microsoft.Extensions.Logging.Abstractions.NullLogger");
// RouteData is referenced by RouteView so we expect it to be preserved.
Assert.AssemblyContainsType(result, componentsShimAssemblyPath, "Microsoft.AspNetCore.Components.RouteData");
// CascadingParameterAttribute is not referenced by the app, and should be trimmed.
Assert.AssemblyDoesNotContainType(result, componentsShimAssemblyPath, "Microsoft.AspNetCore.Components.CascadingParameterAttribute");
}
private static BootJsonData ReadBootJsonData(MSBuildResult result, string path)

View File

@ -395,7 +395,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<ItemGroup>
<_BlazorTypeGranularAssembly
Include="@(ManagedAssemblyToLink)"
Condition="'%(Extension)' == '.dll' AND ($([System.String]::Copy('%(Filename)').StartsWith('Microsoft.AspNetCore.')) or $([System.String]::Copy('%(Filename)').StartsWith('Microsoft.Extensions.')))">
Condition="'%(Extension)' == '.dll' AND $([System.String]::Copy('%(Filename)').StartsWith('Microsoft.AspNetCore.'))">
<Required>false</Required>
<Preserve>all</Preserve>
</_BlazorTypeGranularAssembly>

View File

@ -8,7 +8,6 @@ namespace standalone
{
GC.KeepAlive(typeof(System.Text.Json.JsonSerializer));
GC.KeepAlive(typeof(RazorClassLibrary.Class1));
GC.KeepAlive(typeof(Microsoft.Extensions.Logging.ILogger));
#if REFERENCE_classlibrarywithsatelliteassemblies
GC.KeepAlive(typeof(classlibrarywithsatelliteassemblies.Class1));
#endif