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 ResolveProjectStaticWebAssets runs. 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 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:
- Hooks into
ResolveStaticWebAssetsInputsDependsOn to register TypeScript outputs as static web assets after compilation
- 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
Problem
When using the
Microsoft.TypeScript.MSBuildNuGet package in Razor Class Libraries with TypeScript configured to output to thewwwrootfolder (as described in the official documentation), builds fail or produce incorrect results.Root Cause
The
Microsoft.TypeScript.MSBuildpackage outputs compiled.jsfiles towwwroot, which is treated as an input folder by the Static Web Assets SDK. However, TypeScript compilation runs during theCompilephase, 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:The TypeScript package does not coordinate with the Static Web Assets system to properly register its outputs or clean up
Contentitem references when its outputs are deleted.Proposed Solution
Add a new targets file
Microsoft.NET.Sdk.StaticWebAssets.TypeScript.targetsthat:ResolveStaticWebAssetsInputsDependsOnto register TypeScript outputs as static web assets after compilationContentbeforeCoreCleanandResolveProjectStaticWebAssetsto prevent stale item referencesThe targets file should be conditionally imported when
EnableTypeScriptNuGetTarget='true'(property set by theMicrosoft.TypeScript.MSBuildNuGet package).Affected Scenarios
Related
This follows the same integration pattern as:
Microsoft.NET.Sdk.StaticWebAssets.JSModules.targetsMicrosoft.NET.Sdk.StaticWebAssets.ScopedCss.targets