Skip to content

Commit 356a93d

Browse files
authored
Blazor WASM linker to trimmer (#19849)
1 parent 14a9317 commit 356a93d

8 files changed

Lines changed: 105 additions & 6 deletions

File tree

aspnetcore/blazor/components/class-libraries.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,16 @@ Upload the package to NuGet using the [`dotnet nuget push`](/dotnet/core/tools/d
167167

168168
## Additional resources
169169

170+
::: moniker range=">= aspnetcore-5.0"
171+
172+
* <xref:razor-pages/ui-class>
173+
* [Add an XML Intermediate Language (IL) Trimmer configuration file to a library](xref:blazor/host-and-deploy/configure-trimmer)
174+
175+
::: moniker-end
176+
177+
::: moniker range="< aspnetcore-5.0"
178+
170179
* <xref:razor-pages/ui-class>
171-
* [Add an XML linker configuration file to a library](xref:blazor/host-and-deploy/configure-linker#add-an-xml-linker-configuration-file-to-a-library)
180+
* [Add an XML Intermediate Language (IL) Linker configuration file to a library](xref:blazor/host-and-deploy/configure-linker#add-an-xml-linker-configuration-file-to-a-library)
181+
182+
::: moniker-end

aspnetcore/blazor/globalization-localization.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,23 @@ Blazor WebAssembly apps set the culture using the user's [language preference](h
5858

5959
To explicitly configure the culture, set <xref:System.Globalization.CultureInfo.DefaultThreadCurrentCulture?displayProperty=nameWithType> and <xref:System.Globalization.CultureInfo.DefaultThreadCurrentUICulture?displayProperty=nameWithType> in `Program.Main`.
6060

61-
By default, Blazor's linker configuration for Blazor WebAssembly apps strips out internationalization information except for locales explicitly requested. For more information and guidance on controlling the linker's behavior, see <xref:blazor/host-and-deploy/configure-linker#configure-the-linker-for-internationalization>.
61+
::: moniker range=">= aspnetcore-5.0"
62+
63+
By default, Blazor WebAssembly carries globalization resources required to display values, such as dates and currency, in the user's culture. If the app doesn't require localization, you may configure the app to support the invariant culture, which is based on the `en-US` culture:
64+
65+
```xml
66+
<PropertyGroup>
67+
<InvariantGlobalization>true</InvariantGlobalization>
68+
</PropertyGroup>
69+
```
70+
71+
::: moniker-end
72+
73+
::: moniker range="< aspnetcore-5.0"
74+
75+
By default, the Intermediate Language (IL) Linker configuration for Blazor WebAssembly apps strips out internationalization information except for locales explicitly requested. For more information, see <xref:blazor/host-and-deploy/configure-linker#configure-the-linker-for-internationalization>.
76+
77+
::: moniker-end
6278

6379
While the culture that Blazor selects by default might be sufficient for most users, consider offering a way for users to specify their preferred locale. For a Blazor WebAssembly sample app with a culture picker, see the [`LocSample`](https://github.com/pranavkm/LocSample) localization sample app.
6480

aspnetcore/blazor/host-and-deploy/configure-linker.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Configure the Linker for ASP.NET Core Blazor
33
author: guardrex
44
description: Learn how to control the Intermediate Language (IL) Linker when building a Blazor app.
5-
monikerRange: '>= aspnetcore-3.1'
5+
monikerRange: '>= aspnetcore-3.1 <= aspnetcore-5.0'
66
ms.author: riande
77
ms.custom: mvc
88
ms.date: 05/19/2020
@@ -15,7 +15,7 @@ By [Luke Latham](https://github.com/guardrex)
1515

1616
Blazor WebAssembly performs [Intermediate Language (IL)](/dotnet/standard/managed-code#intermediate-language--execution) linking during a build to trim unnecessary IL from the app's output assemblies. The linker is disabled when building in Debug configuration. Apps must build in Release configuration to enable the linker. We recommend building in Release when deploying your Blazor WebAssembly apps.
1717

18-
Linking an app optimizes for size but may have detrimental effects. Apps that use reflection or related dynamic features may break when trimmed because the linker doesn't know about this dynamic behavior and can't determine in general which types are required for reflection at runtime. To trim such apps, the linker must be informed about any types required by reflection in the code and in packages or frameworks that the app depends on.
18+
Linking an app optimizes for size but may have detrimental effects. Apps that use reflection or related dynamic features may break when trimmed because the linker doesn't know about this dynamic behavior and can't determine in general which types are required for reflection at runtime. To trim such apps, the linker must be informed about any types required by reflection in the code and in packages or frameworks that the app depends on.
1919

2020
To ensure the trimmed app works correctly once deployed, it's important to test Release builds of the app frequently while developing.
2121

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: Configure the Trimmer for ASP.NET Core Blazor
3+
author: guardrex
4+
description: Learn how to control the Intermediate Language (IL) Linker (Trimmer) when building a Blazor app.
5+
monikerRange: '>= aspnetcore-5.0'
6+
ms.author: riande
7+
ms.custom: mvc
8+
ms.date: 09/14/2020
9+
no-loc: ["ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
10+
uid: blazor/host-and-deploy/configure-trimmer
11+
---
12+
# Configure the Trimmer for ASP.NET Core Blazor
13+
14+
By [Pranav Krishnamoorthy](https://github.com/pranavkm)
15+
16+
Blazor WebAssembly performs [Intermediate Language (IL)](/dotnet/standard/managed-code#intermediate-language--execution) trimming to reduce the size of the published output.
17+
18+
Trimming an app optimizes for size but may have detrimental effects. Apps that use reflection or related dynamic features may break when trimmed because the trimmer doesn't know about dynamic behavior and can't determine in general which types are required for reflection at runtime. To trim such apps, the trimmer must be informed about any types required by reflection in the code and in packages or frameworks that the app depends on.
19+
20+
To ensure the trimmed app works correctly once deployed, it's important to test published output frequently while developing.
21+
22+
Trimming for .NET apps can be disabled by setting the `PublishTrimmed` MSBuild property to `false` in the app's project file:
23+
24+
```xml
25+
<PropertyGroup>
26+
<PublishTrimmed>false</PublishTrimmed>
27+
</PropertyGroup>
28+
```
29+
30+
## Additional resources
31+
32+
* [Trim self-contained deployments and executables](/dotnet/core/deploying/trim-self-contained)
33+
* <xref:blazor/webassembly-performance-best-practices#intermediate-language-il-trimming>

aspnetcore/blazor/host-and-deploy/webassembly.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,10 +692,22 @@ The `--urls` argument sets the IP addresses or host addresses with ports and pro
692692
--urls=http://127.0.0.1:0
693693
```
694694

695+
::: moniker range=">= aspnetcore-5.0"
696+
697+
## Configure the Trimmer
698+
699+
Blazor performs Intermediate Language (IL) trimming on each Release build to remove unnecessary IL from the output assemblies. For more information, see <xref:blazor/host-and-deploy/configure-trimmer>.
700+
701+
::: moniker-end
702+
703+
::: moniker range="< aspnetcore-5.0"
704+
695705
## Configure the Linker
696706

697707
Blazor performs Intermediate Language (IL) linking on each Release build to remove unnecessary IL from the output assemblies. For more information, see <xref:blazor/host-and-deploy/configure-linker>.
698708

709+
::: moniker-end
710+
699711
## Custom boot resource loading
700712

701713
A Blazor WebAssembly app can be initialized with the `loadBootResource` function to override the built-in boot resource loading mechanism. Use `loadBootResource` for the following scenarios:

aspnetcore/blazor/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,22 @@ When a Blazor WebAssembly app is built and run in a browser:
115115

116116
The size of the published app, its *payload size*, is a critical performance factor for an app's useability. A large app takes a relatively long time to download to a browser, which diminishes the user experience. Blazor WebAssembly optimizes payload size to reduce download times:
117117

118+
::: moniker range=">= aspnetcore-5.0"
119+
120+
* Unused code is stripped out of the app when it's published by the [Intermediate Language (IL) Trimmer](xref:blazor/host-and-deploy/configure-trimmer).
121+
* HTTP responses are compressed.
122+
* The .NET runtime and assemblies are cached in the browser.
123+
124+
::: moniker-end
125+
126+
::: moniker range="< aspnetcore-5.0"
127+
118128
* Unused code is stripped out of the app when it's published by the [Intermediate Language (IL) Linker](xref:blazor/host-and-deploy/configure-linker).
119129
* HTTP responses are compressed.
120130
* The .NET runtime and assemblies are cached in the browser.
121131

132+
::: moniker-end
133+
122134
## Blazor Server
123135

124136
Blazor decouples component rendering logic from how UI updates are applied. Blazor Server provides support for hosting Razor components on the server in an ASP.NET Core app. UI updates are handled over a [SignalR](xref:signalr/introduction) connection.

aspnetcore/blazor/webassembly-performance-best-practices.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,21 @@ Blazor WebAssembly offers two additional versions of <xref:Microsoft.JSInterop.I
125125
126126
## Reduce app size
127127

128+
::: moniker range=">= aspnetcore-5.0"
129+
130+
### Intermediate Language (IL) trimming
131+
132+
[Trimming unused assemblies from a Blazor WebAssembly app](xref:blazor/host-and-deploy/configure-trimmer) reduces the app's size by removing unused code in the app's binaries. By default, the Trimmer is executed when publishing an application. To benefit from trimming, publish the app for deployment using the [`dotnet publish`](/dotnet/core/tools/dotnet-publish) command with the [-c|--configuration](/dotnet/core/tools/dotnet-publish#options) option set to `Release`:
133+
134+
::: moniker-end
135+
136+
::: moniker range="< aspnetcore-5.0"
137+
128138
### Intermediate Language (IL) linking
129139

130-
[Linking a Blazor WebAssembly app](xref:blazor/host-and-deploy/configure-linker) reduces the app's size by trimming unused code in the app's binaries. By default, the linker is only enabled when building in `Release` configuration. To benefit from this, publish the app for deployment using the [`dotnet publish`](/dotnet/core/tools/dotnet-publish) command with the [-c|--configuration](/dotnet/core/tools/dotnet-publish#options) option set to `Release`:
140+
[Linking a Blazor WebAssembly app](xref:blazor/host-and-deploy/configure-linker) reduces the app's size by trimming unused code in the app's binaries. By default, the Intermediate Language (IL) Linker is only enabled when building in `Release` configuration. To benefit from this, publish the app for deployment using the [`dotnet publish`](/dotnet/core/tools/dotnet-publish) command with the [-c|--configuration](/dotnet/core/tools/dotnet-publish#options) option set to `Release`:
141+
142+
::: moniker-end
131143

132144
```dotnetcli
133145
dotnet publish -c Release
@@ -157,13 +169,14 @@ Blazor WebAssembly's runtime includes the following .NET features that can be di
157169

158170
::: moniker range=">= aspnetcore-5.0"
159171

160-
* By default, Blazor WebAssembly carries globalization resources required to display values, such as dates and currency, in the user's culture. If the app doesn't require localization, you may configure the app to support the invariant culture, which is based on the `en-US` culture:
172+
* By default, Blazor WebAssembly carries globalization resources required to display values, such as dates and currency, in the user's culture. If the app doesn't require localization, you may [configure the app to support the invariant culture](xref:blazor/globalization-localization), which is based on the `en-US` culture:
161173

162174
```xml
163175
<PropertyGroup>
164176
<InvariantGlobalization>true</InvariantGlobalization>
165177
</PropertyGroup>
166178
```
179+
167180
::: moniker-end
168181

169182
::: moniker range="< aspnetcore-5.0"

aspnetcore/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@
447447
uid: blazor/host-and-deploy/server
448448
- name: Configure the Linker
449449
uid: blazor/host-and-deploy/configure-linker
450+
- name: Configure the Trimmer
451+
uid: blazor/host-and-deploy/configure-trimmer
450452
- name: Blazor Server and EF Core
451453
uid: blazor/blazor-server-ef-core
452454
- name: Advanced scenarios

0 commit comments

Comments
 (0)