dotnet / dotnet/sdk

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

Open
#52,301 4 comments 1 reaction 0 assignees View on GitHub
Area-AspNetCore StaticWebAssets
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### 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](https://learn.microsoft.com/en-us/aspnet/core/razor-pages/ui-class#typescript-integration)), 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`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.