I have a WinForms app using Blazor Hybrid, which generates a wwwroot directory containing static files (JS, CSS, fonts). When publishing the app as a single-file executable, I want to hide the wwwroot directory and embed its content as resources. I plan to use EmbeddedFileProvider to consume these resources at runtime.
Here’s my current approach, but it doesn’t seem to embed the files:
<PropertyGroup>
<EmbedWwwroot>true</EmbedWwwroot>
<PublishWwwroot>$(PublishDir)wwwroot</PublishWwwroot>
</PropertyGroup>
<Target Name="EmbedPublishedWwwroot" AfterTargets="Publish" Condition="$(EmbedWwwroot) == 'true'">
<ItemGroup>
<PublishedWwwrootFiles Include="$(PublishWwwroot)\**\*.*" />
</ItemGroup>
<Message Text="Published wwwroot path: $(PublishWwwroot)" Importance="high" />
<ItemGroup>
<EmbeddedResource Include="@(PublishedWwwrootFiles)">
<LogicalName>wwwroot.$([MSBuild]::ValueOrDefault('%(RecursiveDir)', '').Replace('\', '.'))%(Filename)%(Extension)</LogicalName>
<Visible>false</Visible>
<Type>Non-Resx</Type>
</EmbeddedResource>
</ItemGroup>
<Message Text="Embedding wwwroot content: %(EmbeddedResource.Identity) - %(EmbeddedResource.LogicalName)" Importance="high" />
</Target>
<Target Name="RemoveWwwrootFromPublish" AfterTargets="Publish" Condition="$(EmbedWwwroot) == 'true'">
<ItemGroup>
<RemoveDir Directories="$(PublishWwwroot)" Condition="Exists($(PublishWwwroot))" />
</ItemGroup>
</Target>
The wwwroot files are not being embedded as expected. I suspect this is because the embedding logic runs in the AfterTargets="Publish" phase, but the wwwroot content is only available during publish targets, and embedding typically needs to happen during the core compile phase.
How can I properly embed the wwwroot content as resources during the correct build phase? Additionally, how can I use EmbeddedFileProvider to load these resources at runtime in a Blazor Hybrid app? Any guidance or corrections would be greatly appreciated!