-
Couldn't load subscription status.
- Fork 5.2k
[browser] Support WasmFilesToIncludeInFileSystem in WasmSdk #120970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7b9e236
89a33e5
747d9ae
261e367
09d3586
d935c34
a04cf3f
2cca4eb
2145d20
6d8cd3e
239fc96
6e4432b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.Specialized; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
| using Xunit.Abstractions; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Wasm.Build.Tests; | ||
|
|
||
| public class FilesToIncludeInFileSystemTests : WasmTemplateTestsBase | ||
| { | ||
| public FilesToIncludeInFileSystemTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext) | ||
| : base(output, buildContext) | ||
| { | ||
| } | ||
|
|
||
| public static IEnumerable<object?[]> LoadFilesToVfsData() | ||
| { | ||
| if (!EnvironmentVariables.UseJavascriptBundler) | ||
| yield return new object?[] { false }; | ||
|
|
||
| yield return new object?[] { true }; | ||
| } | ||
|
|
||
| [Theory, TestCategory("bundler-friendly")] | ||
| [MemberData(nameof(LoadFilesToVfsData))] | ||
| public async Task LoadFilesToVfs(bool publish) | ||
| { | ||
| Configuration config = Configuration.Debug; | ||
| ProjectInfo info = CopyTestAsset(config, aot: false, TestAsset.WasmBasicTestApp, "FilesToIncludeInFileSystemTest"); | ||
|
|
||
| if (publish) | ||
| PublishProject(info, config, new PublishOptions()); | ||
| else | ||
| BuildProject(info, config, new BuildOptions()); | ||
|
|
||
| BrowserRunOptions runOptions = new( | ||
| config, | ||
| TestScenario: "FilesToIncludeInFileSystemTest" | ||
| ); | ||
| RunResult result = publish | ||
| ? await RunForPublishWithWebServer(runOptions) | ||
| : await RunForBuildWithDotnetRun(runOptions); | ||
|
|
||
| Assert.Contains(result.TestOutput, m => m.Contains("'/myfiles/Vfs1.txt' exists 'True' with content 'Vfs1.txt'")); | ||
| Assert.Contains(result.TestOutput, m => m.Contains("'/myfiles/Vfs2.txt' exists 'True' with content 'Vfs2.txt'")); | ||
| Assert.Contains(result.TestOutput, m => m.Contains("'/subdir/subsubdir/Vfs3.txt' exists 'True' with content 'Vfs3.txt'")); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Text.Json; | ||
| using System.Runtime.InteropServices.JavaScript; | ||
|
|
||
| public partial class FilesToIncludeInFileSystemTest | ||
| { | ||
| [JSExport] | ||
| public static void Run() | ||
| { | ||
| // Check file presence in VFS based on application environment | ||
| PrintFileExistence("/myfiles/Vfs1.txt"); | ||
| PrintFileExistence("/myfiles/Vfs2.txt"); | ||
| PrintFileExistence("/subdir/subsubdir/Vfs3.txt"); | ||
| } | ||
|
|
||
| // Synchronize with FilesToIncludeInFileSystemTests | ||
| private static void PrintFileExistence(string path) => TestOutput.WriteLine($"'{path}' exists '{File.Exists(path)}' with content '{File.ReadAllText(path)}'"); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,4 +29,10 @@ | |
| <BlazorWebAssemblyLazyLoad Include="LazyLibrary$(WasmAssemblyExtension)" /> | ||
| <WasmExtraFilesToDeploy Include="profiler.js" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <WasmFilesToIncludeInFileSystem Include="Vfs1.txt" TargetPath="/myfiles/Vfs1.txt" /> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would happen if there is reference to non-existent file ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would get ignored. I could make it fail the build There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That may be better |
||
| <WasmFilesToIncludeInFileSystem Include="subdir/Vfs2.txt" TargetPath="/myfiles/Vfs2.txt" /> | ||
| <WasmFilesToIncludeInFileSystem Include="subdir/subsubdir/Vfs3.txt" /> | ||
| </ItemGroup> | ||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Vfs1.txt |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Vfs2.txt |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Vfs3.txt |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -321,14 +321,14 @@ public string TransformResourcesToAssets(BootJsonData config, bool bundlerFriend | |||||
| var asset = new VfsAsset() | ||||||
| { | ||||||
| virtualPath = a.Key, | ||||||
| name = a.Value.Keys.First(), | ||||||
| name = $"../{a.Value.Keys.First()}", | ||||||
maraf marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| integrity = a.Value.Values.First() | ||||||
| }; | ||||||
|
|
||||||
| if (bundlerFriendly) | ||||||
| { | ||||||
| string escaped = EscapeName(string.Concat(asset.name)); | ||||||
| imports.Add($"import * as {escaped} from \"./{asset.name}\";"); | ||||||
| imports.Add($"import {escaped} from \"./{asset.name}\";"); | ||||||
|
||||||
| imports.Add($"import {escaped} from \"./{asset.name}\";"); | |
| imports.Add($"import * as {escaped} from \"./{asset.name}\";"); |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -214,7 +214,8 @@ private void WriteBootConfig(string entryAssemblyName) | |||||
| var assetTraitName = resource.GetMetadata("AssetTraitName"); | ||||||
| var assetTraitValue = resource.GetMetadata("AssetTraitValue"); | ||||||
| var resourceName = Path.GetFileName(resource.GetMetadata("OriginalItemSpec")); | ||||||
| var resourceRoute = Path.GetFileName(endpointByAsset[resource.ItemSpec].ItemSpec); | ||||||
| var resourceEndpoint = endpointByAsset[resource.ItemSpec].ItemSpec; | ||||||
| var resourceRoute = Path.GetFileName(resourceEndpoint); | ||||||
|
|
||||||
| if (TryGetLazyLoadedAssembly(lazyLoadAssembliesWithoutExtension, resourceName, out var lazyLoad)) | ||||||
| { | ||||||
|
|
@@ -354,6 +355,16 @@ private void WriteBootConfig(string entryAssemblyName) | |||||
| AddResourceToList(resource, resourceList, targetPath); | ||||||
| continue; | ||||||
| } | ||||||
| else if (string.Equals("WasmResource", assetTraitName, StringComparison.OrdinalIgnoreCase) && assetTraitValue.StartsWith("vfs:", StringComparison.OrdinalIgnoreCase)) | ||||||
| { | ||||||
| Log.LogMessage(MessageImportance.Low, "Candidate '{0}' is defined as VFS resource '{1}'.", resource.ItemSpec, assetTraitValue); | ||||||
|
|
||||||
| var targetPath = assetTraitValue.Substring("vfs:".Length); | ||||||
|
||||||
| var targetPath = assetTraitValue.Substring("vfs:".Length); | |
| var targetPath = assetTraitValue["vfs:".Length..]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The TargetPath should be prefixed with '/' to ensure it's an absolute path in the VFS. According to the PR description, files should be loaded to paths like '/file.txt' and '/subdir/file.txt', but this sets TargetPath to 'file.txt' or 'subdir/file.txt' without the leading slash.