Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -98,6 +98,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<!-- Static Web Assets -->
<ResolveStaticWebAssetsInputsDependsOn>
$(ResolveStaticWebAssetsInputsDependsOn);
_EnsureDotnetTypeScriptDefinitions;
_AddWasmStaticWebAssets;
</ResolveStaticWebAssetsInputsDependsOn>
<ResolvePublishStaticWebAssetsDependsOn Condition="'$(WasmBuildingForNestedPublish)' != 'true'">
Expand Down Expand Up @@ -856,4 +857,21 @@ 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 -->
<Target Name="_EnsureDotnetTypeScriptDefinitions">
<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>
22 changes: 22 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,27 @@ public async Task LibraryModeBuild(bool useWasmSdk)
}

}

[Theory]
[InlineData(Configuration.Debug)]
[InlineData(Configuration.Release)]
public void TypeScriptDefinitionsCopiedToWwwroot(Configuration config)
{
ProjectInfo info = CreateWasmTemplateProject(Template.WasmBrowser, config, aot: false, "tsdefs");

string projectDirectory = Path.GetDirectoryName(info.ProjectFilePath)!;
string dotnetDtsPath = Path.Combine(projectDirectory, "wwwroot", "dotnet.d.ts");

Assert.True(File.Exists(dotnetDtsPath), $"dotnet.d.ts should exist at {dotnetDtsPath} after creation");

// Remove the file to test that MSBuild target recreates it
File.Delete(dotnetDtsPath);
Assert.False(File.Exists(dotnetDtsPath), $"dotnet.d.ts should be deleted before the build");

// Build to trigger the _CopyDotnetTypeScriptDefinitions target
BuildProject(info, config, new BuildOptions());

Assert.True(File.Exists(dotnetDtsPath), $"dotnet.d.ts should be recreated at {dotnetDtsPath} after the build");
}
}
}
Loading
Loading