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

[Static Web Assets] Reworks the protocol for getting referenced assets and brings back backwards compatibility #19482

Merged
merged 21 commits into from
Aug 7, 2021
Merged
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 @@ -267,6 +267,11 @@ Copyright (c) .NET Foundation. All rights reserved.
_BlazorWasmReplaceBundle;
_BlazorWasmPrepareForRun;
</GetCurrentProjectStaticWebAssetsDependsOn>

<GetCurrentProjectStaticWebAssetsV2DependsOn>
$(GetCurrentProjectStaticWebAssetsV2DependsOn);
_UpdateBlazorWasmStaticWebAssetsForV2;
</GetCurrentProjectStaticWebAssetsV2DependsOn>
</PropertyGroup>

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

<Target Name="_UpdateBlazorWasmStaticWebAssetsForV2" DependsOnTargets="GetCurrentProjectStaticWebAssets">
<ItemGroup>
<_CurrentWasmProjectStaticWebAssets Include="@(_ThisProjectStaticWebAssets)" Condition="'%(SourceId)' == '$(PackageId)'" />
<_CurrentWasmProjectStaticWebAssets>
<!-- We set the asset kind explicitly to build becasue blazor before 6.0 did its own publishing and was
not fully integrated with static web assets, so we let it be even when called from 6.0 targets
-->
<AssetKind>Build</AssetKind>
</_CurrentWasmProjectStaticWebAssets>
<_ThisProjectStaticWebAssets Remove="@(_CurrentWasmProjectStaticWebAssets)" />
<_ThisProjectStaticWebAssets Include="@(_CurrentWasmProjectStaticWebAssets)" />
</ItemGroup>
</Target>

<Target Name="_BlazorWasmPrepareForRun" DependsOnTargets="_ProcessBlazorWasmOutputs" BeforeTargets="_RazorPrepareForRun" AfterTargets="GetCurrentProjectStaticWebAssets">
<PropertyGroup>
<_BlazorBuildBootJsonPath>$(IntermediateOutputPath)blazor.boot.json</_BlazorBuildBootJsonPath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<ItemGroup>
<_BlazorPublishPrefilteredAssets
Include="@(StaticWebAsset)"
Condition="'%(AssetTraitName)' == 'BlazorWebAssemblyResource' or '%(AssetTraitName)' == 'Culture' or '%(AssetRole)' == 'Alternative'" />
Condition="'%(StaticWebAsset.AssetTraitName)' == 'BlazorWebAssemblyResource' or '%(StaticWebAsset.AssetTraitName)' == 'Culture' or '%(AssetRole)' == 'Alternative'" />
</ItemGroup>

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

<_BlazorPublishConfigFile
Include="@(StaticWebAsset)"
Condition="'%(AssetTraitName)' == 'BlazorWebAssemblyResource' and '%(AssetTraitValue)' == 'settings'"/>
Condition="'%(StaticWebAsset.AssetTraitName)' == 'BlazorWebAssemblyResource' and '%(StaticWebAsset.AssetTraitValue)' == 'settings'"/>

</ItemGroup>

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<!-- Static web assets defaults -->
<StaticWebAssetBasePath Condition="'$(StaticWebAssetBasePath)' == ''">/</StaticWebAssetBasePath>
<StaticWebAssetProjectMode Condition="'$(StaticWebAssetProjectMode)' == ''">Root</StaticWebAssetProjectMode>
<StaticWebAssetsPublishTarget>ComputeFilesToPublish</StaticWebAssetsPublishTarget>
<StaticWebAssetsGetPublishAssetsTargets>ComputeFilesToPublish;GetCurrentProjectPublishStaticWebAssetItems</StaticWebAssetsGetPublishAssetsTargets>
<StaticWebAssetsAdditionalPublishProperties>$(StaticWebAssetsAdditionalPublishProperties);BuildProjectReferences=false;ResolveAssemblyReferencesFindRelatedSatellites=true;_PublishingBlazorWasmProject=true</StaticWebAssetsAdditionalPublishProperties>
<StaticWebAssetsAdditionalPublishPropertiesToRemove>$(StaticWebAssetsAdditionalPublishPropertiesToRemove);NoBuild;RuntimeIdentifier</StaticWebAssetsAdditionalPublishPropertiesToRemove>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Copyright (c) .NET Foundation. All rights reserved.

<PropertyGroup>
<ResolveStaticWebAssetsInputsDependsOn>$(ResolveStaticWebAssetsInputsDependsOn);_AddServiceWorkerAssets</ResolveStaticWebAssetsInputsDependsOn>
<CopyStaticWebAssetsToPublishDirectoryDependsOn>$(CopyStaticWebAssetsToPublishDirectoryDependsOn);_WritePublishServiceWorkerAssetsManifest</CopyStaticWebAssetsToPublishDirectoryDependsOn>
<CopyStaticWebAssetsToOutputFolderDependsOn>$(CopyStaticWebAssetsToOutputFolderDependsOn);_WriteBuildServiceWorkerAssetsManifest</CopyStaticWebAssetsToOutputFolderDependsOn>
<GenerateComputedPublishStaticWebAssetsDependsOn>$(GenerateComputedPublishStaticWebAssetsDependsOn);_WritePublishServiceWorkerAssetsManifest</GenerateComputedPublishStaticWebAssetsDependsOn>
<GenerateComputedBuildStaticWebAssetsDependsOn>$(GenerateComputedBuildStaticWebAssetsDependsOn);_WriteBuildServiceWorkerAssetsManifest</GenerateComputedBuildStaticWebAssetsDependsOn>
</PropertyGroup>

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

</Target>

<Target Name="_WriteBuildServiceWorkerAssetsManifest" DependsOnTargets="_ComputeServiceWorkerAssets">
<Target Name="_WriteBuildServiceWorkerAssetsManifest" DependsOnTargets="ResolveStaticWebAssetsInputs">

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

<ComputeStaticWebAssetsForCurrentProject
Assets="@(_BuildAssetsForManifestCandidate)"
ProjectMode="$(StaticWebAssetProjectMode)"
AssetKind="Build"
Source="$(PackageId)"
>
<Output TaskParameter="StaticWebAssets" ItemName="_BuildAssetsForManifest" />
</ComputeStaticWebAssetsForCurrentProject>

<ComputeStaticWebAssetsTargetPaths Assets="@(_BuildAssetsForManifest)" PathPrefix="$(StaticWebAssetBasePath)" UseAlternatePathDirectorySeparator="true">
<Output TaskParameter="AssetsWithTargetPath" ItemName="_BuildAssetsForManifestWithTargetPath" />
</ComputeStaticWebAssetsTargetPaths>
Expand All @@ -123,7 +132,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<_BuildServiceWorkItem Include="@(_BuildAssetsForManifestWithTargetPath)">
<AssetUrl>%(_BuildAssetsForManifestWithTargetPath.TargetPath)</AssetUrl>
</_BuildServiceWorkItem>
<_BuildServiceWorkerFile Include="@(StaticWebAsset)" Condition="'%(AssetKind)' == 'Build' And '%(AssetTraitName)' == 'BlazorServiceWorker' And '%(AssetTraitValue)' == 'ServiceWorkerJs'" />
<_BuildServiceWorkerFile Include="@(StaticWebAsset)" Condition="'%(AssetKind)' == 'Build' And '%(StaticWebAsset.AssetTraitName)' == 'BlazorServiceWorker' And '%(StaticWebAsset.AssetTraitValue)' == 'ServiceWorkerJs'" />
</ItemGroup>

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

</Target>

<Target Name="_WritePublishServiceWorkerAssetsManifest" DependsOnTargets="_ComputeServiceWorkerAssets;LoadStaticWebAssetsPublishManifest" AfterTargets="GenerateStaticWebAssetsPublishManifest">
<Target Name="_WritePublishServiceWorkerAssetsManifest" DependsOnTargets="_ComputeServiceWorkerAssets;LoadStaticWebAssetsPublishManifest">

<ItemGroup>
<_PublishAssetsForManifest Include="@(StaticWebAsset)" Condition="'%(AssetTraitName)' != 'BlazorServiceWorker' and '%(AssetRole)' != 'Alternative'" />
<_PublishAssetsForManifestCandidate Include="@(StaticWebAsset)" Condition="'%(StaticWebAsset.AssetTraitName)' != 'BlazorServiceWorker' and '%(AssetRole)' != 'Alternative'" />
</ItemGroup>

<ComputeStaticWebAssetsForCurrentProject
Assets="@(_PublishAssetsForManifestCandidate)"
ProjectMode="$(StaticWebAssetProjectMode)"
AssetKind="Publish"
Source="$(PackageId)"
>
<Output TaskParameter="StaticWebAssets" ItemName="_PublishAssetsForManifest" />
</ComputeStaticWebAssetsForCurrentProject>

<ComputeStaticWebAssetsTargetPaths Assets="@(_PublishAssetsForManifest)" PathPrefix="$(StaticWebAssetBasePath)" UseAlternatePathDirectorySeparator="true">
<Output TaskParameter="AssetsWithTargetPath" ItemName="_AssetsForManifestWithTargetPath" />
<Output TaskParameter="AssetsWithTargetPath" ItemName="_PublishAssetsForManifestWithTargetPath" />
</ComputeStaticWebAssetsTargetPaths>

<ItemGroup>
<_ServiceWorkItem Include="@(_AssetsForManifestWithTargetPath)">
<AssetUrl>%(_AssetsForManifestWithTargetPath.TargetPath)</AssetUrl>
<_ServiceWorkItem Include="@(_PublishAssetsForManifestWithTargetPath)">
<AssetUrl>%(_PublishAssetsForManifestWithTargetPath.TargetPath)</AssetUrl>
</_ServiceWorkItem>
<_PublishServiceWorkerFile Include="@(StaticWebAsset)" Condition="'%(AssetKind)' == 'Publish' And '%(AssetTraitName)' == 'BlazorServiceWorker' And '%(AssetTraitValue)' == 'ServiceWorkerJs'" />
<_PublishServiceWorkerFile Include="@(StaticWebAsset)" Condition="'%(AssetKind)' == 'Publish' And '%(StaticWebAsset.AssetTraitName)' == 'BlazorServiceWorker' And '%(StaticWebAsset.AssetTraitValue)' == 'ServiceWorkerJs'" />
</ItemGroup>

<GenerateServiceWorkerAssetsManifest
Expand Down
1 change: 1 addition & 0 deletions src/RazorSdk/Razor.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"projects": [
"src\\BlazorWasmSdk\\Tasks\\Microsoft.NET.Sdk.BlazorWebAssembly.Tasks.csproj",
"src\\BlazorWasmSdk\\Tool\\Microsoft.NET.Sdk.BlazorWebAssembly.Tool.csproj",
"src\\Cli\\Microsoft.DotNet.Cli.Utils\\Microsoft.DotNet.Cli.Utils.csproj",
"src\\RazorSdk\\SourceGenerators\\Microsoft.NET.Sdk.Razor.SourceGenerators.csproj",
"src\\RazorSdk\\Tasks\\Microsoft.NET.Sdk.Razor.Tasks.csproj",
"src\\RazorSdk\\Tool\\Microsoft.NET.Sdk.Razor.Tool.csproj",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ Integration with static web assets:
_AddGeneratedScopedCssFilesForReference;
</GenerateStaticWebAssetsPackTargetsDependsOn>

<GetCurrentProjectStaticWebAssetsV2DependsOn>
$(GetCurrentProjectStaticWebAssetsV2DependsOn);
_UpdateScopedCssStaticWebAssetsForV2;
</GetCurrentProjectStaticWebAssetsV2DependsOn>

<!-- 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
referenced class libraries. This way, we can use that information to adjust the build pipeline without having to rely on external
sources like an additional itemgroup or metadata.
Expand Down Expand Up @@ -390,4 +395,43 @@ Integration with static web assets:
</ItemGroup>
</Target>

<!-- Forward compat with new static web assets -->

<Target Name="_UpdateScopedCssStaticWebAssetsForV2">
<PropertyGroup>
<_AppBundleStaticWebAssetIdentity>@(_AppBundleStaticWebAsset)</_AppBundleStaticWebAssetIdentity>
<_ProjectBundleStaticWebAssetIdentity>@(_ProjectBundleStaticWebAsset)</_ProjectBundleStaticWebAssetIdentity>
</PropertyGroup>

<ItemGroup>
<!-- <<App>>.bundle.scp.css -->
<_CurrentProjectBundle Include="@(_ThisProjectStaticWebAssets)" Condition="'%(SourceId)' == '$(PackageId)' and '%(_ThisProjectStaticWebAssets.Identity)' == '$(_ProjectBundleStaticWebAssetIdentity)'">
<AssetMode>Reference</AssetMode>
<AssetRole>Primary</AssetRole>
<RelatedAsset></RelatedAsset>
<AssetTraitName>ScopedCss</AssetTraitName>
<AssetTraitValue>ProjectBundle</AssetTraitValue>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</_CurrentProjectBundle>

<_ThisProjectStaticWebAssets Remove="@(_CurrentProjectBundle)" />
<_ThisProjectStaticWebAssets Include="@(_CurrentProjectBundle)" />

<!-- <<App>>.styles.css -->
<_CurrentProjectAppBundle Include="@(_ThisProjectStaticWebAssets)" Condition="'%(SourceId)' == '$(PackageId)' and '%(Identity)' == '$(_AppBundleStaticWebAssetIdentity)'">
<AssetMode>CurrentProject</AssetMode>
<AssetRole>Primary</AssetRole>
<RelatedAsset></RelatedAsset>
<AssetTraitName>ScopedCss</AssetTraitName>
<AssetTraitValue>ApplicationBundle</AssetTraitValue>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</_CurrentProjectAppBundle>

<_ThisProjectStaticWebAssets Remove="@(_CurrentProjectAppBundle)" />
<_ThisProjectStaticWebAssets Include="@(_CurrentProjectAppBundle)" />
</ItemGroup>
</Target>

</Project>
14 changes: 6 additions & 8 deletions src/RazorSdk/Targets/Microsoft.NET.Sdk.Razor.ScopedCss.targets
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ Integration with static web assets:
_AddScopedCssBundles;
</ResolveStaticWebAssetsInputsDependsOn>

<CopyStaticWebAssetsToOutputFolderDependsOn>
$(CopyStaticWebAssetsToOutputFolderDependsOn);
<GenerateComputedBuildStaticWebAssetsDependsOn>
$(GenerateComputedBuildStaticWebAssetsDependsOn);
_ResolveBundlingConfiguration;
_GenerateScopedCssFiles;
BundleScopedCssFiles;
</CopyStaticWebAssetsToOutputFolderDependsOn>
</GenerateComputedBuildStaticWebAssetsDependsOn>

</PropertyGroup>

Expand All @@ -75,10 +75,10 @@ Integration with static web assets:
_AddGeneratedScopedCssFiles;
</ResolveStaticWebAssetsInputsDependsOn>

<CopyStaticWebAssetsToOutputFolderDependsOn>
<GenerateComputedBuildStaticWebAssetsDependsOn>
_GenerateScopedCssFiles;
$(CopyStaticWebAssetsToOutputFolderDependsOn);
</CopyStaticWebAssetsToOutputFolderDependsOn>
$(GenerateComputedBuildStaticWebAssetsDependsOn);
</GenerateComputedBuildStaticWebAssetsDependsOn>

</PropertyGroup>

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

<Target Name="_PrepareForScopedCss" DependsOnTargets="$(_PrepareForScopedCssDependsOn)" />

<Target Name="ResolveScopedCssInputs">
<!--
Gathers input source files for Razor component generation. This is a separate target so that we can avoid
Expand Down
Loading