Skip to content

[Blazor] Wire up CSS isolation for the current project #24221

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

Merged
merged 11 commits into from
Jul 24, 2020

Conversation

javiercn
Copy link
Member

  • Works E2E
  • Needs cleanups
  • Needs tests

Copy link
Contributor

@pranavkm pranavkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@pranavkm pranavkm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great

@@ -64,6 +64,9 @@ Copyright (c) .NET Foundation. All rights reserved.
<!-- Controls whether or not the static web assets feature is enabled. By default is enabled for netcoreapp3.0
applications and RazorLangVersion 3 or above. -->
<StaticWebAssetsEnabled Condition="'$(StaticWebAssetsEnabled)' == ''">$(_Targeting30OrNewerRazorLangVersion)</StaticWebAssetsEnabled>

<!-- Controls whether or not the scoped css feature is enabled. By default is enabled for net5.0 applications and RazorLangVersion 5 or above -->
<ScopedCssEnabled Condition="'$(ScopedCssEnabled)' == ''">$(_Targeting30OrNewerRazorLangVersion)</ScopedCssEnabled>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be 5.0 or newer?

Copy link
Member Author

@javiercn javiercn Jul 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't get this to work, Wasm.Authentication.Client was complaining about it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to pass /p:ScopedCssEnabled to make it work. I'll revisit later on, but we could fix it after preview8 if necessary

@Pilchie Pilchie added the area-blazor Includes: Blazor, Razor Components label Jul 23, 2020
@@ -269,7 +269,7 @@ public void Fails_WhenStaticWebAsset_HaveDifferentSourceId()
Assert.Equal(expectedError, message);
}

[Fact]
[Fact(Skip = "https://github.com/dotnet/aspnetcore/issues/24257")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to relax this restriction. The issue is for future follow-up

Comment on lines 214 to 234
<Target Name="_AddBundleToStaticWebAssetsPublishedFile" Condition="'$(ScopedCssDisableBundling)' != 'true' and '@(_AllScopedCss)' != ''" BeforeTargets="_StaticWebAssetsComputeFilesToPublish" DependsOnTargets="_CollectAllScopedCssAssets">
<ItemGroup>
<!-- Manually add the file to the publish flow. See https://github.com/dotnet/aspnetcore/issues/24245 -->
<_ExternalPublishStaticWebAsset Include="$(_ScopedCssOutputFullPath)">
<SourceType>generated</SourceType>
<SourceId>$(PackageId)</SourceId>
<ContentRoot>$(_ScopedCssIntermediatePath)</ContentRoot>
<BasePath>$(StaticWebAssetBasePath)</BasePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<RelativePath>$([MSBuild]::MakeRelative('$(MSBuildProjectDirectory)',$([MSBuild]::NormalizePath('wwwroot/$(StaticWebAssetBasePath)/_framework/scoped.styles.css'))))</RelativePath>
</_ExternalPublishStaticWebAsset>
</ItemGroup>
</Target>

<Target Name="_AdjustIsolatedCssPackageContents" BeforeTargets="_RemoveWebRootContentFromPackaging;_CreateStaticWebAssetsCustomPropsCacheFile" DependsOnTargets="_CollectAllScopedCssAssets">
<ItemGroup>
<_CurrentProjectStaticWebAsset Remove="$(_ScopedCssOutputFullPath)" />
<StaticWebAsset Remove="$(_ScopedCssOutputFullPath)" />
<StaticWebAsset Include="@(_AllScopedCss)" Condition="'%(SourceType)' == ''" />
</ItemGroup>
</Target>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part gets a bit tricky due to the lack of support for assets representing generated content. #24245 will address that.

@javiercn javiercn marked this pull request as ready for review July 24, 2020 02:15
@javiercn javiercn requested review from SteveSandersonMS and a team as code owners July 24, 2020 02:15
// can update the Content item for the .razor.css file with Scoped=false and we will not consider it.
foreach (var unmatched in unmatchedScopedCss)
{
Log.LogError($"The scoped css file '{unmatched.ItemSpec}' was defined but no associated razor component was found for it.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do this in a follow up, but it's useful to include an error code with these errors.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be an acceptable error code? I tried to search for previous history on this but couldn't find anything.

{
var componentCandidate = RazorComponents[i];
var j = 0;
while (j < unmatchedScopedCss.Count)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👯

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

return builder.ToString();
}

private string ToBase36(byte[] hash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private string ToBase36(byte[] hash)
private string ToBase36(byte[] hash)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@javiercn javiercn merged commit 3a81992 into master Jul 24, 2020
@javiercn javiercn deleted the javiercn/css-isolation-swa branch July 24, 2020 07:02
javiercn added a commit that referenced this pull request Jul 24, 2020
* Wires up CSS isolation on the build.
* Transforms the css files during build.
* Bundles all scopes css into a single file and exposes it on _framework/scoped.styles.cs
* Packs pre-processed files as static web assets.
mkArtakMSFT pushed a commit that referenced this pull request Jul 24, 2020
* Wires up CSS isolation on the build.
* Transforms the css files during build.
* Bundles all scopes css into a single file and exposes it on _framework/scoped.styles.cs
* Packs pre-processed files as static web assets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants