Skip to content

Commit 4d4c8fb

Browse files
authored
Copydotnet.d.ts to wwwroot on build/publish. (#120097)
- WebAssembly builds now expose an opt-in `WasmEmitTypeScriptDefinitions` MSBuild property (default `false`) so projects can request the generated `dotnet.d.ts` typings. - A new `_EnsureDotnetTypeScriptDefinitions` target copies `dotnet.d.ts` from the runtime pack into the project’s `wwwroot` on build or publish when that property is enabled. - Added a test (`TypeScriptDefinitionsCopiedToWwwrootOnBuild`) to cover Debug/Release and verify the file is produced only when requested. Fixes #77174.
1 parent e155b45 commit 4d4c8fb

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ Copyright (c) .NET Foundation. All rights reserved.
191191
<_WasmCopyOutputSymbolsToOutputDirectory Condition="'$(_WasmCopyOutputSymbolsToOutputDirectory)'==''">true</_WasmCopyOutputSymbolsToOutputDirectory>
192192
<_WasmEnableThreads>$(WasmEnableThreads)</_WasmEnableThreads>
193193
<_WasmEnableThreads Condition="'$(_WasmEnableThreads)' == ''">false</_WasmEnableThreads>
194+
<_WasmEmitTypeScriptDefinitions>$(WasmEmitTypeScriptDefinitions)</_WasmEmitTypeScriptDefinitions>
195+
<_WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == ''">false</_WasmEmitTypeScriptDefinitions>
194196

195197
<_WasmEnableWebcil>$(WasmEnableWebcil)</_WasmEnableWebcil>
196198
<_WasmEnableWebcil Condition="'$(_TargetingNET80OrLater)' != 'true'">false</_WasmEnableWebcil>
@@ -885,4 +887,23 @@ Copyright (c) .NET Foundation. All rights reserved.
885887
<WasmAssembliesToBundle Include="@(ResolvedFileToPublish)" Exclude="@(_Exclude)" Condition="%(Extension) == '.dll'" />
886888
</ItemGroup>
887889
</Target>
890+
891+
<!-- Ensure dotnet.d.ts is available in wwwroot for TypeScript development experience when enabled -->
892+
<Target Name="_EnsureDotnetTypeScriptDefinitions"
893+
AfterTargets="_ResolveWasmConfiguration"
894+
Condition="'$(_WasmEmitTypeScriptDefinitions)' == 'true'">
895+
<PropertyGroup>
896+
<_RuntimePackDir>$(MicrosoftNetCoreAppRuntimePackDir)</_RuntimePackDir>
897+
<_RuntimePackDir Condition="'$(_RuntimePackDir)' == ''">%(ResolvedRuntimePack.PackageDirectory)</_RuntimePackDir>
898+
<_RuntimePackNativeDir>$([MSBuild]::NormalizeDirectory($(_RuntimePackDir), 'runtimes', 'browser-wasm', 'native'))</_RuntimePackNativeDir>
899+
<_DotnetTypesSourcePath>$([MSBuild]::NormalizePath($(_RuntimePackNativeDir), 'dotnet.d.ts'))</_DotnetTypesSourcePath>
900+
<_DotnetTypesDestPath>$(MSBuildProjectDirectory)\wwwroot\dotnet.d.ts</_DotnetTypesDestPath>
901+
</PropertyGroup>
902+
903+
<!-- Copy dotnet.d.ts if source exists. MSBuild will skip if files are identical. -->
904+
<Copy SourceFiles="$(_DotnetTypesSourcePath)"
905+
DestinationFiles="$(_DotnetTypesDestPath)"
906+
Condition="Exists('$(_DotnetTypesSourcePath)')"
907+
SkipUnchangedFiles="true" />
908+
</Target>
888909
</Project>

src/mono/wasm/Wasm.Build.Tests/Templates/WasmTemplateTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,37 @@ public async Task LibraryModeBuild(bool useWasmSdk)
317317
}
318318

319319
}
320+
321+
[Theory]
322+
[InlineData(Configuration.Debug, true)]
323+
[InlineData(Configuration.Release, true)]
324+
[InlineData(Configuration.Debug, false)]
325+
[InlineData(Configuration.Release, false)]
326+
public void TypeScriptDefinitionsCopiedToWwwrootOnBuild(Configuration config, bool emitTypeScriptDts)
327+
{
328+
string shouldEmit = emitTypeScriptDts ? "true" : "false";
329+
string emitTypeScriptDtsProp = $"<WasmEmitTypeScriptDefinitions>{shouldEmit}</WasmEmitTypeScriptDefinitions>";
330+
ProjectInfo info = CreateWasmTemplateProject(Template.WasmBrowser, config, aot: false, "tsdefs", extraProperties: emitTypeScriptDtsProp);
331+
332+
string projectDirectory = Path.GetDirectoryName(info.ProjectFilePath)!;
333+
string dotnetDtsWwwrootPath = Path.Combine(projectDirectory, "wwwroot", "dotnet.d.ts");
334+
335+
// Verify dotnet.d.ts is not in wwwroot after creation
336+
Assert.False(File.Exists(dotnetDtsWwwrootPath), $"dotnet.d.ts should not exist at {dotnetDtsWwwrootPath} after creation of the project");
337+
338+
// Build to trigger the _EnsureDotnetTypeScriptDefinitions target during the build phase
339+
BuildProject(info, config, new BuildOptions());
340+
341+
// Verify dotnet.d.ts presence in the project's wwwroot directory after build
342+
bool fileExists = File.Exists(dotnetDtsWwwrootPath);
343+
if (emitTypeScriptDts)
344+
{
345+
Assert.True(fileExists, $"dotnet.d.ts should be created at {dotnetDtsWwwrootPath} after the build with WasmEmitTypeScriptDefinitions={shouldEmit}");
346+
}
347+
else
348+
{
349+
Assert.False(fileExists, $"dotnet.d.ts should not exist at {dotnetDtsWwwrootPath} after the build with WasmEmitTypeScriptDefinitions={shouldEmit}");
350+
}
351+
}
320352
}
321353
}

src/mono/wasm/build/WasmApp.Common.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@
9393
- AppBundle/_content contains web files from nuget packages (css, js, etc)
9494
- $(WasmStripILAfterAOT) - Set to true to enable trimming away AOT compiled methods body (IL code)
9595
Defaults to true.
96+
- $(WasmEmitTypeScriptDefinitions) - Controls whether TypeScript definitions (dotnet.d.ts) should be copied to the project's `wwwroot`.
97+
Defaults to false.
9698
9799
Public items:
98100
- @(WasmExtraFilesToDeploy) - Files to copy to $(WasmAppDir).

0 commit comments

Comments
 (0)