Search code examples
typescriptmsbuildnuget.net-6.0csproj

How to include typescript output as staticwebassets in nuget package when using Microsoft.TypeScript.MSBuild


I have a following razor class library following files

  • scripts/main.ts
  • scripts/tsconfig.ts
  • wwwroot/styles.css

The typescript builds fine, but when I pack the project, the javascript output is not included in the package under staticwebassets:

nuget structure

How do I properly integrate typescript with msbuild so that the generated output is included as static web assets?


this is my current csproj

<Project Sdk="Microsoft.NET.Sdk.Razor">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>enable</Nullable>
        <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
        <Authors>Liero</Authors>
        <Version>0.7.3</Version>
        <PackageTags>Blazor DataGrid</PackageTags>
        <EmbedUntrackedSources>true</EmbedUntrackedSources>
        <DebugType>embedded</DebugType>
    </PropertyGroup>

    <PropertyGroup>
        <TypescriptOutDir>wwwroot</TypescriptOutDir>
        <ResolveCurrentProjectStaticWebAssetsInputsDependsOn>
            TypeScriptCompile;
            $(ResolveCurrentProjectStaticWebAssetsInputsDependsOn)
        </ResolveCurrentProjectStaticWebAssetsInputsDependsOn>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.0" />
        <PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.6.4">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
    </ItemGroup>

</Project>

Solution

  • the PrepareForBuildDependsOn should be like this:

    <PropertyGroup>
        <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
        <PrepareForBuildDependsOn>CompileTypeScript;GetTypeScriptOutputForPublishing;$(PrepareForBuildDependsOn)</PrepareForBuildDependsOn>
        <TypescriptOutDir>wwwroot</TypescriptOutDir>
    </PropertyGroup>
    
    
    <ItemGroup>
        <PackageReference Include="Microsoft.TypeScript.MSBuild" Version="4.6.4">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
    </ItemGroup>
    
    

    see https://github.com/dotnet/aspnetcore/issues/42110