diff --git a/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets b/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets
index b8c831e44c1516..4a82b8300dbcae 100644
--- a/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets
+++ b/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets
@@ -191,6 +191,8 @@ Copyright (c) .NET Foundation. All rights reserved.
<_WasmCopyOutputSymbolsToOutputDirectory Condition="'$(_WasmCopyOutputSymbolsToOutputDirectory)'==''">true
<_WasmEnableThreads>$(WasmEnableThreads)
<_WasmEnableThreads Condition="'$(_WasmEnableThreads)' == ''">false
+ <_WasmEmitTypeScriptDefinitions>$(WasmEmitTypeScriptDefinitions)
+ <_WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == ''">false
<_WasmEnableWebcil>$(WasmEnableWebcil)
<_WasmEnableWebcil Condition="'$(_TargetingNET80OrLater)' != 'true'">false
@@ -856,4 +858,23 @@ Copyright (c) .NET Foundation. All rights reserved.
+
+
+
+
+ <_RuntimePackDir>$(MicrosoftNetCoreAppRuntimePackDir)
+ <_RuntimePackDir Condition="'$(_RuntimePackDir)' == ''">%(ResolvedRuntimePack.PackageDirectory)
+ <_RuntimePackNativeDir>$([MSBuild]::NormalizeDirectory($(_RuntimePackDir), 'runtimes', 'browser-wasm', 'native'))
+ <_DotnetTypesSourcePath>$([MSBuild]::NormalizePath($(_RuntimePackNativeDir), 'dotnet.d.ts'))
+ <_DotnetTypesDestPath>$(MSBuildProjectDirectory)\wwwroot\dotnet.d.ts
+
+
+
+
+
diff --git a/src/mono/wasm/Wasm.Build.Tests/Templates/WasmTemplateTests.cs b/src/mono/wasm/Wasm.Build.Tests/Templates/WasmTemplateTests.cs
index 65f0f39b92a567..b458e471c57230 100644
--- a/src/mono/wasm/Wasm.Build.Tests/Templates/WasmTemplateTests.cs
+++ b/src/mono/wasm/Wasm.Build.Tests/Templates/WasmTemplateTests.cs
@@ -317,5 +317,37 @@ public async Task LibraryModeBuild(bool useWasmSdk)
}
}
+
+ [Theory]
+ [InlineData(Configuration.Debug, true)]
+ [InlineData(Configuration.Release, true)]
+ [InlineData(Configuration.Debug, false)]
+ [InlineData(Configuration.Release, false)]
+ public void TypeScriptDefinitionsCopiedToWwwrootOnBuild(Configuration config, bool emitTypeScriptDts)
+ {
+ string shouldEmit = emitTypeScriptDts ? "true" : "false";
+ string emitTypeScriptDtsProp = $"{shouldEmit}";
+ ProjectInfo info = CreateWasmTemplateProject(Template.WasmBrowser, config, aot: false, "tsdefs", extraProperties: emitTypeScriptDtsProp);
+
+ string projectDirectory = Path.GetDirectoryName(info.ProjectFilePath)!;
+ string dotnetDtsWwwrootPath = Path.Combine(projectDirectory, "wwwroot", "dotnet.d.ts");
+
+ // Verify dotnet.d.ts is not in wwwroot after creation
+ Assert.False(File.Exists(dotnetDtsWwwrootPath), $"dotnet.d.ts should not exist at {dotnetDtsWwwrootPath} after creation of the project");
+
+ // Build to trigger the _EnsureDotnetTypeScriptDefinitions target during the build phase
+ BuildProject(info, config, new BuildOptions());
+
+ // Verify dotnet.d.ts presence in the project's wwwroot directory after build
+ bool fileExists = File.Exists(dotnetDtsWwwrootPath);
+ if (emitTypeScriptDts)
+ {
+ Assert.True(fileExists, $"dotnet.d.ts should be created at {dotnetDtsWwwrootPath} after the build with WasmEmitTypeScriptDefinitions={shouldEmit}");
+ }
+ else
+ {
+ Assert.False(fileExists, $"dotnet.d.ts should not exist at {dotnetDtsWwwrootPath} after the build with WasmEmitTypeScriptDefinitions={shouldEmit}");
+ }
+ }
}
}
diff --git a/src/mono/wasm/build/WasmApp.Common.targets b/src/mono/wasm/build/WasmApp.Common.targets
index d9bf8e3b931dae..4d66ab266868be 100644
--- a/src/mono/wasm/build/WasmApp.Common.targets
+++ b/src/mono/wasm/build/WasmApp.Common.targets
@@ -93,6 +93,8 @@
- AppBundle/_content contains web files from nuget packages (css, js, etc)
- $(WasmStripILAfterAOT) - Set to true to enable trimming away AOT compiled methods body (IL code)
Defaults to true.
+ - $(WasmEmitTypeScriptDefinitions) - Controls whether TypeScript definitions (dotnet.d.ts) should be copied to the project's `wwwroot`.
+ Defaults to false.
Public items:
- @(WasmExtraFilesToDeploy) - Files to copy to $(WasmAppDir).