Asp.net-core – How to exclude a folder from a project but not from publish

asp.net-coremsbuildvisual-studio-2017

I've migrated an asp.net core project to VS2017 RC, which now supports the ability to exclude files from a project. I've excluded two folders, which added these lines to my csproj file:

<ItemGroup>
  <Content Remove="wwwroot\dist\**" />
  <Content Remove="wwwroot\lib\**" />
</ItemGroup>

This works great, except now those files don't get published anymore. How do I exclude these folders from the project but still include them on publish?

Best Answer

The following msbuild items taken from the dotnet new spa templates helped me to achieve what I believe you are after:

<ItemGroup>
    <Content Remove="wwwroot\dist\**" />
    <Content Remove="wwwroot\lib\**" />
</ItemGroup>

<Target Name="GiveAName" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec Command="npm install" />
    <Exec Command="npm etc etc do your stuff" />
    <Exec Command="or webpack" />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
        <DistFiles Include="wwwroot\dist\**; wwwroot\lib\**" />
        <ResolvedFileToPublish Include="@(DistFiles-&gt;'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
            <RelativePath>%(DistFiles.Identity)</RelativePath>
            <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        </ResolvedFileToPublish>
    </ItemGroup>
</Target>