Skip to content
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

fix: Adjust static assets generation sequence #890

Merged
merged 9 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
56 changes: 43 additions & 13 deletions src/Uno.Wasm.Bootstrap/ShellTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public partial class ShellTask_v0 : Task
[Required]
public string WasmShellMode { get; set; } = "";

public ITaskItem[] ExistingStaticWebAsset { get; set; } = [];

public ITaskItem[] EmbeddedResources { get; set; } = [];

Expand Down Expand Up @@ -145,7 +146,7 @@ public partial class ShellTask_v0 : Task

public override bool Execute()
{
IntermediateOutputPath = TryConvertLongPath(IntermediateOutputPath);
IntermediateOutputPath = IntermediateOutputPath;
_intermediateAssetsPath = Path.Combine(IntermediateOutputPath, "unowwwrootassets");
Directory.CreateDirectory(_intermediateAssetsPath);

Expand All @@ -161,6 +162,7 @@ public override bool Execute()
GenerateIndexHtml();
GenerateEmbeddedJs();
GenerateConfig();
RemoveDuplicateAssets();
}
finally
{
Expand All @@ -170,6 +172,26 @@ public override bool Execute()
return true;
}

private void RemoveDuplicateAssets()
{
// Remove duplicate assets from the list to be exported.
// They might have been imported from the build pass.

var existingAssets = StaticWebContent
.Where(s => ExistingStaticWebAsset.Any(e => e.ItemSpec == s.ItemSpec || e.GetMetadata("FullPath") == s.GetMetadata("FullPath")))
.ToArray();

foreach (var existingAsset in existingAssets)
{
Log.LogMessage(MessageImportance.Low, $"Existing asset to remove [{existingAsset.ItemSpec}]");
}

// remove existingAssets from StaticWebContent
StaticWebContent = StaticWebContent
.Where(s => !existingAssets.Contains(s))
.ToArray();
}

private void GeneratedAOTProfile()
{
var useAotProfile = !GenerateAOTProfile && UseAotProfile;
Expand Down Expand Up @@ -209,25 +231,31 @@ private void Cleanup()

private (string fullPath, string relativePath) GetFilePaths(ITaskItem item)
{
// This is for project-local defined content
var baseSourceFile = item.GetMetadata("DefiningProjectDirectory");
if (item.GetMetadata("RelativePath") is { } relativePath && !string.IsNullOrEmpty(relativePath))
{
Log.LogMessage(MessageImportance.Low, $"RelativePath '{relativePath}' for full path '{item.GetMetadata("FullPath")}' (ItemSpec: {item.ItemSpec})");

if (item.GetMetadata("TargetPath") is { } targetPath && !string.IsNullOrEmpty(targetPath))
// This case is mainly for shared projects and files out of the baseSourceFile path
return (item.GetMetadata("FullPath"), relativePath);
}
else if (item.GetMetadata("TargetPath") is { } targetPath && !string.IsNullOrEmpty(targetPath))
{
var fullPath = Path.IsPathRooted(item.ItemSpec) ? item.ItemSpec : Path.Combine(baseSourceFile, item.ItemSpec);
Log.LogMessage(MessageImportance.Low, $"TargetPath '{targetPath}' for full path '{item.GetMetadata("FullPath")}' (ItemSpec: {item.ItemSpec})");

// This is used for item remapping
return (fullPath, targetPath);
return (item.GetMetadata("FullPath"), targetPath);
}
else if (item.GetMetadata("Link") is { } link && !string.IsNullOrEmpty(link))
{
var fullPath = Path.IsPathRooted(item.ItemSpec) ? item.ItemSpec : Path.Combine(baseSourceFile, item.ItemSpec);
Log.LogMessage(MessageImportance.Low, $"Link '{link}' for full path '{item.GetMetadata("FullPath")}' (ItemSpec: {item.ItemSpec})");

// This case is mainly for shared projects and files out of the baseSourceFile path
return (fullPath, link);
return (item.GetMetadata("FullPath"), link);
}
else if (item.GetMetadata("FullPath") is { } fullPath && File.Exists(fullPath))
{
Log.LogMessage(MessageImportance.Low, $"FullPath '{fullPath}' (ItemSpec: {item.ItemSpec})");

var sourceFilePath = item.ItemSpec;

if (sourceFilePath.StartsWith(CurrentProjectPath))
Expand All @@ -242,7 +270,9 @@ private void Cleanup()
}
else
{
return (Path.Combine(baseSourceFile, item.ItemSpec), item.ItemSpec);
Log.LogMessage(MessageImportance.Low, $"Without metadata '{item.GetMetadata("FullPath")}' (ItemSpec: {item.ItemSpec})");

return (item.GetMetadata("FullPath"), item.ItemSpec);
}
}

Expand Down Expand Up @@ -331,9 +361,9 @@ private void ExtractAdditionalJS()
{
var (fullSourcePath, relativePath) = GetFilePaths(projectResource);

if (relativePath.Contains("WasmScripts"))
if (fullSourcePath.Contains("WasmScripts"))
{
var scriptName = Path.GetFileName(relativePath);
var scriptName = Path.GetFileName(fullSourcePath);

Log.LogMessage($"Embedded resources JS {scriptName}");

Expand Down Expand Up @@ -368,9 +398,9 @@ private void ExtractAdditionalCSS()
{
var (fullSourcePath, relativePath) = GetFilePaths(projectResource);

if (relativePath.Contains("WasmCSS"))
if (fullSourcePath.Contains("WasmCSS"))
{
var cssName = Path.GetFileName(relativePath);
var cssName = Path.GetFileName(fullSourcePath);

Log.LogMessage($"Embedded CSS {cssName}");

Expand Down
30 changes: 26 additions & 4 deletions src/Uno.Wasm.Bootstrap/build/Uno.Wasm.Bootstrap.targets
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
</Target>

<Target Name="GenerateUnoWasmAssets"
BeforeTargets="ResolveProjectStaticWebAssets;_UnoWasmNativeForBuild"
BeforeTargets="ResolveStaticWebAssetsInputs;PrepareInputsForWasmBuild;ResolveJSModuleStaticWebAssets;_UnoWasmNativeForBuild"
DependsOnTargets="_UnoAdjustCompatibility">

<ItemGroup>
Expand All @@ -252,14 +252,14 @@
Include="@(_AllChildProjectItemsWithTargetPath)"
Condition=" '%(_AllChildProjectItemsWithTargetPath.CopyToOutputDirectory)' == 'PreserveNewest' OR '%(_AllChildProjectItemsWithTargetPath.CopyToOutputDirectory)' == 'Always' " />

<Content Remove="$(WasmPWAManifestFile)" />
<ContentWithTargetPath Remove="$(WasmPWAManifestFile)" />
</ItemGroup>

<ShellTask_v0
AotProfile="$(WasmAotProfilePath)"
Assembly="$(IntermediateOutputPath)$(TargetFileName)"
AssemblyName="$(AssemblyName)"
Assets="@(Content);@(_UnoWasmCopyToOutputAssets)"
Assets="@(ContentWithTargetPath);@(_UnoWasmCopyToOutputAssets)"
ContentExtensionsToExclude="$(WasmShellContentExtensionsToExclude)"
CSPConfiguration="$(WasmShellCSPConfiguration)"
CurrentProjectPath="$(MSBuildProjectDirectory)"
Expand All @@ -282,16 +282,38 @@
PWAManifestFile="$(WasmPWAManifestFile)"
ReferencePath="@(_UnoWasmBootstrapAssembliesForReferenceCopyLocalPaths)"
RunAOTCompilation="$(RunAOTCompilation)"
ExistingStaticWebAsset="@(StaticWebAsset)"
Optimize="$(Optimize)"
WasmBuildNative="$(WasmBuildNative)"
WasmShellMode="$(WasmShellMode)"
WebAppBasePath="$(WasmShellWebAppBasePath)"
>
<Output TaskParameter="StaticWebContent" ItemName="Content" />
<Output TaskParameter="StaticWebContent" ItemName="_UnoStaticWebContent" />
<Output TaskParameter="NativeFileReference" ItemName="NativeFileReference" />
<Output TaskParameter="FilteredAotProfile" PropertyName="_FilteredAotProfile" />
</ShellTask_v0>

<DefineStaticWebAssets
CandidateAssets="@(_UnoStaticWebContent)"
FingerprintCandidates="$(StaticWebAssetsFingerprintContent)"
FingerprintPatterns="@(StaticWebAssetFingerprintPattern)"
RelativePathPattern="wwwroot/**"
SourceType="Discovered"
SourceId="$(PackageId)"
ContentRoot="$(MSBuildProjectDirectory)\wwwroot\"
BasePath="$(StaticWebAssetBasePath)"
AssetMergeSource="$(StaticWebAssetMergeTarget)">
<Output TaskParameter="Assets" ItemName="StaticWebAsset" />
</DefineStaticWebAssets>

<DefineStaticWebAssetEndpoints
CandidateAssets="@(StaticWebAsset)"
ExistingEndpoints="@(StaticWebAssetEndpoint)"
ContentTypeMappings="@(StaticWebAssetContentTypeMapping)"
>
<Output TaskParameter="Endpoints" ItemName="StaticWebAssetEndpoint" />
</DefineStaticWebAssetEndpoints>

<PropertyGroup Condition=" '$(_FilteredAotProfile)' != '' ">
<!-- Override the user's profile with the filtered one -->
<WasmAotProfilePath>$(_FilteredAotProfile)</WasmAotProfilePath>
Expand Down
4 changes: 0 additions & 4 deletions src/Uno.Wasm.MixedModeSample/Uno.Wasm.MixedModeSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
<WasmShellMonoRuntimeExecutionMode Condition="$([MSBuild]::IsOsPlatform('Linux'))">InterpreterAndAOT</WasmShellMonoRuntimeExecutionMode>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="System.Collections.Immutable" Version="1.4.0" />
Expand Down
3 changes: 3 additions & 0 deletions src/Uno.Wasm.MixedModeSample/wwwroot/dummy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
8 changes: 4 additions & 4 deletions src/Uno.Wasm.Sample/Uno.Wasm.Sample.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
<!-- Add linked content first to validate directory creation from links -->
<Content Include="..\.editorconfig" Link="AdditionalContent\%(FileName)%(Extension)" />
<Content Include="../nuget.config" Link="AdditionalContent\%(FileName)%(Extension)" />
<Content Include="AdditionalContent\SomeContent01.txt">
<Content Include="$(MSBuildThisFileDirectory)AdditionalContent\SomeContent01.txt">
<UnoDeploy>Package</UnoDeploy>
</Content>
<Content Include="AdditionalContent/SomeContent02.txt" />
<Content Include="AdditionalContent/SomeContent03.txt" UnoDeploy="Root" />
<Content Include="AdditionalContent/SomeContent04.txt" UnoDeploy="None" />
<Content Include="$(MSBuildThisFileDirectory)AdditionalContent/SomeContent02.txt" />
<Content Include="$(MSBuildThisFileDirectory)AdditionalContent/SomeContent03.txt" UnoDeploy="Root" />
<Content Include="$(MSBuildThisFileDirectory)AdditionalContent/SomeContent04.txt" UnoDeploy="None" />
</ItemGroup>
</Project>
Loading