Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<_WasmCopyOutputSymbolsToOutputDirectory Condition="'$(_WasmCopyOutputSymbolsToOutputDirectory)'==''">true</_WasmCopyOutputSymbolsToOutputDirectory>
<_WasmEnableThreads>$(WasmEnableThreads)</_WasmEnableThreads>
<_WasmEnableThreads Condition="'$(_WasmEnableThreads)' == ''">false</_WasmEnableThreads>
<WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == ''">false</WasmEmitTypeScriptDefinitions>

<_WasmEnableWebcil>$(WasmEnableWebcil)</_WasmEnableWebcil>
<_WasmEnableWebcil Condition="'$(_TargetingNET80OrLater)' != 'true'">false</_WasmEnableWebcil>
Expand Down Expand Up @@ -856,4 +857,23 @@ Copyright (c) .NET Foundation. All rights reserved.
<WasmAssembliesToBundle Include="@(ResolvedFileToPublish)" Exclude="@(_Exclude)" Condition="%(Extension) == '.dll'" />
</ItemGroup>
</Target>

<!-- Ensure dotnet.d.ts is available in wwwroot for TypeScript development experience when enabled -->
<Target Name="_EnsureDotnetTypeScriptDefinitions"
AfterTargets="Restore"
Condition="'$(WasmEmitTypeScriptDefinitions)' == 'true'">
<PropertyGroup>
<_RuntimePackDir>$(MicrosoftNetCoreAppRuntimePackDir)</_RuntimePackDir>
<_RuntimePackDir Condition="'$(_RuntimePackDir)' == ''">%(ResolvedRuntimePack.PackageDirectory)</_RuntimePackDir>
<_RuntimePackNativeDir>$([MSBuild]::NormalizeDirectory($(_RuntimePackDir), 'runtimes', 'browser-wasm', 'native'))</_RuntimePackNativeDir>
<_DotnetTypesSourcePath>$(_RuntimePackNativeDir)dotnet.d.ts</_DotnetTypesSourcePath>
<_DotnetTypesDestPath>$(MSBuildProjectDirectory)\wwwroot\dotnet.d.ts</_DotnetTypesDestPath>
</PropertyGroup>

<!-- Copy dotnet.d.ts if source exists. MSBuild will skip if files are identical. -->
<Copy SourceFiles="$(_DotnetTypesSourcePath)"
DestinationFiles="$(_DotnetTypesDestPath)"
Condition="Exists('$(_DotnetTypesSourcePath)')"
SkipUnchangedFiles="true" />
</Target>
</Project>
21 changes: 21 additions & 0 deletions src/mono/wasm/Wasm.Build.Tests/Templates/WasmTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,5 +317,26 @@ public async Task LibraryModeBuild(bool useWasmSdk)
}

}

[Theory]
[InlineData(Configuration.Debug)]
[InlineData(Configuration.Release)]
public void TypeScriptDefinitionsCopiedToWwwrootOnBuild(Configuration config)
{
string emitTypeScriptDts = "<WasmEmitTypeScriptDefinitions>true</WasmEmitTypeScriptDefinitions>";
ProjectInfo info = CreateWasmTemplateProject(Template.WasmBrowser, config, aot: false, "tsdefs", extraProperties: emitTypeScriptDts);

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 when WasmEmitTypeScriptDefinitions is used");

// Build to trigger the _EnsureDotnetTypeScriptDefinitions target on restore
BuildProject(info, config, new BuildOptions(WasmEmitTypeScriptDefinitions: true));

// Verify dotnet.d.ts is created in the project's wwwroot directory after build
Assert.True(File.Exists(dotnetDtsWwwrootPath), $"dotnet.d.ts should be created at {dotnetDtsWwwrootPath} after the build with WasmEmitTypeScriptDefinitions=true");
}
}
}
3 changes: 3 additions & 0 deletions src/mono/wasm/build/WasmApp.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -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 output.
Defaults to false.
Public items:
- @(WasmExtraFilesToDeploy) - Files to copy to $(WasmAppDir).
Expand Down Expand Up @@ -189,6 +191,7 @@
<WasmStripILAfterAOT Condition="'$(WasmStripILAfterAOT)' == ''">true</WasmStripILAfterAOT>
<WasmRuntimeAssetsLocation Condition="'$(WasmRuntimeAssetsLocation)' == ''">_framework</WasmRuntimeAssetsLocation>
<MetricsSupport Condition="'$(MetricsSupport)' == ''">false</MetricsSupport>
<WasmEmitTypeScriptDefinitions Condition="'$(WasmEmitTypeScriptDefinitions)' == ''">false</WasmEmitTypeScriptDefinitions>

<!-- semicolon is a msbuild property separator. It is also the path separator on windows.
So, we need to escape it here, so the paths don't get split up when converting
Expand Down