-
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?
Conversation
src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs
Show resolved
Hide resolved
VFS imports were incorrectly generated as "es module" imports, where as they should be treated as "general URL" imports
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <WasmFilesToIncludeInFileSystem Include="Vfs1.txt" TargetPath="/myfiles/Vfs1.txt" /> |
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.
What would happen if there is reference to non-existent file ?
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.
It would get ignored. I could make it fail the build
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.
That may be better
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.
Pull Request Overview
This PR implements support for automatically loading files into the WebAssembly Virtual File System (VFS) using the WasmFilesToIncludeInFileSystem item in the WasmSdk. Files specified with this item are loaded into VFS at application startup, with support for custom target paths via TargetPath metadata.
Key Changes:
- Added VFS resource support in boot JSON generation to handle files marked with
vfs:trait - Updated bundler-friendly mode to correctly reference VFS assets with relative paths
- Implemented target path resolution with fallback to file's wwwroot relative path
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
src/tasks/WasmAppBuilder/WasmAppBuilder.cs |
Updated virtual path prefix to respect RuntimeAssetsLocation |
src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/GenerateWasmBootJson.cs |
Added VFS resource handling in boot config generation |
src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/BootJsonBuilderHelper.cs |
Fixed VFS asset path generation for bundler-friendly mode |
src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/subdir/subsubdir/Vfs3.txt |
Test file for nested VFS path without custom TargetPath |
src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/subdir/Vfs2.txt |
Test file for VFS with custom TargetPath |
src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js |
Added test scenario entry point |
src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/Vfs1.txt |
Test file for root-level VFS with custom TargetPath |
src/mono/wasm/testassets/WasmBasicTestApp/App/WasmBasicTestApp.csproj |
Added WasmFilesToIncludeInFileSystem declarations for testing |
src/mono/wasm/testassets/WasmBasicTestApp/App/FilesToIncludeInFileSystemTest.cs |
Test code to verify VFS file existence |
src/mono/wasm/testassets/JavascriptBundlers/rollup.config.mjs |
Updated rollup to handle .txt files |
src/mono/wasm/Wasm.Build.Tests/FilesToIncludeInFileSystemTests.cs |
Integration test for VFS functionality |
src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets |
MSBuild integration for WasmFilesToIncludeInFileSystem |
eng/testing/scenarios/BuildWasmAppsJobsList.txt |
Registered new test class for CI |
| { | ||
| Log.LogMessage(MessageImportance.Low, "Candidate '{0}' is defined as VFS resource '{1}'.", resource.ItemSpec, assetTraitValue); | ||
|
|
||
| var targetPath = assetTraitValue.Substring("vfs:".Length); |
Copilot
AI
Oct 27, 2025
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.
Use string slice operator assetTraitValue[\"vfs:\".Length..] instead of Substring for better performance and modern C# style.
| var targetPath = assetTraitValue.Substring("vfs:".Length); | |
| var targetPath = assetTraitValue["vfs:".Length..]; |
| { | ||
| string escaped = EscapeName(string.Concat(asset.name)); | ||
| imports.Add($"import * as {escaped} from \"./{asset.name}\";"); | ||
| imports.Add($"import {escaped} from \"./{asset.name}\";"); |
Copilot
AI
Oct 27, 2025
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.
Removing the * as from the import statement changes the import from a namespace import to a default import. This breaks compatibility for modules that export a namespace rather than a default export. If VFS assets are expected to be modules with namespace exports, this change will cause runtime errors.
| imports.Add($"import {escaped} from \"./{asset.name}\";"); | |
| imports.Add($"import * as {escaped} from \"./{asset.name}\";"); |
|
|
||
| <ItemGroup> | ||
| <WasmFilesToIncludeInFileSystem Condition="'%(WasmFilesToIncludeInFileSystem.TargetPath)' == ''"> | ||
| <TargetPath>%(WasmFilesToIncludeInFileSystem.Identity)</TargetPath> |
Copilot
AI
Oct 27, 2025
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.
| <TargetPath>%(WasmFilesToIncludeInFileSystem.Identity)</TargetPath> | |
| <TargetPath>/%(WasmFilesToIncludeInFileSystem.Identity)</TargetPath> |
Use
WasmFilesToIncludeInFileSystemitems to automatically load files to VFS when the application is starting.The identity of
WasmFilesToIncludeInFileSystemneeds to be a relative path to files a file inwwwroot(either physical or linked). The optionalTargetPathmetadata allows to modify file location in VFS. If the metadata is omitted, a default location will resolved from relative file path inwwwroot.Example:
wwwroot/file.txtfile<WasmFilesToIncludeInFileSystem Include="file.txt" />will load the file to/file.txt<WasmFilesToIncludeInFileSystem Include="file.txt" TargetPath="/mydata/file.txt" />will load the file to/mydata/file.txtwwwroot/subdir/file.txtfile<WasmFilesToIncludeInFileSystem Include="subdir/file.txt" />will load the file to/subdir/file.txt<WasmFilesToIncludeInFileSystem Include="subdir/file.txt" TargetPath="/mydata/file.txt" />will load the file to/mydata/file.txtFixes #99223