Skip to content

Commit 60d659c

Browse files
authored
[Static Web Assets] Reworks the protocol for getting referenced assets and brings back backwards compatibility (#19482)
* Brings back compatibility with earlier target framework versions. * Avoids reading dependent projects static web asset manifests multiple times. * Allow dependent projects to define how the referencing project should call into them to get the list of static web asset items. * Fixes issue with the manifest being generated when there are no static web assets. * Fixes issues with templatized baselines. * Fixes an issue where static web assets will fail when the path for the manifest is not available.
1 parent e56c942 commit 60d659c

File tree

66 files changed

+10314
-1481
lines changed

Some content is hidden

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

66 files changed

+10314
-1481
lines changed

src/BlazorWasmSdk/Targets/Microsoft.NET.Sdk.BlazorWebAssembly.5_0.targets

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ Copyright (c) .NET Foundation. All rights reserved.
267267
_BlazorWasmReplaceBundle;
268268
_BlazorWasmPrepareForRun;
269269
</GetCurrentProjectStaticWebAssetsDependsOn>
270+
271+
<GetCurrentProjectStaticWebAssetsV2DependsOn>
272+
$(GetCurrentProjectStaticWebAssetsV2DependsOn);
273+
_UpdateBlazorWasmStaticWebAssetsForV2;
274+
</GetCurrentProjectStaticWebAssetsV2DependsOn>
270275
</PropertyGroup>
271276

272277
<Target Name="_BlazorWasmReplaceBundle">
@@ -276,6 +281,20 @@ Copyright (c) .NET Foundation. All rights reserved.
276281
</ItemGroup>
277282
</Target>
278283

284+
<Target Name="_UpdateBlazorWasmStaticWebAssetsForV2" DependsOnTargets="GetCurrentProjectStaticWebAssets">
285+
<ItemGroup>
286+
<_CurrentWasmProjectStaticWebAssets Include="@(_ThisProjectStaticWebAssets)" Condition="'%(SourceId)' == '$(PackageId)'" />
287+
<_CurrentWasmProjectStaticWebAssets>
288+
<!-- We set the asset kind explicitly to build becasue blazor before 6.0 did its own publishing and was
289+
not fully integrated with static web assets, so we let it be even when called from 6.0 targets
290+
-->
291+
<AssetKind>Build</AssetKind>
292+
</_CurrentWasmProjectStaticWebAssets>
293+
<_ThisProjectStaticWebAssets Remove="@(_CurrentWasmProjectStaticWebAssets)" />
294+
<_ThisProjectStaticWebAssets Include="@(_CurrentWasmProjectStaticWebAssets)" />
295+
</ItemGroup>
296+
</Target>
297+
279298
<Target Name="_BlazorWasmPrepareForRun" DependsOnTargets="_ProcessBlazorWasmOutputs" BeforeTargets="_RazorPrepareForRun" AfterTargets="GetCurrentProjectStaticWebAssets">
280299
<PropertyGroup>
281300
<_BlazorBuildBootJsonPath>$(IntermediateOutputPath)blazor.boot.json</_BlazorBuildBootJsonPath>

src/BlazorWasmSdk/Targets/Microsoft.NET.Sdk.BlazorWebAssembly.6_0.targets

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ Copyright (c) .NET Foundation. All rights reserved.
394394
<ItemGroup>
395395
<_BlazorPublishPrefilteredAssets
396396
Include="@(StaticWebAsset)"
397-
Condition="'%(AssetTraitName)' == 'BlazorWebAssemblyResource' or '%(AssetTraitName)' == 'Culture' or '%(AssetRole)' == 'Alternative'" />
397+
Condition="'%(StaticWebAsset.AssetTraitName)' == 'BlazorWebAssemblyResource' or '%(StaticWebAsset.AssetTraitName)' == 'Culture' or '%(AssetRole)' == 'Alternative'" />
398398
</ItemGroup>
399399

400400
<ComputeBlazorPublishAssets
@@ -432,11 +432,11 @@ Copyright (c) .NET Foundation. All rights reserved.
432432
<ItemGroup>
433433
<_BlazorPublishAsset
434434
Include="@(StaticWebAsset)"
435-
Condition="'%(AssetKind)' != 'Build' and ('%(AssetTraitName)' == 'BlazorWebAssemblyResource' or '%(AssetTraitName)' == 'Culture') and '%(RelativePath)' != '_framework/blazor.webassembly.js'" />
435+
Condition="'%(AssetKind)' != 'Build' and ('%(StaticWebAsset.AssetTraitName)' == 'BlazorWebAssemblyResource' or '%(StaticWebAsset.AssetTraitName)' == 'Culture') and '%(RelativePath)' != '_framework/blazor.webassembly.js'" />
436436

437437
<_BlazorPublishConfigFile
438438
Include="@(StaticWebAsset)"
439-
Condition="'%(AssetTraitName)' == 'BlazorWebAssemblyResource' and '%(AssetTraitValue)' == 'settings'"/>
439+
Condition="'%(StaticWebAsset.AssetTraitName)' == 'BlazorWebAssemblyResource' and '%(StaticWebAsset.AssetTraitValue)' == 'settings'"/>
440440

441441
</ItemGroup>
442442

@@ -515,7 +515,7 @@ Copyright (c) .NET Foundation. All rights reserved.
515515

516516
<ItemGroup>
517517
<_GzipFileToCompressForPublish Include="@(StaticWebAsset)"
518-
Condition="'%(AssetKind)' != 'Build' and ('%(AssetTraitName)' == 'BlazorWebAssemblyResource' or '%(AssetTraitName)' == 'Culture')" >
518+
Condition="'%(AssetKind)' != 'Build' and ('%(StaticWebAsset.AssetTraitName)' == 'BlazorWebAssemblyResource' or '%(StaticWebAsset.AssetTraitName)' == 'Culture')" >
519519
<RelatedAsset>%(Identity)</RelatedAsset>
520520
<AssetRole>Alternative</AssetRole>
521521
<AssetTraitName>Content-Encoding</AssetTraitName>
@@ -530,7 +530,7 @@ Copyright (c) .NET Foundation. All rights reserved.
530530
need to check that they are still relevant (we have updated existing assets to account for linking) -->
531531
<_AlreadyGzipCompressedAssets
532532
Include="@(StaticWebAsset)"
533-
Condition="'%(AssetKind)' != 'Build' and ('%(AssetTraitName)' == 'Content-Encoding' and '%(AssetTraitValue)' == 'gzip')" />
533+
Condition="'%(AssetKind)' != 'Build' and ('%(StaticWebAsset.AssetTraitName)' == 'Content-Encoding' and '%(StaticWebAsset.AssetTraitValue)' == 'gzip')" />
534534
<_GzipFileToCompressForPublish Remove="@(_AlreadyGzipCompressedAssets->'%(RelatedAsset)')" />
535535
</ItemGroup>
536536

src/BlazorWasmSdk/Targets/Microsoft.NET.Sdk.BlazorWebAssembly.Current.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Copyright (c) .NET Foundation. All rights reserved.
3535
<!-- Static web assets defaults -->
3636
<StaticWebAssetBasePath Condition="'$(StaticWebAssetBasePath)' == ''">/</StaticWebAssetBasePath>
3737
<StaticWebAssetProjectMode Condition="'$(StaticWebAssetProjectMode)' == ''">Root</StaticWebAssetProjectMode>
38-
<StaticWebAssetsPublishTarget>ComputeFilesToPublish</StaticWebAssetsPublishTarget>
38+
<StaticWebAssetsGetPublishAssetsTargets>ComputeFilesToPublish;GetCurrentProjectPublishStaticWebAssetItems</StaticWebAssetsGetPublishAssetsTargets>
3939
<StaticWebAssetsAdditionalPublishProperties>$(StaticWebAssetsAdditionalPublishProperties);BuildProjectReferences=false;ResolveAssemblyReferencesFindRelatedSatellites=true;_PublishingBlazorWasmProject=true</StaticWebAssetsAdditionalPublishProperties>
4040
<StaticWebAssetsAdditionalPublishPropertiesToRemove>$(StaticWebAssetsAdditionalPublishPropertiesToRemove);NoBuild;RuntimeIdentifier</StaticWebAssetsAdditionalPublishPropertiesToRemove>
4141

src/BlazorWasmSdk/Targets/Microsoft.NET.Sdk.BlazorWebAssembly.ServiceWorkerAssetsManifest.targets

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Copyright (c) .NET Foundation. All rights reserved.
1515

1616
<PropertyGroup>
1717
<ResolveStaticWebAssetsInputsDependsOn>$(ResolveStaticWebAssetsInputsDependsOn);_AddServiceWorkerAssets</ResolveStaticWebAssetsInputsDependsOn>
18-
<CopyStaticWebAssetsToPublishDirectoryDependsOn>$(CopyStaticWebAssetsToPublishDirectoryDependsOn);_WritePublishServiceWorkerAssetsManifest</CopyStaticWebAssetsToPublishDirectoryDependsOn>
19-
<CopyStaticWebAssetsToOutputFolderDependsOn>$(CopyStaticWebAssetsToOutputFolderDependsOn);_WriteBuildServiceWorkerAssetsManifest</CopyStaticWebAssetsToOutputFolderDependsOn>
18+
<GenerateComputedPublishStaticWebAssetsDependsOn>$(GenerateComputedPublishStaticWebAssetsDependsOn);_WritePublishServiceWorkerAssetsManifest</GenerateComputedPublishStaticWebAssetsDependsOn>
19+
<GenerateComputedBuildStaticWebAssetsDependsOn>$(GenerateComputedBuildStaticWebAssetsDependsOn);_WriteBuildServiceWorkerAssetsManifest</GenerateComputedBuildStaticWebAssetsDependsOn>
2020
</PropertyGroup>
2121

2222
<Target Name="_AddServiceWorkerAssets" DependsOnTargets="_ComputeServiceWorkerAssets">
@@ -108,13 +108,22 @@ Copyright (c) .NET Foundation. All rights reserved.
108108

109109
</Target>
110110

111-
<Target Name="_WriteBuildServiceWorkerAssetsManifest" DependsOnTargets="_ComputeServiceWorkerAssets">
111+
<Target Name="_WriteBuildServiceWorkerAssetsManifest" DependsOnTargets="ResolveStaticWebAssetsInputs">
112112

113113
<ItemGroup>
114-
<_BuildAssetsForManifest Include="@(StaticWebAsset)" Condition="'%(AssetTraitName)' != 'BlazorServiceWorker' and '%(AssetRole)' != 'Alternative' and '%(AssetKind)' != 'Publish' and Exists('%(Identity)')" />
115-
<_BuildAssetsForManifest Include="@(StaticWebAsset->'%(OriginalItemSpec)')" Condition="'%(AssetTraitName)' != 'BlazorServiceWorker' and '%(AssetRole)' != 'Alternative' and '%(AssetKind)' != 'Publish' and !Exists('%(Identity)')" />
114+
<_BuildAssetsForManifestCandidate Include="@(StaticWebAsset)" Condition="'%(StaticWebAsset.AssetTraitName)' != 'BlazorServiceWorker' and '%(AssetRole)' != 'Alternative' and Exists('%(Identity)')" />
115+
<_BuildAssetsForManifestCandidate Include="@(StaticWebAsset->'%(OriginalItemSpec)')" Condition="'%(StaticWebAsset.AssetTraitName)' != 'BlazorServiceWorker' and '%(AssetRole)' != 'Alternative' and !Exists('%(Identity)')" />
116116
</ItemGroup>
117117

118+
<ComputeStaticWebAssetsForCurrentProject
119+
Assets="@(_BuildAssetsForManifestCandidate)"
120+
ProjectMode="$(StaticWebAssetProjectMode)"
121+
AssetKind="Build"
122+
Source="$(PackageId)"
123+
>
124+
<Output TaskParameter="StaticWebAssets" ItemName="_BuildAssetsForManifest" />
125+
</ComputeStaticWebAssetsForCurrentProject>
126+
118127
<ComputeStaticWebAssetsTargetPaths Assets="@(_BuildAssetsForManifest)" PathPrefix="$(StaticWebAssetBasePath)" UseAlternatePathDirectorySeparator="true">
119128
<Output TaskParameter="AssetsWithTargetPath" ItemName="_BuildAssetsForManifestWithTargetPath" />
120129
</ComputeStaticWebAssetsTargetPaths>
@@ -123,7 +132,7 @@ Copyright (c) .NET Foundation. All rights reserved.
123132
<_BuildServiceWorkItem Include="@(_BuildAssetsForManifestWithTargetPath)">
124133
<AssetUrl>%(_BuildAssetsForManifestWithTargetPath.TargetPath)</AssetUrl>
125134
</_BuildServiceWorkItem>
126-
<_BuildServiceWorkerFile Include="@(StaticWebAsset)" Condition="'%(AssetKind)' == 'Build' And '%(AssetTraitName)' == 'BlazorServiceWorker' And '%(AssetTraitValue)' == 'ServiceWorkerJs'" />
135+
<_BuildServiceWorkerFile Include="@(StaticWebAsset)" Condition="'%(AssetKind)' == 'Build' And '%(StaticWebAsset.AssetTraitName)' == 'BlazorServiceWorker' And '%(StaticWebAsset.AssetTraitValue)' == 'ServiceWorkerJs'" />
127136
</ItemGroup>
128137

129138
<GenerateServiceWorkerAssetsManifest
@@ -157,21 +166,30 @@ Copyright (c) .NET Foundation. All rights reserved.
157166

158167
</Target>
159168

160-
<Target Name="_WritePublishServiceWorkerAssetsManifest" DependsOnTargets="_ComputeServiceWorkerAssets;LoadStaticWebAssetsPublishManifest" AfterTargets="GenerateStaticWebAssetsPublishManifest">
169+
<Target Name="_WritePublishServiceWorkerAssetsManifest" DependsOnTargets="_ComputeServiceWorkerAssets;LoadStaticWebAssetsPublishManifest">
161170

162171
<ItemGroup>
163-
<_PublishAssetsForManifest Include="@(StaticWebAsset)" Condition="'%(AssetTraitName)' != 'BlazorServiceWorker' and '%(AssetRole)' != 'Alternative'" />
172+
<_PublishAssetsForManifestCandidate Include="@(StaticWebAsset)" Condition="'%(StaticWebAsset.AssetTraitName)' != 'BlazorServiceWorker' and '%(AssetRole)' != 'Alternative'" />
164173
</ItemGroup>
165174

175+
<ComputeStaticWebAssetsForCurrentProject
176+
Assets="@(_PublishAssetsForManifestCandidate)"
177+
ProjectMode="$(StaticWebAssetProjectMode)"
178+
AssetKind="Publish"
179+
Source="$(PackageId)"
180+
>
181+
<Output TaskParameter="StaticWebAssets" ItemName="_PublishAssetsForManifest" />
182+
</ComputeStaticWebAssetsForCurrentProject>
183+
166184
<ComputeStaticWebAssetsTargetPaths Assets="@(_PublishAssetsForManifest)" PathPrefix="$(StaticWebAssetBasePath)" UseAlternatePathDirectorySeparator="true">
167-
<Output TaskParameter="AssetsWithTargetPath" ItemName="_AssetsForManifestWithTargetPath" />
185+
<Output TaskParameter="AssetsWithTargetPath" ItemName="_PublishAssetsForManifestWithTargetPath" />
168186
</ComputeStaticWebAssetsTargetPaths>
169187

170188
<ItemGroup>
171-
<_ServiceWorkItem Include="@(_AssetsForManifestWithTargetPath)">
172-
<AssetUrl>%(_AssetsForManifestWithTargetPath.TargetPath)</AssetUrl>
189+
<_ServiceWorkItem Include="@(_PublishAssetsForManifestWithTargetPath)">
190+
<AssetUrl>%(_PublishAssetsForManifestWithTargetPath.TargetPath)</AssetUrl>
173191
</_ServiceWorkItem>
174-
<_PublishServiceWorkerFile Include="@(StaticWebAsset)" Condition="'%(AssetKind)' == 'Publish' And '%(AssetTraitName)' == 'BlazorServiceWorker' And '%(AssetTraitValue)' == 'ServiceWorkerJs'" />
192+
<_PublishServiceWorkerFile Include="@(StaticWebAsset)" Condition="'%(AssetKind)' == 'Publish' And '%(StaticWebAsset.AssetTraitName)' == 'BlazorServiceWorker' And '%(StaticWebAsset.AssetTraitValue)' == 'ServiceWorkerJs'" />
175193
</ItemGroup>
176194

177195
<GenerateServiceWorkerAssetsManifest

src/RazorSdk/Razor.slnf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"projects": [
55
"src\\BlazorWasmSdk\\Tasks\\Microsoft.NET.Sdk.BlazorWebAssembly.Tasks.csproj",
66
"src\\BlazorWasmSdk\\Tool\\Microsoft.NET.Sdk.BlazorWebAssembly.Tool.csproj",
7+
"src\\Cli\\Microsoft.DotNet.Cli.Utils\\Microsoft.DotNet.Cli.Utils.csproj",
78
"src\\RazorSdk\\SourceGenerators\\Microsoft.NET.Sdk.Razor.SourceGenerators.csproj",
89
"src\\RazorSdk\\Tasks\\Microsoft.NET.Sdk.Razor.Tasks.csproj",
910
"src\\RazorSdk\\Tool\\Microsoft.NET.Sdk.Razor.Tool.csproj",

src/RazorSdk/Targets/Microsoft.NET.Sdk.Razor.ScopedCss.5_0.targets

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ Integration with static web assets:
9898
_AddGeneratedScopedCssFilesForReference;
9999
</GenerateStaticWebAssetsPackTargetsDependsOn>
100100

101+
<GetCurrentProjectStaticWebAssetsV2DependsOn>
102+
$(GetCurrentProjectStaticWebAssetsV2DependsOn);
103+
_UpdateScopedCssStaticWebAssetsForV2;
104+
</GetCurrentProjectStaticWebAssetsV2DependsOn>
105+
101106
<!-- We are going to use .rz.scp.css as the extension to mark scoped css files that come from packages or that have been pre-procesed by
102107
referenced class libraries. This way, we can use that information to adjust the build pipeline without having to rely on external
103108
sources like an additional itemgroup or metadata.
@@ -390,4 +395,43 @@ Integration with static web assets:
390395
</ItemGroup>
391396
</Target>
392397

398+
<!-- Forward compat with new static web assets -->
399+
400+
<Target Name="_UpdateScopedCssStaticWebAssetsForV2">
401+
<PropertyGroup>
402+
<_AppBundleStaticWebAssetIdentity>@(_AppBundleStaticWebAsset)</_AppBundleStaticWebAssetIdentity>
403+
<_ProjectBundleStaticWebAssetIdentity>@(_ProjectBundleStaticWebAsset)</_ProjectBundleStaticWebAssetIdentity>
404+
</PropertyGroup>
405+
406+
<ItemGroup>
407+
<!-- <<App>>.bundle.scp.css -->
408+
<_CurrentProjectBundle Include="@(_ThisProjectStaticWebAssets)" Condition="'%(SourceId)' == '$(PackageId)' and '%(_ThisProjectStaticWebAssets.Identity)' == '$(_ProjectBundleStaticWebAssetIdentity)'">
409+
<AssetMode>Reference</AssetMode>
410+
<AssetRole>Primary</AssetRole>
411+
<RelatedAsset></RelatedAsset>
412+
<AssetTraitName>ScopedCss</AssetTraitName>
413+
<AssetTraitValue>ProjectBundle</AssetTraitValue>
414+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
415+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
416+
</_CurrentProjectBundle>
417+
418+
<_ThisProjectStaticWebAssets Remove="@(_CurrentProjectBundle)" />
419+
<_ThisProjectStaticWebAssets Include="@(_CurrentProjectBundle)" />
420+
421+
<!-- <<App>>.styles.css -->
422+
<_CurrentProjectAppBundle Include="@(_ThisProjectStaticWebAssets)" Condition="'%(SourceId)' == '$(PackageId)' and '%(Identity)' == '$(_AppBundleStaticWebAssetIdentity)'">
423+
<AssetMode>CurrentProject</AssetMode>
424+
<AssetRole>Primary</AssetRole>
425+
<RelatedAsset></RelatedAsset>
426+
<AssetTraitName>ScopedCss</AssetTraitName>
427+
<AssetTraitValue>ApplicationBundle</AssetTraitValue>
428+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
429+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
430+
</_CurrentProjectAppBundle>
431+
432+
<_ThisProjectStaticWebAssets Remove="@(_CurrentProjectAppBundle)" />
433+
<_ThisProjectStaticWebAssets Include="@(_CurrentProjectAppBundle)" />
434+
</ItemGroup>
435+
</Target>
436+
393437
</Project>

src/RazorSdk/Targets/Microsoft.NET.Sdk.Razor.ScopedCss.targets

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ Integration with static web assets:
5858
_AddScopedCssBundles;
5959
</ResolveStaticWebAssetsInputsDependsOn>
6060

61-
<CopyStaticWebAssetsToOutputFolderDependsOn>
62-
$(CopyStaticWebAssetsToOutputFolderDependsOn);
61+
<GenerateComputedBuildStaticWebAssetsDependsOn>
62+
$(GenerateComputedBuildStaticWebAssetsDependsOn);
6363
_ResolveBundlingConfiguration;
6464
_GenerateScopedCssFiles;
6565
BundleScopedCssFiles;
66-
</CopyStaticWebAssetsToOutputFolderDependsOn>
66+
</GenerateComputedBuildStaticWebAssetsDependsOn>
6767

6868
</PropertyGroup>
6969

@@ -75,10 +75,10 @@ Integration with static web assets:
7575
_AddGeneratedScopedCssFiles;
7676
</ResolveStaticWebAssetsInputsDependsOn>
7777

78-
<CopyStaticWebAssetsToOutputFolderDependsOn>
78+
<GenerateComputedBuildStaticWebAssetsDependsOn>
7979
_GenerateScopedCssFiles;
80-
$(CopyStaticWebAssetsToOutputFolderDependsOn);
81-
</CopyStaticWebAssetsToOutputFolderDependsOn>
80+
$(GenerateComputedBuildStaticWebAssetsDependsOn);
81+
</GenerateComputedBuildStaticWebAssetsDependsOn>
8282

8383
</PropertyGroup>
8484

@@ -90,8 +90,6 @@ Integration with static web assets:
9090
<_ScopedCssExtension>.rz.scp.css</_ScopedCssExtension>
9191
</PropertyGroup>
9292

93-
<Target Name="_PrepareForScopedCss" DependsOnTargets="$(_PrepareForScopedCssDependsOn)" />
94-
9593
<Target Name="ResolveScopedCssInputs">
9694
<!--
9795
Gathers input source files for Razor component generation. This is a separate target so that we can avoid

0 commit comments

Comments
 (0)