dotnet core nuget package copying content files on restore

From: Announcing NuGet 3.1 with Support for Universal Windows Platform

Importing content from a Nuget package was depreciated for projects using a project.json file in Nuget v3.1. Since then the project.json file has been dropped in favour of the new .csproj format. Importing content from a Nuget package should still work though if you're using the packages.config file instead.

Also mentioned is the fact that there are other package managers available for delivering content.

It looks to me like the answer in the new world is to create a node module containing utility.js and let npm deliver it to your project.

Possible Workaround:

I've looked at .targets to copy files and got this working, but it does run on each build - which may or may not be a problem for you. I can't do what I want with it.

image

In [PackageId].targets:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <!-- Either do this for all scripts in the Scripts/js folder -->
    <Target Name="CopyScriptsToProject" BeforeTargets="Build">
        <Message Text="Copying scripts to project" />
        <ItemGroup>
            <SourceScripts Include="$(MSBuildThisFileDirectory)..\..\content\Scripts\js\**\*.*"/>
        </ItemGroup>
        <Copy SourceFiles="@(SourceScripts)" DestinationFiles="@(SourceScripts -> '$(MSBuildProjectDirectory)\wwwroot\js\%(RecursiveDir)%(Filename)%(Extension)')" Condition="!Exists('$(MSBuildProjectDirectory)\wwwroot\js\%(RecursiveDir)%(Filename)%(Extension)')" />
    </Target>
    <!-- Or do this for the individual script -->
    <Target Name="CopyUtilityScriptToProject" BeforeTargets="Build">
        <Copy SourceFiles="$(MSBuildThisFileDirectory)..\..\content\Scripts\js\Utility.js" DestinationFiles="$(MSBuildProjectDirectory)\wwwroot\js\Utility.js" Condition="!Exists('$(MSBuildProjectDirectory)\wwwroot\js\Utility.js')" />
    </Target>
</Project>
<!-- Note: condition can be removed from either if you want it to overwrite each build -->

and in the .csproj file (replacing [PackageId] with the name of your package):

<Project Sdk="Microsoft.NET.Sdk">
    ... any Globals for source control stuff ...
    <PropertyGroup>
        <TargetFramework>netcoreapp2.0</TargetFramework>
        <Version>7.0.0</Version>
        <PackageId>[PackageId]</PackageId>
    </PropertyGroup>
    ... any PackageReference stuff ...
    <ItemGroup Label="Packaging">
        <Content Include="build\netcoreapp2.0\[PackageId].targets" PackagePath="build\netcoreapp2.0\[PackageId].targets" />
            <!-- Either -->
            <Content Include="Scripts\js\**\*.*" PackagePath="content\Scripts\js;contentFiles\Scripts\js" />
            <!-- or -->
            <Content Include="Scripts\js\Utility.js" PackagePath="content\Scripts\js;contentFiles\Scripts\js" />
        </ItemGroup>
</Project> 

There seemed to be a bug whereby when the <PackageId>[PackageId]</PackageId> wasn't set explicitly in the .csproj, the build targets didn't work. Although that may well be an issue with my development environment.


Apparently you need the any\any in the path (learn more) as well as to include <PackageCopyToOutput>true</PackageCopyToOutput>, like this:

<ItemGroup>
  <Content Include="Scripts\js\Utility.js">
    <Pack>true</Pack>

    <PackagePath>contentFiles\any\any\wwwroot\js\;content\any\any\wwwroot\js\</PackagePath>

    <PackageCopyToOutput>true</PackageCopyToOutput>
  </Content>
</ItemGroup>

You'll also need to precompile your TypeScript before including the .js files in the package

However, this still doesn't create a file there, just some strange reference to it.

In the end, we got it working with a .targets file, you can find a working repo here: https://github.com/NuGet/Home/issues/6743