Skip to content

Add Static Web Assets integration for Microsoft.TypeScript.MSBuild NuGet package #52301

@javiercn

Description

@javiercn

Problem

When using the Microsoft.TypeScript.MSBuild NuGet package in Razor Class Libraries with TypeScript configured to output to the wwwroot folder (as described in the official documentation), builds fail or produce incorrect results.

Root Cause

The Microsoft.TypeScript.MSBuild package outputs compiled .js files to wwwroot, which is treated as an input folder by the Static Web Assets SDK. However, TypeScript compilation runs during the Compile phase, after Static Web Assets discovery has already occurred.

This creates two problems:

  1. Clean build: TypeScript outputs are not discovered as static web assets because they don't exist yet when ResolveProjectStaticWebAssets runs. The files only appear to work during development due to a runtime fallback that serves files added after build-time discovery.

  2. Rebuild: The Razor SDK's default globbing automatically includes wwwroot files as Content items. During Clean, the TypeScript targets delete the .js files, but the Content items persist in memory. When Build runs, DefineStaticWebAssets fails because it finds Content items referencing files that no longer exist:

error : The static web asset 'wwwroot\MyScript.js' has source type 'Discovered' and is required 
to exist on disk at 'D:\MyProject\wwwroot\MyScript.js', but the file does not exist.

The TypeScript package does not coordinate with the Static Web Assets system to properly register its outputs or clean up Content item references when its outputs are deleted.

Proposed Solution

Add a new targets file Microsoft.NET.Sdk.StaticWebAssets.TypeScript.targets that:

  1. Hooks into ResolveStaticWebAssetsInputsDependsOn to register TypeScript outputs as static web assets after compilation
  2. Removes TypeScript outputs from Content before CoreClean and ResolveProjectStaticWebAssets to prevent stale item references

The targets file should be conditionally imported when EnableTypeScriptNuGetTarget='true' (property set by the Microsoft.TypeScript.MSBuild NuGet package).

Affected Scenarios

Scenario Status
Clean build ⚠️ Assets not properly discovered (works only via dev fallback)
Rebuild ❌ Fails
Incremental build after modifying .ts ❌ May fail
Clean followed by build ❌ May fail
Publish ⚠️ Assets may be missing or incorrectly processed

Related

This follows the same integration pattern as:

  • Microsoft.NET.Sdk.StaticWebAssets.JSModules.targets
  • Microsoft.NET.Sdk.StaticWebAssets.ScopedCss.targets

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-AspNetCoreRazorSDK, BlazorWebAssemblySDK, dotnet-watchuntriagedRequest triage from a team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions