Skip to content

Commit ac7666f

Browse files
authored
[browser] Support WasmFilesToIncludeInFileSystem in WasmSdk (#120970)
1 parent 9ebda3d commit ac7666f

File tree

13 files changed

+146
-13
lines changed

13 files changed

+146
-13
lines changed

eng/testing/scenarios/BuildWasmAppsJobsList.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ Wasm.Build.Tests.PreloadingTests
4949
Wasm.Build.Tests.EnvVariablesTests
5050
Wasm.Build.Tests.HttpTests
5151
Wasm.Build.Tests.DiagnosticsTests
52+
Wasm.Build.Tests.FilesToIncludeInFileSystemTests

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ Copyright (c) .NET Foundation. All rights reserved.
246246
</PropertyGroup>
247247

248248
<ItemGroup>
249-
<_WasmConfigFileCandidates Include="@(StaticWebAsset)" Condition="'%(SourceType)' == 'Discovered'" />
249+
<_WasmDiscoveredFileCandidates Include="@(StaticWebAsset)" Condition="'%(SourceType)' == 'Discovered'" />
250250

251251
<!-- Remove dotnet.js/wasm from runtime pack, in favor of the relinked ones in @(WasmNativeAsset) -->
252252
<ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)" Condition="$(_WasmNativeAssetFileNames.Contains(';%(FileName)%(Extension);'))" />
@@ -319,14 +319,13 @@ Copyright (c) .NET Foundation. All rights reserved.
319319
</DefineStaticWebAssets>
320320

321321
<DefineStaticWebAssets
322-
CandidateAssets="@(_WasmConfigFileCandidates)"
322+
CandidateAssets="@(_WasmDiscoveredFileCandidates)"
323323
AssetTraitName="WasmResource"
324324
AssetTraitValue="settings"
325325
RelativePathFilter="appsettings*.json"
326326
>
327327
<Output TaskParameter="Assets" ItemName="_WasmJsConfigStaticWebAsset" />
328328
</DefineStaticWebAssets>
329-
330329
<DefineStaticWebAssetEndpoints
331330
CandidateAssets="@(_WasmJsConfigStaticWebAsset)"
332331
ExistingEndpoints="@(StaticWebAssetEndpoint)"
@@ -335,12 +334,42 @@ Copyright (c) .NET Foundation. All rights reserved.
335334
<Output TaskParameter="Endpoints" ItemName="_WasmJsConfigStaticWebAssetEndpoint" />
336335
</DefineStaticWebAssetEndpoints>
337336

337+
<ItemGroup>
338+
<WasmFilesToIncludeInFileSystem Condition="'%(WasmFilesToIncludeInFileSystem.TargetPath)' == ''">
339+
<TargetPath>/%(WasmFilesToIncludeInFileSystem.Identity)</TargetPath>
340+
</WasmFilesToIncludeInFileSystem>
341+
</ItemGroup>
342+
343+
<DefineStaticWebAssets
344+
CandidateAssets="@(_WasmDiscoveredFileCandidates)"
345+
AssetTraitName="WasmResource"
346+
AssetTraitValue="vfs:%(WasmFilesToIncludeInFileSystem.TargetPath)"
347+
RelativePathFilter="@(WasmFilesToIncludeInFileSystem)"
348+
Condition="'@(WasmFilesToIncludeInFileSystem)' != ''"
349+
>
350+
<Output TaskParameter="Assets" ItemName="_WasmFilesToIncludeInFileSystemStaticWebAsset" />
351+
</DefineStaticWebAssets>
352+
353+
<Error
354+
Text="Some of the 'WasmFilesToIncludeInFileSystem' where not found in StaticWebAssets. WasmFilesToIncludeInFileSystem: @(WasmFilesToIncludeInFileSystem), matched StaticWebAssets: @(_WasmFilesToIncludeInFileSystemStaticWebAsset)"
355+
Condition="'@(WasmFilesToIncludeInFileSystem->Count())' != '@(_WasmFilesToIncludeInFileSystemStaticWebAsset->Count())'" />
356+
357+
<DefineStaticWebAssetEndpoints
358+
CandidateAssets="@(_WasmFilesToIncludeInFileSystemStaticWebAsset)"
359+
ExistingEndpoints="@(StaticWebAssetEndpoint)"
360+
ContentTypeMappings="@(StaticWebAssetContentTypeMapping)"
361+
>
362+
<Output TaskParameter="Endpoints" ItemName="_WasmFilesToIncludeInFileSystemStaticWebAssetEndpoint" />
363+
</DefineStaticWebAssetEndpoints>
364+
338365
<ItemGroup>
339366
<!-- Update the boot config static web asset since we've given it a trait -->
340367
<StaticWebAsset Remove="@(_WasmJsConfigStaticWebAsset)" />
341368
<StaticWebAsset Include="@(_WasmJsConfigStaticWebAsset)" />
369+
<StaticWebAsset Remove="@(_WasmFilesToIncludeInFileSystemStaticWebAsset)" />
370+
<StaticWebAsset Include="@(_WasmFilesToIncludeInFileSystemStaticWebAsset)" />
342371
<StaticWebAssetEndpoint Include="@(_WasmJsConfigStaticWebAssetEndpoint)" />
343-
372+
<StaticWebAssetEndpoint Include="@(_WasmFilesToIncludeInFileSystemStaticWebAssetEndpoint)" />
344373
<ReferenceCopyLocalPaths Remove="@(_WasmBuildFilesToRemove)" />
345374
</ItemGroup>
346375

@@ -385,8 +414,8 @@ Copyright (c) .NET Foundation. All rights reserved.
385414
</DefineStaticWebAssetEndpoints>
386415

387416
<ResolveFingerprintedStaticWebAssetEndpointsForAssets
388-
CandidateEndpoints="@(WasmStaticWebAssetEndpoint);@(_WasmJsModuleCandidatesForBuildEndpoint)"
389-
CandidateAssets="@(WasmStaticWebAsset);@(_WasmJsModuleCandidatesForBuild)"
417+
CandidateEndpoints="@(WasmStaticWebAssetEndpoint);@(_WasmJsModuleCandidatesForBuildEndpoint);@(StaticWebAssetEndpoint)"
418+
CandidateAssets="@(WasmStaticWebAsset);@(_WasmJsModuleCandidatesForBuild);@(_WasmFilesToIncludeInFileSystemStaticWebAsset)"
390419
IsStandalone="$(StaticWebAssetStandaloneHosting)"
391420
>
392421
<Output TaskParameter="ResolvedEndpoints" ItemName="_WasmResolvedEndpoints" />
@@ -395,7 +424,7 @@ Copyright (c) .NET Foundation. All rights reserved.
395424
<GenerateWasmBootJson
396425
AssemblyPath="@(IntermediateAssembly)"
397426
ApplicationEnvironment="$(_WasmBuildApplicationEnvironmentName)"
398-
Resources="@(WasmStaticWebAsset);@(_WasmJsModuleCandidatesForBuild)"
427+
Resources="@(WasmStaticWebAsset);@(_WasmJsModuleCandidatesForBuild);@(_WasmFilesToIncludeInFileSystemStaticWebAsset)"
399428
Endpoints="@(_WasmResolvedEndpoints)"
400429
DebugBuild="true"
401430
DebugLevel="$(WasmDebugLevel)"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Collections.Specialized;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
using Xunit.Abstractions;
12+
13+
#nullable enable
14+
15+
namespace Wasm.Build.Tests;
16+
17+
public class FilesToIncludeInFileSystemTests : WasmTemplateTestsBase
18+
{
19+
public FilesToIncludeInFileSystemTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext)
20+
: base(output, buildContext)
21+
{
22+
}
23+
24+
public static IEnumerable<object?[]> LoadFilesToVfsData()
25+
{
26+
if (!EnvironmentVariables.UseJavascriptBundler)
27+
yield return new object?[] { false };
28+
29+
yield return new object?[] { true };
30+
}
31+
32+
[Theory, TestCategory("bundler-friendly")]
33+
[MemberData(nameof(LoadFilesToVfsData))]
34+
public async Task LoadFilesToVfs(bool publish)
35+
{
36+
Configuration config = Configuration.Debug;
37+
ProjectInfo info = CopyTestAsset(config, aot: false, TestAsset.WasmBasicTestApp, "FilesToIncludeInFileSystemTest");
38+
39+
if (publish)
40+
PublishProject(info, config, new PublishOptions());
41+
else
42+
BuildProject(info, config, new BuildOptions());
43+
44+
BrowserRunOptions runOptions = new(
45+
config,
46+
TestScenario: "FilesToIncludeInFileSystemTest"
47+
);
48+
RunResult result = publish
49+
? await RunForPublishWithWebServer(runOptions)
50+
: await RunForBuildWithDotnetRun(runOptions);
51+
52+
Assert.Contains(result.TestOutput, m => m.Contains("'/myfiles/Vfs1.txt' exists 'True' with content 'Vfs1.txt'"));
53+
Assert.Contains(result.TestOutput, m => m.Contains("'/myfiles/Vfs2.txt' exists 'True' with content 'Vfs2.txt'"));
54+
Assert.Contains(result.TestOutput, m => m.Contains("'/subdir/subsubdir/Vfs3.txt' exists 'True' with content 'Vfs3.txt'"));
55+
}
56+
}

src/mono/wasm/testassets/JavascriptBundlers/rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
}),
1919
files({
2020
output: 'public',
21-
extensions: /\.(json)$/,
21+
extensions: /\.(json|txt)$/,
2222
}),
2323
nodeResolve({
2424
extensions: ['.js']
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.IO;
6+
using System.Text.Json;
7+
using System.Runtime.InteropServices.JavaScript;
8+
9+
public partial class FilesToIncludeInFileSystemTest
10+
{
11+
[JSExport]
12+
public static void Run()
13+
{
14+
// Check file presence in VFS based on application environment
15+
PrintFileExistence("/myfiles/Vfs1.txt");
16+
PrintFileExistence("/myfiles/Vfs2.txt");
17+
PrintFileExistence("/subdir/subsubdir/Vfs3.txt");
18+
}
19+
20+
// Synchronize with FilesToIncludeInFileSystemTests
21+
private static void PrintFileExistence(string path) => TestOutput.WriteLine($"'{path}' exists '{File.Exists(path)}' with content '{File.ReadAllText(path)}'");
22+
}

src/mono/wasm/testassets/WasmBasicTestApp/App/WasmBasicTestApp.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@
2929
<BlazorWebAssemblyLazyLoad Include="LazyLibrary$(WasmAssemblyExtension)" />
3030
<WasmExtraFilesToDeploy Include="profiler.js" />
3131
</ItemGroup>
32+
33+
<ItemGroup>
34+
<WasmFilesToIncludeInFileSystem Include="Vfs1.txt" TargetPath="/myfiles/Vfs1.txt" />
35+
<WasmFilesToIncludeInFileSystem Include="subdir/Vfs2.txt" TargetPath="/myfiles/Vfs2.txt" />
36+
<WasmFilesToIncludeInFileSystem Include="subdir/subsubdir/Vfs3.txt" />
37+
</ItemGroup>
3238
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Vfs1.txt

src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ try {
231231
exports.AppSettingsTest.Run();
232232
exit(0);
233233
break;
234+
case "FilesToIncludeInFileSystemTest":
235+
exports.FilesToIncludeInFileSystemTest.Run();
236+
exit(0);
237+
break;
234238
case "DownloadResourceProgressTest":
235239
exit(0);
236240
break;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Vfs2.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Vfs3.txt

0 commit comments

Comments
 (0)