-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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), 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:
-
Clean build: TypeScript outputs are not discovered as static web assets because they don't exist yet when
ResolveProjectStaticWebAssetsruns. The files only appear to work during development due to a runtime fallback that serves files added after build-time discovery. -
Rebuild: The Razor SDK's default globbing automatically includes
wwwrootfiles asContentitems. During Clean, the TypeScript targets delete the.jsfiles, but theContentitems persist in memory. When Build runs,DefineStaticWebAssetsfails because it findsContentitems 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:
- Hooks into
ResolveStaticWebAssetsInputsDependsOnto register TypeScript outputs as static web assets after compilation - Removes TypeScript outputs from
ContentbeforeCoreCleanandResolveProjectStaticWebAssetsto 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 | |
| Rebuild | ❌ Fails |
| Incremental build after modifying .ts | ❌ May fail |
| Clean followed by build | ❌ May fail |
| Publish |
Related
This follows the same integration pattern as:
Microsoft.NET.Sdk.StaticWebAssets.JSModules.targetsMicrosoft.NET.Sdk.StaticWebAssets.ScopedCss.targets