.netasp.net-corewebpackmsbuilddotnet-publish

Exclude css isolation assets from dotnet publish


I'm using RazorPages (on ASP.NET Core 6) and the css isolation feature. The css files are generated at obj/{Debug,Release}/net6.0/scopedcss/Pages/, and are included when performing a dotnet publish.

I also use Webpack, so I don't need to publish those css files (they are already bundled by Webpack).

I added variations of all of these to my Project.csproj without success:

<ItemGroup>
  <Content Remove="obj/*/*/scopedcss/**/*" />
</ItemGroup>
<ItemGroup>
  <Content Remove="*.cshtml.rz.scp.css" />
</ItemGroup>
<ItemGroup>
  <None Remove="obj/*/*/scopedcss/**/*" />
</ItemGroup>
<ItemGroup>
  <Compile Remove="*.cshtml.rz.scp.css" />
</ItemGroup>
<ItemGroup>
  <PublishedFiles Remove="obj/*/*/scopedcss/**/*" />
</ItemGroup>
<PropertyGroup>
  <DefaultItemExcludes>$(DefaultItemExcludes);obj/*/*/scopedcss/**/*</DefaultItemExcludes>
</PropertyGroup>

How can I exclude the css isolation assets from the published output?

UPDATE
I found the msbuild stuff that controls this, but don't know how to prevent it from copying during publish. This bit in the SDK imports the feature's msbuild targets, which are here and here.


Solution

  • This is unsupported for now, but will be implemented in net7 (hopefully).

    A workaround using "private" properties:

    <Target Name="_RemoveScopedCssFiles" AfterTargets="_AddGeneratedScopedCssFiles">
      <ItemGroup>
        <StaticWebAsset Remove="@(StaticWebAsset)" Condition="$([System.String]::Copy('%(Identity)').EndsWith('rz.scp.css'))">
      </ItemGroup>
    </Target>
    

    I'd be hesitant to use that as it's not part of the public contract / interface. It may suit you though.

    A slow and ugly, but safe, workaround, is to let the runtime publish the files, then delete them:

    <Target Name="DeleteScopedCssAssetsAfterPublish" AfterTargets="AfterPublish">
      <RemoveDir Directories="$(PublishUrl)/wwwroot/Pages" />
    </Target>