Skip to content

Commit

Permalink
Move asset compression to the StaticWebAssets SDK (#31559)
Browse files Browse the repository at this point in the history
  • Loading branch information
MackinnonBuck authored Apr 14, 2023
1 parent a59ee31 commit cf2977b
Show file tree
Hide file tree
Showing 43 changed files with 4,396 additions and 3,581 deletions.
10 changes: 10 additions & 0 deletions sdk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tasks", "Tasks", "{6C8E1E65
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.StaticWebAssets.Tasks", "src\StaticWebAssetsSdk\Tasks\Microsoft.NET.Sdk.StaticWebAssets.Tasks.csproj", "{3D42A6BA-38DB-4AA5-9F86-8A78D00D9A07}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tool", "Tool", "{51A31407-7C6A-42E9-93B8-F21DEC518A34}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.NET.Sdk.StaticWebAssets.Tool", "src\StaticWebAssetsSdk\Tool\Microsoft.NET.Sdk.StaticWebAssets.Tool.csproj", "{B8550E71-0CBE-4F88-AC79-2003EFED1DE1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WasmSdk", "WasmSdk", "{21835A9E-D667-4761-8675-B2BC105CC2FE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Targets", "Targets", "{71BC1256-D225-4272-ADB2-95264E09AF73}"
Expand Down Expand Up @@ -856,6 +860,10 @@ Global
{3D42A6BA-38DB-4AA5-9F86-8A78D00D9A07}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D42A6BA-38DB-4AA5-9F86-8A78D00D9A07}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D42A6BA-38DB-4AA5-9F86-8A78D00D9A07}.Release|Any CPU.Build.0 = Release|Any CPU
{B8550E71-0CBE-4F88-AC79-2003EFED1DE1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B8550E71-0CBE-4F88-AC79-2003EFED1DE1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8550E71-0CBE-4F88-AC79-2003EFED1DE1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8550E71-0CBE-4F88-AC79-2003EFED1DE1}.Release|Any CPU.Build.0 = Release|Any CPU
{754C18B9-AEDB-4455-BAF4-844C6CEEF8F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{754C18B9-AEDB-4455-BAF4-844C6CEEF8F7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{754C18B9-AEDB-4455-BAF4-844C6CEEF8F7}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -1034,6 +1042,8 @@ Global
{C5C73211-A713-40BF-8FC8-A690CCBE66F5} = {9E9F3BB2-6FED-47BC-869C-BFAF6E7C85FC}
{6C8E1E65-EA73-434D-818F-E564BFFE9F86} = {9E9F3BB2-6FED-47BC-869C-BFAF6E7C85FC}
{3D42A6BA-38DB-4AA5-9F86-8A78D00D9A07} = {6C8E1E65-EA73-434D-818F-E564BFFE9F86}
{51A31407-7C6A-42E9-93B8-F21DEC518A34} = {9E9F3BB2-6FED-47BC-869C-BFAF6E7C85FC}
{B8550E71-0CBE-4F88-AC79-2003EFED1DE1} = {51A31407-7C6A-42E9-93B8-F21DEC518A34}
{21835A9E-D667-4761-8675-B2BC105CC2FE} = {22AB674F-ED91-4FBC-BFEE-8A1E82F9F05E}
{71BC1256-D225-4272-ADB2-95264E09AF73} = {21835A9E-D667-4761-8675-B2BC105CC2FE}
{B2CE3F28-8FEC-4715-B483-5B442E3136CE} = {21835A9E-D667-4761-8675-B2BC105CC2FE}
Expand Down
1 change: 1 addition & 0 deletions source-build.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"src\\Resolvers\\Microsoft.NET.Sdk.WorkloadManifestReader\\Microsoft.NET.Sdk.WorkloadManifestReader.csproj",
"src\\Resolvers\\WorkloadManifestValidator\\WorkloadManifestValidator.csproj",
"src\\StaticWebAssetsSdk\\Tasks\\Microsoft.NET.Sdk.StaticWebAssets.Tasks.csproj",
"src\\StaticWebAssetsSdk\\Tool\\Microsoft.NET.Sdk.StaticWebAssets.Tool.csproj",
"src\\Tasks\\Microsoft.NET.Build.Extensions.Tasks\\Microsoft.NET.Build.Extensions.Tasks.csproj",
"src\\Tasks\\Microsoft.NET.Build.Tasks\\Microsoft.NET.Build.Tasks.csproj",
"src\\WebSdk\\ProjectSystem\\Tasks\\Microsoft.NET.Sdk.Web.ProjectSystem.Tasks.csproj",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<_BlazorWebAssemblySdkToolAssembly>$(BlazorWebAssemblySdkDirectoryRoot)tools\net8.0\Microsoft.NET.Sdk.BlazorWebAssembly.Tool.dll</_BlazorWebAssemblySdkToolAssembly>
</PropertyGroup>

<UsingTask TaskName="Microsoft.NET.Sdk.BlazorWebAssembly.BrotliCompress" AssemblyFile="$(_BlazorWebAssemblySdkTasksAssembly)" />
<UsingTask TaskName="Microsoft.NET.Sdk.BlazorWebAssembly.GzipCompress" AssemblyFile="$(_BlazorWebAssemblySdkTasksAssembly)" />
<UsingTask TaskName="Microsoft.NET.Sdk.BlazorWebAssembly.CreateBlazorTrimmerRootDescriptorFile" AssemblyFile="$(_BlazorWebAssemblySdkTasksAssembly)" />
<UsingTask TaskName="Microsoft.NET.Sdk.BlazorWebAssembly.ComputeBlazorFilesToCompress" AssemblyFile="$(_BlazorWebAssemblySdkTasksAssembly)" />
<UsingTask TaskName="Microsoft.NET.Sdk.BlazorWebAssembly.GenerateBlazorBootExtensionJson" AssemblyFile="$(_BlazorWebAssemblySdkTasksAssembly)" />

<PropertyGroup>
Expand All @@ -60,24 +57,21 @@ Copyright (c) .NET Foundation. All rights reserved.

<DisableAutoWasmBuildApp>true</DisableAutoWasmBuildApp>
<DisableAutoWasmPublishApp>true</DisableAutoWasmPublishApp>
<BlazorEnableCompression Condition="'$(WasmBuildingForNestedPublish)' == 'true'">false</BlazorEnableCompression>
<WasmNestedPublishAppDependsOn>_GatherBlazorFilesToPublish;$(WasmNestedPublishAppDependsOn)</WasmNestedPublishAppDependsOn>
</PropertyGroup>

<!-- Wire-up static web assets -->
<PropertyGroup>
<GenerateStaticWebAssetsPublishManifestDependsOn Condition="'$(WasmBuildingForNestedPublish)' != 'true'">
$(GenerateStaticWebAssetsPublishManifestDependsOn);
_BlazorCompressPublishFiles;
</GenerateStaticWebAssetsPublishManifestDependsOn>

<AddWasmStaticWebAssetsDependsOn>
$(AddWasmStaticWebAssetsDependsOn);
<ResolveStaticWebAssetsInputsDependsOn>
$(ResolveStaticWebAssetsInputsDependsOn);
_ResolveBlazorWasmOutputs;
</AddWasmStaticWebAssetsDependsOn>
</ResolveStaticWebAssetsInputsDependsOn>

<GenerateBuildWasmBootJsonDependsOn>
$(GenerateBuildWasmBootJsonDependsOn);
GenerateBuildBlazorBootExtensionJson;
</GenerateBuildWasmBootJsonDependsOn>

<GeneratePublishWasmBootJsonDependsOn>
$(GeneratePublishWasmBootJsonDependsOn);
GeneratePublishBlazorBootExtensionJson;
Expand Down Expand Up @@ -125,44 +119,8 @@ Copyright (c) .NET Foundation. All rights reserved.
<Output TaskParameter="Assets" ItemName="_BlazorJSStaticWebAsset" />
</DefineStaticWebAssets>

<PropertyGroup>
<_BlazorBuildGZipCompressDirectory>$(IntermediateOutputPath)build-gz\</_BlazorBuildGZipCompressDirectory>
</PropertyGroup>

<!--
Compress referenced binaries using GZip during build. This skips files such as the project's assemblies
that change from build to build. Runtime assets contribute to the bulk of the download size. Compressing it
has the most benefit while avoiding any ongoing costs to the dev inner loop.
-->

<ItemGroup>
<_BlazorStaticWebAsset Include="@(_BlazorJSStaticWebAsset)" />
<_BlazorStaticWebAsset Include="@(WasmStaticWebAsset)" />
</ItemGroup>

<ComputeBlazorFilesToCompress Assets="@(_BlazorStaticWebAsset)">
<Output TaskParameter="AssetsToCompress" ItemName="_GzipFileToCompressForBuild" />
</ComputeBlazorFilesToCompress>

<GZipCompress
FilesToCompress="@(_GzipFileToCompressForBuild)"
OutputDirectory="$(_BlazorBuildGZipCompressDirectory)">

<Output TaskParameter="CompressedFiles" ItemName="_BlazorBuildGZipCompressedFile" />
<Output TaskParameter="CompressedFiles" ItemName="FileWrites" />
</GZipCompress>

<ItemGroup>
<_BlazorBuildGZipCompressedFile>
<OriginalItemSpec>%(RelatedAsset)</OriginalItemSpec>
</_BlazorBuildGZipCompressedFile>

<_BlazorGzipStaticWebAsset Include="@(_BlazorBuildGZipCompressedFile->'%(FullPath)')" />
</ItemGroup>

<ItemGroup>
<StaticWebAsset Include="@(_BlazorJSStaticWebAsset)" />
<StaticWebAsset Include="@(_BlazorGzipStaticWebAsset)" />
</ItemGroup>
</Target>

Expand All @@ -171,8 +129,6 @@ Copyright (c) .NET Foundation. All rights reserved.
<Message Importance="High" Text="$(MSBuildProjectName) (Blazor output) -&gt; $(TargetDir)wwwroot" Condition="'$(CopyBuildOutputToOutputDirectory)' == 'true' and '$(SkipCopyBuildProduct)'!='true'" />
</Target>

<!-- Publish starts here -->

<!-- Make sure that ResolveAssemblyReferences runs early enough to ensure satellite assemblies are populated in the ResolvedFilesToPublish -->
<Target Name="_BlazorPrepareForPublish"
DependsOnTargets="PrepareResourceNames;ComputeIntermediateSatelliteAssemblies;ResolveAssemblyReferences"
Expand All @@ -184,77 +140,6 @@ Copyright (c) .NET Foundation. All rights reserved.
</ItemGroup>
</Target>

<Target Name="_BlazorCompressPublishFiles" AfterTargets="GeneratePublishWasmBootJson" Condition="'$(BlazorEnableCompression)' != 'false'">
<PropertyGroup>
<_CompressedFileOutputPath>$(IntermediateOutputPath)compress\</_CompressedFileOutputPath>
<_BlazorWebAssemblyBrotliIncremental>true</_BlazorWebAssemblyBrotliIncremental>
</PropertyGroup>

<PropertyGroup Condition="'$(DOTNET_HOST_PATH)' == ''">
<_DotNetHostDirectory>$(NetCoreRoot)</_DotNetHostDirectory>
<_DotNetHostFileName>dotnet</_DotNetHostFileName>
<_DotNetHostFileName Condition="'$(OS)' == 'Windows_NT'">dotnet.exe</_DotNetHostFileName>
</PropertyGroup>

<Message Text="Compressing Blazor WebAssembly publish artifacts. This may take a while..." Importance="High" />

<MakeDir Directories="$(_CompressedFileOutputPath)" Condition="!Exists('$(_CompressedFileOutputPath)')" />

<ItemGroup>
<_GzipFileToCompressForPublish Include="@(StaticWebAsset)"
Condition="'%(AssetKind)' != 'Build' and ('%(StaticWebAsset.AssetTraitName)' == 'WasmResource' or '%(StaticWebAsset.AssetTraitName)' == 'Culture')" >
<RelatedAsset>%(Identity)</RelatedAsset>
<AssetRole>Alternative</AssetRole>
<AssetTraitName>Content-Encoding</AssetTraitName>
<AssetTraitValue>gzip</AssetTraitValue>
</_GzipFileToCompressForPublish>

<_BrotliFileToCompressForPublish Include="@(_GzipFileToCompressForPublish)" Condition="'%(AssetKind)' != 'Build'">
<AssetTraitValue>br</AssetTraitValue>
</_BrotliFileToCompressForPublish>

<!-- We compressed a bunch of assets that were not modified since the build process. We can reuse those and avoid the extra compression we just
need to check that they are still relevant (we have updated existing assets to account for linking) -->
<_AlreadyGzipCompressedAssets
Include="@(StaticWebAsset)"
Condition="'%(AssetKind)' != 'Build' and ('%(StaticWebAsset.AssetTraitName)' == 'Content-Encoding' and '%(StaticWebAsset.AssetTraitValue)' == 'gzip')" />
<_GzipFileToCompressForPublish Remove="@(_AlreadyGzipCompressedAssets->'%(RelatedAsset)')" />
</ItemGroup>

<GZipCompress
FilesToCompress="@(_GzipFileToCompressForPublish)"
OutputDirectory="$(_CompressedFileOutputPath)">

<Output TaskParameter="CompressedFiles" ItemName="_BlazorPublishGZipCompressedFile" />
<Output TaskParameter="CompressedFiles" ItemName="FileWrites" />
</GZipCompress>

<BrotliCompress
OutputDirectory="$(_CompressedFileOutputPath)"
FilesToCompress="@(_BrotliFileToCompressForPublish)"
CompressionLevel="$(_BlazorBrotliCompressionLevel)"
SkipIfOutputIsNewer="$(_BlazorWebAssemblyBrotliIncremental)"
ToolAssembly="$(_BlazorWebAssemblySdkToolAssembly)"
ToolExe="$(_DotNetHostFileName)"
ToolPath="$(_DotNetHostDirectory)">

<Output TaskParameter="CompressedFiles" ItemName="_BlazorPublishBrotliCompressedFile" />
<Output TaskParameter="CompressedFiles" ItemName="FileWrites" />
</BrotliCompress>

<ItemGroup>
<_BlazorPublishGZipCompressedFile>
<OriginalItemSpec>%(RelatedAsset)</OriginalItemSpec>
</_BlazorPublishGZipCompressedFile>
<_BlazorPublishBrotliCompressedFile>
<OriginalItemSpec>%(RelatedAsset)</OriginalItemSpec>
</_BlazorPublishBrotliCompressedFile>

<StaticWebAsset Include="@(_BlazorPublishGZipCompressedFile->'%(FullPath)')" />
<StaticWebAsset Include="@(_BlazorPublishBrotliCompressedFile->'%(FullPath)')" />
</ItemGroup>
</Target>

<Target Name="GenerateBuildBlazorBootExtensionJson">
<PropertyGroup>
<_BlazorBuildBootExtensionJsonPath>$(IntermediateOutputPath)blazor.build.boot-extension.json</_BlazorBuildBootExtensionJsonPath>
Expand Down
76 changes: 0 additions & 76 deletions src/BlazorWasmSdk/Tasks/ComputeBlazorFilesToCompress.cs

This file was deleted.

24 changes: 24 additions & 0 deletions src/Layout/redist/targets/GenerateLayout.targets
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,29 @@
SkipUnchangedFiles="true" />
</Target>

<Target Name="PublishStaticWebAssetsTools"
BeforeTargets="PublishNetSdks">
<MSBuild
Targets="Publish"
Projects="$(RepoRoot)/src/StaticWebAssetsSdk/Tool/Microsoft.NET.Sdk.StaticWebAssets.Tool.csproj"
Properties="Configuration=$(Configuration)" />
</Target>

<Target Name="MoveStaticWebAssetsTools" AfterTargets="PublishStaticWebAssetsTools">
<ItemGroup>
<_StaticWebAssetsToolsOutput Include="$(ArtifactsBinDir)Microsoft.NET.Sdk.StaticWebAssets.Tool\$(Configuration)\$(SdkTargetFramework)\publish\*.*" />
</ItemGroup>

<Error
Text="StaticWebAssets SDK tools outputs were not found in $(ArtifactsBinDir)Microsoft.NET.Sdk.StaticWebAssets.Tool\$(Configuration)\$(SdkTargetFramework)\publish"
Condition="'@(_StaticWebAssetsToolsOutput->Count())' == '0'" />

<Copy
SourceFiles="@(_StaticWebAssetsToolsOutput)"
DestinationFolder="$(ArtifactsBinDir)$(Configuration)\Sdks\Microsoft.NET.Sdk.StaticWebAssets\tools\$(SdkTargetFramework)\"
SkipUnchangedFiles="true" />
</Target>

<Target Name="PublishRazorSdkTools"
BeforeTargets="PublishNetSdks">
<MSBuild
Expand Down Expand Up @@ -451,6 +474,7 @@
PublishMSBuildExtensions;
PublishFSharp;
PublishBlazorWasmTools;
PublishStaticWebAssetsTools;
GenerateCliRuntimeConfigurationFiles;
MakeFscRunnableAndMoveToPublishDir;
RemoveFscFilesAfterPublish;
Expand Down
Loading

0 comments on commit cf2977b

Please sign in to comment.