Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[StaticWebAssets] Optimize compression #47734

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<StaticWebAssetsAdditionalPublishProperties>$(StaticWebAssetsAdditionalPublishProperties);_PublishingBlazorWasmProject=true</StaticWebAssetsAdditionalPublishProperties>
<StaticWebAssetsAdditionalEmbeddedPublishProperties>$(StaticWebAssetsAdditionalEmbeddedPublishProperties);_PublishingBlazorWasmProject=true</StaticWebAssetsAdditionalEmbeddedPublishProperties>
<StaticWebAssetStandaloneHosting Condition="'$(StaticWebAssetStandaloneHosting)' == '' and '$(StaticWebAssetProjectMode)' == 'Root'">true</StaticWebAssetStandaloneHosting>
<StaticWebAssetMakeReferencedAssetOriginalItemSpecAbsolute Condition="'$(StaticWebAssetMakeReferencedAssetOriginalItemSpecAbsolute)' == ''">true</StaticWebAssetMakeReferencedAssetOriginalItemSpecAbsolute>
<StaticWebAssetsGetEmbeddedPublishAssetsTargets>ComputeFilesToPublish;GetCurrentProjectEmbeddedPublishStaticWebAssetItems</StaticWebAssetsGetEmbeddedPublishAssetsTargets>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,12 @@ Copyright (c) .NET Foundation. All rights reserved.
</PropertyGroup>

<PropertyGroup>
<BuildCompressionFormats>$(BuildCompressionFormats);gzip</BuildCompressionFormats>
<PublishCompressionFormats>$(PublishCompressionFormats);gzip;brotli</PublishCompressionFormats>
<EnableDefaultCompressionFormats Condition="'$(EnableDefaultCompressionFormats)' == ''">true</EnableDefaultCompressionFormats>
<BuildCompressionFormats Condition="'$(EnableDefaultCompressionFormats)' == 'true'">$(BuildCompressionFormats);gzip</BuildCompressionFormats>
<PublishCompressionFormats Condition="'$(EnableDefaultCompressionFormats)' == 'true'">$(PublishCompressionFormats);gzip;brotli</PublishCompressionFormats>
<DisableBuildCompression Condition="'$(DisableBuildCompression)' == ''">false</DisableBuildCompression>
<CompressionIncludePatterns>$(CompressionIncludePatterns)</CompressionIncludePatterns>
<CompressionExcludePatterns>$(CompressionExcludePatterns)</CompressionExcludePatterns>
<!-- Support passing in a custom compression level to brotli and respect the old internal flag for blazor -->
<BrotliCompressionLevel Condition="'$(_BlazorBrotliCompressionLevel)' != ''">$(_BlazorBrotliCompressionLevel)</BrotliCompressionLevel>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -195,7 +196,7 @@ Copyright (c) .NET Foundation. All rights reserved.
GeneratePublishCompressedStaticWebAssets
ResolvePublishCompressedStaticWebAssetsConfiguration
-->
<ResolvePublishRelatedStaticWebAssetsDependsOn Condition="'$(DisableBuildCompression)' != 'true'">
<ResolvePublishRelatedStaticWebAssetsDependsOn>
ResolvePublishCompressedStaticWebAssets;
$(ResolvePublishRelatedStaticWebAssetsDependsOn)
</ResolvePublishRelatedStaticWebAssetsDependsOn>
Expand Down Expand Up @@ -223,12 +224,10 @@ Copyright (c) .NET Foundation. All rights reserved.
CandidateAssets="@(_CompressedStaticWebAssets)"
>
<Output TaskParameter="Assets" ItemName="_CompressionBuildStaticWebAsset" />
<Output TaskParameter="AssetDetails" ItemName="_ResolveBuildCompressedStaticWebAssetsDetails" />
</DefineStaticWebAssets>

<DefineStaticWebAssetEndpoints
CandidateAssets="@(_CompressionBuildStaticWebAsset);@(_PrecompressedStaticWebAssets)"
AssetFileDetails="@(_ResolveBuildCompressedStaticWebAssetsDetails)"
ContentTypeMappings="@(StaticWebAssetContentTypeMapping)"
>
<Output TaskParameter="Endpoints" ItemName="_CompressionBuildStaticWebAssetEndpoint" />
Expand All @@ -242,7 +241,6 @@ Copyright (c) .NET Foundation. All rights reserved.

<ApplyCompressionNegotiation
CandidateEndpoints="@(StaticWebAssetEndpoint)"
AssetFileDetails="@(_ResolveBuildCompressedStaticWebAssetsDetails)"
CandidateAssets="@(_CompressionCurrentProjectBuildAssets)"
>
<Output TaskParameter="UpdatedEndpoints" ItemName="_UpdatedCompressionBuildEndpoints" />
Expand All @@ -267,7 +265,7 @@ Copyright (c) .NET Foundation. All rights reserved.

<BrotliCompress Condition="'@(_BrotliCompressedStaticWebAssets)' != ''"
FilesToCompress="@(_BrotliCompressedStaticWebAssets)"
CompressionLevel="$(_BlazorBrotliCompressionLevel)"
CompressionLevel="$(BrotliCompressionLevel)"
ToolAssembly="$(_StaticWebAssetsSdkToolAssembly)"
ToolExe="$(_DotNetHostFileName)"
ToolPath="$(_DotNetHostDirectory)" />
Expand Down Expand Up @@ -301,8 +299,23 @@ Copyright (c) .NET Foundation. All rights reserved.
<StaticWebAsset Include="@(_PrecompressedStaticWebAssets)" />
</ItemGroup>

<PropertyGroup>
<StaticWebAssetBuildCompressAllAssets Condition="'$(StaticWebAssetBuildCompressAllAssets)' == '' and '$(StaticWebAssetProjectMode)' == 'Root'">true</StaticWebAssetBuildCompressAllAssets>
<StaticWebAssetBuildCompressAllAssets Condition="'$(StaticWebAssetBuildCompressAllAssets)' == ''">false</StaticWebAssetBuildCompressAllAssets>
</PropertyGroup>

<ItemGroup Condition="'$(StaticWebAssetBuildCompressAllAssets)' == 'true'">
<_CandidateAssetsForBuild Include="@(StaticWebAsset)" />
</ItemGroup>

<ItemGroup Condition="'$(StaticWebAssetBuildCompressAllAssets)' != 'true'">
<_CandidateAssetsForBuild
Include="@(StaticWebAsset)"
Condition="'%(SourceType)' == 'Discovered' or '%(SourceType)' == 'Computed'" />
</ItemGroup>

<ResolveCompressedAssets
CandidateAssets="@(StaticWebAsset)"
CandidateAssets="@(_CandidateAssetsForBuild)"
Formats="$(BuildCompressionFormats)"
IncludePatterns="$(CompressionIncludePatterns)"
ExcludePatterns="$(CompressionExcludePatterns)"
Expand Down Expand Up @@ -383,12 +396,23 @@ Copyright (c) .NET Foundation. All rights reserved.

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

<ItemGroup>
<PropertyGroup>
<StaticWebAssetPublishCompressAllAssets Condition="'$(StaticWebAssetPublishCompressAllAssets)' == '' and '$(StaticWebAssetProjectMode)' == 'Root'">true</StaticWebAssetPublishCompressAllAssets>
<StaticWebAssetPublishCompressAllAssets Condition="'$(StaticWebAssetPublishCompressAllAssets)' == ''">false</StaticWebAssetPublishCompressAllAssets>
</PropertyGroup>

<ItemGroup Condition="'$(StaticWebAssetPublishCompressAllAssets)' == 'true'">
<_CandidateAssetsForPublish
Include="@(StaticWebAsset)"
Condition="'%(AssetKind)' != 'Build'" />
</ItemGroup>

<ItemGroup Condition="'$(StaticWebAssetPublishCompressAllAssets)' != 'true'">
<_CandidateAssetsForPublish
Include="@(StaticWebAsset)"
Condition="'%(AssetKind)' != 'Build' and ('%(SourceType)' == 'Discovered' or '%(SourceType)' == 'Computed')" />
</ItemGroup>

<ResolveCompressedAssets
CandidateAssets="@(_CandidateAssetsForPublish)"
Formats="$(PublishCompressionFormats)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ Copyright (c) .NET Foundation. All rights reserved.
Patterns="@(_CachedBuildStaticWebAssetDiscoveryPatterns)"
ProjectMode="$(StaticWebAssetProjectMode)"
AssetKind="Build"
MakeReferencedAssetOriginalItemSpecAbsolute="$(StaticWebAssetMakeReferencedAssetOriginalItemSpecAbsolute)"
Source="$(PackageId)"
>
<Output TaskParameter="StaticWebAssets" ItemName="_CachedBuildReferencedStaticWebAsset" />
Expand Down Expand Up @@ -227,6 +228,7 @@ Copyright (c) .NET Foundation. All rights reserved.
ProjectMode="$(StaticWebAssetProjectMode)"
AssetKind="Publish"
Source="$(PackageId)"
MakeReferencedAssetOriginalItemSpecAbsolute="$(StaticWebAssetMakeReferencedAssetOriginalItemSpecAbsolute)"
>
<Output TaskParameter="StaticWebAssets" ItemName="_CachedPublishReferencedStaticWebAsset" />
</ComputeReferenceStaticWebAssetItems>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,24 +671,27 @@ Copyright (c) .NET Foundation. All rights reserved.
BeforeTargets="AssignTargetPaths"
DependsOnTargets="ResolveStaticWebAssetsConfiguration;UpdateExistingPackageStaticWebAssets">

<PropertyGroup>
<_ResolveProjectStaticWebAssetsCachePath>$(_StaticWebAssetsManifestBase)rpswa.dswa.cache.json</_ResolveProjectStaticWebAssetsCachePath>
</PropertyGroup>

<DefineStaticWebAssets
CandidateAssets="@(Content)"
CandidateAssets="@(Content->Distinct())"
FingerprintCandidates="$(StaticWebAssetsFingerprintContent)"
FingerprintPatterns="@(StaticWebAssetFingerprintPattern)"
RelativePathPattern="wwwroot/**"
SourceType="Discovered"
SourceId="$(PackageId)"
ContentRoot="$(MSBuildProjectDirectory)\wwwroot\"
BasePath="$(StaticWebAssetBasePath)"
AssetMergeSource="$(StaticWebAssetMergeTarget)">
AssetMergeSource="$(StaticWebAssetMergeTarget)"
CacheManifestPath="$(_ResolveProjectStaticWebAssetsCachePath)">
<Output TaskParameter="Assets" ItemName="StaticWebAsset" />
<Output TaskParameter="Assets" ItemName="_CurrentProjectStaticWebAsset" />
<Output TaskParameter="AssetDetails" ItemName="_ResolveProjectStaticWebAssetsDetails" />
</DefineStaticWebAssets>

<DefineStaticWebAssetEndpoints
CandidateAssets="@(_CurrentProjectStaticWebAsset)"
AssetFileDetails="@(_ResolveProjectStaticWebAssetsDetails)"
ContentTypeMappings="@(StaticWebAssetContentTypeMapping)"
>
<Output TaskParameter="Endpoints" ItemName="StaticWebAssetEndpoint" />
Expand Down Expand Up @@ -737,7 +740,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<Import Project="Microsoft.NET.Sdk.StaticWebAssets.Compression.targets" Condition="'$(CompressionEnabled)' == 'true'" />

<Import Project="Microsoft.NET.Sdk.StaticWebAssets.ServiceWorker.targets" Condition="'$(ServiceWorkerAssetsManifest)' != ''" />

<Import Project="Microsoft.NET.Sdk.StaticWebAssets.HtmlImportMap.targets" Condition="'$(WriteImportMapToHtml)' == 'true'" />

</Project>
35 changes: 2 additions & 33 deletions src/StaticWebAssetsSdk/Tasks/ApplyCompressionNegotiation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#nullable disable

using System.Globalization;
using Microsoft.AspNetCore.StaticWebAssets.Tasks.Utils;
using Microsoft.Build.Framework;

namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;
Expand All @@ -17,27 +16,11 @@ public class ApplyCompressionNegotiation : Task
[Required]
public ITaskItem[] CandidateAssets { get; set; }

public ITaskItem[] AssetFileDetails { get; set; }

[Output]
public ITaskItem[] UpdatedEndpoints { get; set; }

public Func<string, long> TestResolveFileLength;

private Dictionary<string, ITaskItem> _assetFileDetails;

public override bool Execute()
{
if (AssetFileDetails != null)
{
_assetFileDetails = new(AssetFileDetails.Length, OSPath.PathComparer);
for (var i = 0; i < AssetFileDetails.Length; i++)
{
var item = AssetFileDetails[i];
_assetFileDetails[item.ItemSpec] = item;
}
}

var assetsById = CandidateAssets.Select(StaticWebAsset.FromTaskItem).ToDictionary(a => a.Identity);

var endpointsByAsset = CandidateEndpoints.Select(StaticWebAssetEndpoint.FromTaskItem)
Expand Down Expand Up @@ -211,22 +194,8 @@ public override bool Execute()
return true;
}

private string ResolveQuality(StaticWebAsset compressedAsset)
{
long length;
if (_assetFileDetails != null && _assetFileDetails.TryGetValue(compressedAsset.Identity, out var assetFileDetail))
{
length = long.Parse(assetFileDetail.GetMetadata("FileLength"), CultureInfo.InvariantCulture);
}
else
{
length = TestResolveFileLength != null
? TestResolveFileLength(compressedAsset.Identity)
: new FileInfo(compressedAsset.Identity).Length;
}

return Math.Round(1.0 / (length + 1), 12).ToString("F12", CultureInfo.InvariantCulture);
}
private static string ResolveQuality(StaticWebAsset compressedAsset) =>
Math.Round(1.0 / (compressedAsset.FileLength + 1), 12).ToString("F12", CultureInfo.InvariantCulture);

private static bool IsCompatible(StaticWebAssetEndpoint compressedEndpoint, StaticWebAssetEndpoint relatedEndpointCandidate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class ComputeReferenceStaticWebAssetItems : Task

public bool UpdateSourceType { get; set; } = true;

public bool MakeReferencedAssetOriginalItemSpecAbsolute { get; set; }

[Output]
public ITaskItem[] StaticWebAssets { get; set; }

Expand Down Expand Up @@ -63,6 +65,7 @@ public override bool Execute()
if (ShouldIncludeAssetAsReference(selected, out var reason))
{
selected.SourceType = UpdateSourceType ? StaticWebAsset.SourceTypes.Project : selected.SourceType;
selected.OriginalItemSpec = MakeReferencedAssetOriginalItemSpecAbsolute ? Path.GetFullPath(selected.OriginalItemSpec) : selected.OriginalItemSpec;
resultAssets.Add(selected);
}
Log.LogMessage(MessageImportance.Low, reason);
Expand Down
Loading
Loading