Skip to content

Commit 2106da3

Browse files
authored
[StaticWebAssets] Optimize compression (#47734)
* Fix issue where publish compression was disabled when build compression was disabled * Support passing a custom compression level with BrotliCompressionLevel * Compress only current project assets for Razor Class Libraries and compress assets missing compression on Web and Blazor scenarios. * Support disabling compression for discovered assets (stuff in the wwwroot folder) * Create endpoints for package assets without endpoints
1 parent 4bd632b commit 2106da3

File tree

96 files changed

+3306
-2121
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+3306
-2121
lines changed

src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.Compression.targets

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,13 @@ Copyright (c) .NET Foundation. All rights reserved.
161161
</PropertyGroup>
162162

163163
<PropertyGroup>
164-
<BuildCompressionFormats>$(BuildCompressionFormats);gzip</BuildCompressionFormats>
165-
<PublishCompressionFormats>$(PublishCompressionFormats);gzip;brotli</PublishCompressionFormats>
164+
<EnableDefaultCompressionFormats Condition="'$(EnableDefaultCompressionFormats)' == ''">true</EnableDefaultCompressionFormats>
165+
<BuildCompressionFormats Condition="'$(EnableDefaultCompressionFormats)' == 'true'">$(BuildCompressionFormats);gzip</BuildCompressionFormats>
166+
<PublishCompressionFormats Condition="'$(EnableDefaultCompressionFormats)' == 'true'">$(PublishCompressionFormats);gzip;brotli</PublishCompressionFormats>
166167
<DisableBuildCompression Condition="'$(DisableBuildCompression)' == ''">false</DisableBuildCompression>
167-
<CompressionIncludePatterns>$(CompressionIncludePatterns)</CompressionIncludePatterns>
168-
<CompressionExcludePatterns>$(CompressionExcludePatterns)</CompressionExcludePatterns>
168+
<CompressDiscoveredAssetsDuringBuild Condition="$(CompressDiscoveredAssetsDuringBuild) == ''">true</CompressDiscoveredAssetsDuringBuild>
169+
<!-- Support passing in a custom compression level to brotli and respect the old internal flag for blazor -->
170+
<BrotliCompressionLevel Condition="'$(_BlazorBrotliCompressionLevel)' != ''">$(_BlazorBrotliCompressionLevel)</BrotliCompressionLevel>
169171
</PropertyGroup>
170172

171173
<PropertyGroup>
@@ -195,7 +197,7 @@ Copyright (c) .NET Foundation. All rights reserved.
195197
GeneratePublishCompressedStaticWebAssets
196198
ResolvePublishCompressedStaticWebAssetsConfiguration
197199
-->
198-
<ResolvePublishRelatedStaticWebAssetsDependsOn Condition="'$(DisableBuildCompression)' != 'true'">
200+
<ResolvePublishRelatedStaticWebAssetsDependsOn>
199201
ResolvePublishCompressedStaticWebAssets;
200202
$(ResolvePublishRelatedStaticWebAssetsDependsOn)
201203
</ResolvePublishRelatedStaticWebAssetsDependsOn>
@@ -270,7 +272,7 @@ Copyright (c) .NET Foundation. All rights reserved.
270272

271273
<BrotliCompress Condition="'@(_BrotliCompressedStaticWebAssets)' != ''"
272274
FilesToCompress="@(_BrotliCompressedStaticWebAssets)"
273-
CompressionLevel="$(_BlazorBrotliCompressionLevel)"
275+
CompressionLevel="$(BrotliCompressionLevel)"
274276
ToolAssembly="$(_StaticWebAssetsSdkToolAssembly)"
275277
ToolExe="$(_DotNetHostFileName)"
276278
ToolPath="$(_DotNetHostDirectory)" />
@@ -304,8 +306,29 @@ Copyright (c) .NET Foundation. All rights reserved.
304306
<StaticWebAsset Include="@(_PrecompressedStaticWebAssets)" />
305307
</ItemGroup>
306308

309+
<PropertyGroup>
310+
<StaticWebAssetBuildCompressAllAssets Condition="'$(StaticWebAssetBuildCompressAllAssets)' == '' and '$(StaticWebAssetProjectMode)' == 'Root'">true</StaticWebAssetBuildCompressAllAssets>
311+
<StaticWebAssetBuildCompressAllAssets Condition="'$(StaticWebAssetBuildCompressAllAssets)' == ''">false</StaticWebAssetBuildCompressAllAssets>
312+
</PropertyGroup>
313+
314+
<ItemGroup Condition="'$(StaticWebAssetBuildCompressAllAssets)' == 'true'">
315+
<_CandidateAssetsForBuild Include="@(StaticWebAsset)" />
316+
</ItemGroup>
317+
318+
<ItemGroup Condition="'$(StaticWebAssetBuildCompressAllAssets)' != 'true'">
319+
<_CandidateAssetsForBuild
320+
Include="@(StaticWebAsset)"
321+
Condition="'%(SourceType)' == 'Discovered' or '%(SourceType)' == 'Computed'" />
322+
</ItemGroup>
323+
324+
<ItemGroup Condition="'$(CompressDiscoveredAssetsDuringBuild)' != 'true'">
325+
<_CandidateAssetsForBuild
326+
Remove="@(_CandidateAssetsForBuild)"
327+
Condition="'%(SourceType)' == 'Discovered'" />
328+
</ItemGroup>
329+
307330
<ResolveCompressedAssets
308-
CandidateAssets="@(StaticWebAsset)"
331+
CandidateAssets="@(_CandidateAssetsForBuild)"
309332
Formats="$(BuildCompressionFormats)"
310333
IncludePatterns="$(CompressionIncludePatterns)"
311334
ExcludePatterns="$(CompressionExcludePatterns)"
@@ -386,12 +409,23 @@ Copyright (c) .NET Foundation. All rights reserved.
386409

387410
<Target Name="ResolvePublishCompressedStaticWebAssetsConfiguration" DependsOnTargets="ResolvePublishStaticWebAssets;$(ResolveCompressedFilesForPublishDependsOn)">
388411

389-
<ItemGroup>
412+
<PropertyGroup>
413+
<StaticWebAssetPublishCompressAllAssets Condition="'$(StaticWebAssetPublishCompressAllAssets)' == '' and '$(StaticWebAssetProjectMode)' == 'Root'">true</StaticWebAssetPublishCompressAllAssets>
414+
<StaticWebAssetPublishCompressAllAssets Condition="'$(StaticWebAssetPublishCompressAllAssets)' == ''">false</StaticWebAssetPublishCompressAllAssets>
415+
</PropertyGroup>
416+
417+
<ItemGroup Condition="'$(StaticWebAssetPublishCompressAllAssets)' == 'true'">
390418
<_CandidateAssetsForPublish
391419
Include="@(StaticWebAsset)"
392420
Condition="'%(AssetKind)' != 'Build'" />
393421
</ItemGroup>
394422

423+
<ItemGroup Condition="'$(StaticWebAssetPublishCompressAllAssets)' != 'true'">
424+
<_CandidateAssetsForPublish
425+
Include="@(StaticWebAsset)"
426+
Condition="'%(AssetKind)' != 'Build' and ('%(SourceType)' == 'Discovered' or '%(SourceType)' == 'Computed')" />
427+
</ItemGroup>
428+
395429
<ResolveCompressedAssets
396430
CandidateAssets="@(_CandidateAssetsForPublish)"
397431
Formats="$(PublishCompressionFormats)"

src/StaticWebAssetsSdk/Targets/Microsoft.NET.Sdk.StaticWebAssets.targets

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,10 +717,23 @@ Copyright (c) .NET Foundation. All rights reserved.
717717
<Output TaskParameter="OriginalAssets" ItemName="_OriginalPackageAssets" />
718718
</UpdatePackageStaticWebAssets>
719719

720+
<ItemGroup>
721+
<_AssetsWithoutEndpoints Include="@(_UpdatedPackageAssets)" Exclude="@(StaticWebAssetEndpoint->'%(AssetFile)')" />
722+
</ItemGroup>
723+
724+
<DefineStaticWebAssetEndpoints
725+
CandidateAssets="@(_AssetsWithoutEndpoints)"
726+
ContentTypeMappings="@(StaticWebAssetContentTypeMapping)"
727+
>
728+
<Output TaskParameter="Endpoints" ItemName="_UpdatedPackageAssetsEndpoint" />
729+
</DefineStaticWebAssetEndpoints>
730+
720731
<ItemGroup>
721732
<StaticWebAsset Remove="@(_OriginalPackageAssets)"/>
722733
<StaticWebAsset Include="@(_UpdatedPackageAssets)" />
734+
<StaticWebAssetEndpoint Include="@(_UpdatedPackageAssetsEndpoint)" />
723735
</ItemGroup>
736+
724737
</Target>
725738

726739
<Import Project="Microsoft.NET.Sdk.StaticWebAssets.Publish.targets" />

src/WebSdk/Web/Targets/Sdk.Server.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Copyright (c) .NET Foundation. All rights reserved.
2121
<!-- Default properties that shouldn't be replaced by the microsoft.net.sdk.props -->
2222
<PropertyGroup>
2323
<DebugSymbols Condition="'$(DebugSymbols)' == ''">true</DebugSymbols>
24+
<StaticWebAssetProjectMode>Root</StaticWebAssetProjectMode>
25+
<StaticWebAssetBasePath>/</StaticWebAssetBasePath>
2426
</PropertyGroup>
2527

2628
<Import Sdk="Microsoft.NET.Sdk" Project="Sdk.props" />

test/Microsoft.NET.Sdk.BlazorWebAssembly.Tests/StaticWebAssetsBaselines/JSModules_ManifestIncludesModuleTargetPaths.Build.staticwebassets.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"Version": 1,
33
"Hash": "__hash__",
44
"Source": "blazorhosted",
5-
"BasePath": "_content/blazorhosted",
6-
"Mode": "Default",
5+
"BasePath": "/",
6+
"Mode": "Root",
77
"ManifestType": "Build",
88
"ReferencedProjectsConfiguration": [
99
{
@@ -47,11 +47,11 @@
4747
],
4848
"Assets": [
4949
{
50-
"Identity": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\_content\\blazorhosted\\blazorhosted.modules.json.gz",
50+
"Identity": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\blazorhosted.modules.json.gz",
5151
"SourceId": "blazorhosted",
5252
"SourceType": "Computed",
5353
"ContentRoot": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\",
54-
"BasePath": "_content/blazorhosted",
54+
"BasePath": "/",
5555
"RelativePath": "blazorhosted.modules.json.gz",
5656
"AssetKind": "Build",
5757
"AssetMode": "CurrentProject",
@@ -65,7 +65,7 @@
6565
"Integrity": "__integrity__",
6666
"CopyToOutputDirectory": "Never",
6767
"CopyToPublishDirectory": "PreserveNewest",
68-
"OriginalItemSpec": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\jsmodules\\_content\\blazorhosted\\blazorhosted.modules.json.gz",
68+
"OriginalItemSpec": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\jsmodules\\blazorhosted.modules.json.gz",
6969
"FileLength": -1,
7070
"LastWriteTime": "0001-01-01T00:00:00+00:00"
7171
},
@@ -74,7 +74,7 @@
7474
"SourceId": "blazorhosted",
7575
"SourceType": "Computed",
7676
"ContentRoot": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\jsmodules\\",
77-
"BasePath": "_content/blazorhosted",
77+
"BasePath": "/",
7878
"RelativePath": "blazorhosted.modules.json",
7979
"AssetKind": "Build",
8080
"AssetMode": "CurrentProject",
@@ -10032,7 +10032,7 @@
1003210032
"Endpoints": [
1003310033
{
1003410034
"Route": "blazorhosted.modules.json.gz",
10035-
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\_content\\blazorhosted\\blazorhosted.modules.json.gz",
10035+
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\blazorhosted.modules.json.gz",
1003610036
"Selectors": [],
1003710037
"ResponseHeaders": [
1003810038
{
@@ -10077,7 +10077,7 @@
1007710077
},
1007810078
{
1007910079
"Route": "blazorhosted.modules.json",
10080-
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\_content\\blazorhosted\\blazorhosted.modules.json.gz",
10080+
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\blazorhosted.modules.json.gz",
1008110081
"Selectors": [
1008210082
{
1008310083
"Name": "Content-Encoding",

test/Microsoft.NET.Sdk.BlazorWebAssembly.Tests/StaticWebAssetsBaselines/JsModules_Hosted_CanCustomizeBlazorInitialization.Publish.files.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@
245245
"${OutputPath}\\wwwroot\\serviceworkers\\my-service-worker.js",
246246
"${OutputPath}\\wwwroot\\serviceworkers\\my-service-worker.js.br",
247247
"${OutputPath}\\wwwroot\\serviceworkers\\my-service-worker.js.gz",
248-
"${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\_content\\blazorhosted\\blazorhosted.modules.json.br",
249-
"${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\_content\\blazorhosted\\blazorhosted.modules.json.gz",
248+
"${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\blazorhosted.modules.json.br",
249+
"${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\blazorhosted.modules.json.gz",
250250
"${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\jsmodules\\jsmodules.publish.manifest.json",
251251
"${OutputPath}\\wwwroot\\Fake-License.txt",
252252
"${OutputPath}\\wwwroot\\Fake-License.txt.br",

test/Microsoft.NET.Sdk.BlazorWebAssembly.Tests/StaticWebAssetsBaselines/JsModules_Hosted_CanCustomizeBlazorInitialization.Publish.staticwebassets.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"Version": 1,
33
"Hash": "__hash__",
44
"Source": "blazorhosted",
5-
"BasePath": "_content/blazorhosted",
6-
"Mode": "Default",
5+
"BasePath": "/",
6+
"Mode": "Root",
77
"ManifestType": "Publish",
88
"ReferencedProjectsConfiguration": [
99
{
@@ -47,11 +47,11 @@
4747
],
4848
"Assets": [
4949
{
50-
"Identity": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\_content\\blazorhosted\\blazorhosted.modules.json.br",
50+
"Identity": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\blazorhosted.modules.json.br",
5151
"SourceId": "blazorhosted",
5252
"SourceType": "Computed",
5353
"ContentRoot": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\",
54-
"BasePath": "_content/blazorhosted",
54+
"BasePath": "/",
5555
"RelativePath": "blazorhosted.modules.json.br",
5656
"AssetKind": "Publish",
5757
"AssetMode": "CurrentProject",
@@ -65,16 +65,16 @@
6565
"Integrity": "__integrity__",
6666
"CopyToOutputDirectory": "Never",
6767
"CopyToPublishDirectory": "PreserveNewest",
68-
"OriginalItemSpec": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\jsmodules\\_content\\blazorhosted\\blazorhosted.modules.json.br",
68+
"OriginalItemSpec": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\jsmodules\\blazorhosted.modules.json.br",
6969
"FileLength": -1,
7070
"LastWriteTime": "0001-01-01T00:00:00+00:00"
7171
},
7272
{
73-
"Identity": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\_content\\blazorhosted\\blazorhosted.modules.json.gz",
73+
"Identity": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\blazorhosted.modules.json.gz",
7474
"SourceId": "blazorhosted",
7575
"SourceType": "Computed",
7676
"ContentRoot": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\",
77-
"BasePath": "_content/blazorhosted",
77+
"BasePath": "/",
7878
"RelativePath": "blazorhosted.modules.json.gz",
7979
"AssetKind": "Publish",
8080
"AssetMode": "CurrentProject",
@@ -88,7 +88,7 @@
8888
"Integrity": "__integrity__",
8989
"CopyToOutputDirectory": "Never",
9090
"CopyToPublishDirectory": "PreserveNewest",
91-
"OriginalItemSpec": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\jsmodules\\_content\\blazorhosted\\blazorhosted.modules.json.gz",
91+
"OriginalItemSpec": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\jsmodules\\blazorhosted.modules.json.gz",
9292
"FileLength": -1,
9393
"LastWriteTime": "0001-01-01T00:00:00+00:00"
9494
},
@@ -97,7 +97,7 @@
9797
"SourceId": "blazorhosted",
9898
"SourceType": "Computed",
9999
"ContentRoot": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\jsmodules\\",
100-
"BasePath": "_content/blazorhosted",
100+
"BasePath": "/",
101101
"RelativePath": "blazorhosted.modules.json",
102102
"AssetKind": "Publish",
103103
"AssetMode": "CurrentProject",
@@ -5708,7 +5708,7 @@
57085708
"Endpoints": [
57095709
{
57105710
"Route": "blazorhosted.modules.json.br",
5711-
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\_content\\blazorhosted\\blazorhosted.modules.json.br",
5711+
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\blazorhosted.modules.json.br",
57125712
"Selectors": [],
57135713
"ResponseHeaders": [
57145714
{
@@ -5753,7 +5753,7 @@
57535753
},
57545754
{
57555755
"Route": "blazorhosted.modules.json",
5756-
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\_content\\blazorhosted\\blazorhosted.modules.json.br",
5756+
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\blazorhosted.modules.json.br",
57575757
"Selectors": [
57585758
{
57595759
"Name": "Content-Encoding",
@@ -5808,7 +5808,7 @@
58085808
},
58095809
{
58105810
"Route": "blazorhosted.modules.json.gz",
5811-
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\_content\\blazorhosted\\blazorhosted.modules.json.gz",
5811+
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\blazorhosted.modules.json.gz",
58125812
"Selectors": [],
58135813
"ResponseHeaders": [
58145814
{
@@ -5853,7 +5853,7 @@
58535853
},
58545854
{
58555855
"Route": "blazorhosted.modules.json",
5856-
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\_content\\blazorhosted\\blazorhosted.modules.json.gz",
5856+
"AssetFile": "${ProjectPath}\\blazorhosted\\obj\\Debug\\${Tfm}\\compressed\\publish\\blazorhosted.modules.json.gz",
58575857
"Selectors": [
58585858
{
58595859
"Name": "Content-Encoding",

test/Microsoft.NET.Sdk.BlazorWebAssembly.Tests/StaticWebAssetsBaselines/Publish60Hosted_Works.Publish.staticwebassets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"Version": 1,
33
"Hash": "__hash__",
44
"Source": "BlazorWasmHosted60.Server",
5-
"BasePath": "_content/BlazorWasmHosted60.Server",
6-
"Mode": "Default",
5+
"BasePath": "/",
6+
"Mode": "Root",
77
"ManifestType": "Publish",
88
"ReferencedProjectsConfiguration": [
99
{

test/Microsoft.NET.Sdk.BlazorWebAssembly.Tests/StaticWebAssetsBaselines/StaticWebAssets_BackCompatibilityPublish_Hosted_Works.Publish.staticwebassets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"Version": 1,
33
"Hash": "__hash__",
44
"Source": "blazorhosted",
5-
"BasePath": "_content/blazorhosted",
6-
"Mode": "Default",
5+
"BasePath": "/",
6+
"Mode": "Root",
77
"ManifestType": "Publish",
88
"ReferencedProjectsConfiguration": [
99
{

test/Microsoft.NET.Sdk.BlazorWebAssembly.Tests/StaticWebAssetsBaselines/StaticWebAssets_Build_Hosted_Works.Build.staticwebassets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"Version": 1,
33
"Hash": "__hash__",
44
"Source": "blazorhosted",
5-
"BasePath": "_content/blazorhosted",
6-
"Mode": "Default",
5+
"BasePath": "/",
6+
"Mode": "Root",
77
"ManifestType": "Build",
88
"ReferencedProjectsConfiguration": [
99
{

test/Microsoft.NET.Sdk.BlazorWebAssembly.Tests/StaticWebAssetsBaselines/StaticWebAssets_HostedApp_ReferencingNetStandardLibrary_Works.Build.staticwebassets.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"Version": 1,
33
"Hash": "__hash__",
44
"Source": "blazorhosted",
5-
"BasePath": "_content/blazorhosted",
6-
"Mode": "Default",
5+
"BasePath": "/",
6+
"Mode": "Root",
77
"ManifestType": "Build",
88
"ReferencedProjectsConfiguration": [
99
{

0 commit comments

Comments
 (0)