Skip to content
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
1 change: 1 addition & 0 deletions eng/testing/scenarios/BuildWasmAppsJobsList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ Wasm.Build.Tests.WasmTemplateTests
Wasm.Build.Tests.WorkloadTests
Wasm.Build.Tests.MT.Blazor.SimpleMultiThreadedTests
Wasm.Build.Tests.DebugLevelTests
Wasm.Build.Tests.PreloadingTests
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ Copyright (c) .NET Foundation. All rights reserved.
<_WasmBootConfigFileName Condition="'$(_WasmBootConfigFileName)' == '' and '$(_TargetingNET100OrLater)' == 'true'">dotnet.boot.js</_WasmBootConfigFileName>
<_WasmBootConfigFileName Condition="'$(_WasmBootConfigFileName)' == ''">blazor.boot.json</_WasmBootConfigFileName>
<_WasmPublishBootConfigFileName>publish.$(_WasmBootConfigFileName)</_WasmPublishBootConfigFileName>
<_WasmPreloadAssets>$(WasmPreloadAssets)</_WasmPreloadAssets>
<_WasmPreloadAssets Condition="'$(_WasmPreloadAssets)' == ''">true</_WasmPreloadAssets>

<!-- Workaround for https://github.com/dotnet/sdk/issues/12114-->
<PublishDir Condition="'$(AppendRuntimeIdentifierToOutputPath)' != 'true' AND '$(PublishDir)' == '$(OutputPath)$(RuntimeIdentifier)\$(PublishDirName)\'">$(OutputPath)$(PublishDirName)\</PublishDir>
Expand Down Expand Up @@ -445,7 +447,107 @@ Copyright (c) .NET Foundation. All rights reserved.
>
<Output TaskParameter="Endpoints" ItemName="_WasmBuildBootConfigStaticWebAssetEndpoint" />
</DefineStaticWebAssetEndpoints>
</Target>

<PropertyGroup>
<GenerateHtmlAssetPlaceholdersBuildStaticWebAssetsDependsOn>
_AddWasmPreloadBuildProperties;
$(GenerateHtmlAssetPlaceholdersBuildStaticWebAssetsDependsOn)
</GenerateHtmlAssetPlaceholdersBuildStaticWebAssetsDependsOn>
<GenerateHtmlAssetPlaceholdersPublishStaticWebAssetsDependsOn>
_AddWasmPreloadPublishProperties;
$(GenerateHtmlAssetPlaceholdersPublishStaticWebAssetsDependsOn)
</GenerateHtmlAssetPlaceholdersPublishStaticWebAssetsDependsOn>
</PropertyGroup>

<Target Name="_WasmConfigurePreload">
<ItemGroup>
<AppendPreloadRelPreloadProperty Include="Append">
<UpdateTarget>Property</UpdateTarget>
<Name>PreloadRel</Name>
<Value>preload</Value>
</AppendPreloadRelPreloadProperty>
<AppendPreloadAsScriptProperty Include="Append">
<UpdateTarget>Property</UpdateTarget>
<Name>PreloadAs</Name>
<Value>script</Value>
</AppendPreloadAsScriptProperty>
<AppendPreloadPriorityHighProperty Include="Append">
<UpdateTarget>Property</UpdateTarget>
<Name>PreloadPriority</Name>
<Value>high</Value>
</AppendPreloadPriorityHighProperty>
<AppendPreloadCrossoriginAnonymousProperty Include="Append">
<UpdateTarget>Property</UpdateTarget>
<Name>PreloadCrossorigin</Name>
<Value>anonymous</Value>
</AppendPreloadCrossoriginAnonymousProperty>
<AppendPreloadOrder1Property Include="Append">
<UpdateTarget>Property</UpdateTarget>
<Name>PreloadOrder</Name>
<Value>1</Value>
</AppendPreloadOrder1Property>
<AppendGroupWebAssemblyProperty Include="Append">
<UpdateTarget>Property</UpdateTarget>
<Name>PreloadGroup</Name>
<Value>webassembly</Value>
</AppendGroupWebAssemblyProperty>

<!-- Label=Include matches endpoints with fingerprint; Label=Exclude matches endpoints without fingerprint -->
<_WasmPreloadEndpointFilter Include="Property" Name="Label" Mode="Include" Condition="'$(_WasmFingerprintAssets)' == 'true' and ('$(StaticWebAssetProjectMode)' == 'Default' or '$(_WasmFingerprintDotnetJs)' == 'true')" />
<_WasmPreloadEndpointFilter Include="Property" Name="Label" Mode="Exclude" Condition="'@(_WasmPreloadEndpointFilter)' == ''" />
</ItemGroup>
</Target>

<Target Name="_AddWasmPreloadBuildProperties" DependsOnTargets="_WasmConfigurePreload;_AddWasmStaticWebAssets" BeforeTargets="GenerateStaticWebAssetsManifest" Condition="'$(_WasmPreloadAssets)' == 'true'">
<ItemGroup>
<_WasmPreloadBuildScriptAsset Include="@(StaticWebAsset)" Condition="'%(AssetKind)' != 'Publish' and '%(FileName)%(Extension)' == '$(_WasmBootConfigFileName)'" />
</ItemGroup>

<FilterStaticWebAssetEndpoints
Endpoints="@(StaticWebAssetEndpoint)"
Assets="@(_WasmPreloadBuildScriptAsset)"
Filters="@(_WasmPreloadEndpointFilter)"
>
<Output TaskParameter="FilteredEndpoints" ItemName="_WasmPreloadBuildScriptAssetEndpoint" />
</FilterStaticWebAssetEndpoints>

<UpdateStaticWebAssetEndpoints
EndpointsToUpdate="@(_WasmPreloadBuildScriptAssetEndpoint)"
AllEndpoints="@(_WasmPreloadBuildScriptAssetEndpoint)"
Operations="@(AppendPreloadRelPreloadProperty);@(AppendPreloadAsScriptProperty);@(AppendPreloadPriorityHighProperty);@(AppendPreloadCrossoriginAnonymousProperty);@(AppendPreloadOrder1Property);@(AppendGroupWebAssemblyProperty)"
>
<Output TaskParameter="UpdatedEndpoints" ItemName="_UpdatedWasmPreloadBuildAssetEndpoint" />
</UpdateStaticWebAssetEndpoints>
<ItemGroup>
<StaticWebAssetEndpoint Remove="@(_WasmPreloadBuildScriptAssetEndpoint)" />
<StaticWebAssetEndpoint Include="@(_UpdatedWasmPreloadBuildAssetEndpoint)" />
</ItemGroup>
</Target>
<Target Name="_AddWasmPreloadPublishProperties" DependsOnTargets="_WasmConfigurePreload;_AddPublishWasmBootJsonToStaticWebAssets" BeforeTargets="GenerateStaticWebAssetsPublishManifest" Condition="'$(_WasmPreloadAssets)' == 'true'">
<ItemGroup>
<_WasmPreloadPublishScriptAsset Include="@(StaticWebAsset)" Condition="'%(AssetKind)' != 'Build' and '%(FileName)%(Extension)' == '$(_WasmPublishBootConfigFileName)'" />
</ItemGroup>

<FilterStaticWebAssetEndpoints
Endpoints="@(StaticWebAssetEndpoint)"
Assets="@(_WasmPreloadPublishScriptAsset)"
Filters="@(_WasmPreloadEndpointFilter)"
>
<Output TaskParameter="FilteredEndpoints" ItemName="_WasmPreloadPublishScriptAssetEndpoint" />
</FilterStaticWebAssetEndpoints>

<UpdateStaticWebAssetEndpoints
EndpointsToUpdate="@(_WasmPreloadPublishScriptAssetEndpoint)"
AllEndpoints="@(_WasmPreloadPublishScriptAssetEndpoint)"
Operations="@(AppendPreloadRelPreloadProperty);@(AppendPreloadAsScriptProperty);@(AppendPreloadPriorityHighProperty);@(AppendPreloadCrossoriginAnonymousProperty);@(AppendPreloadOrder1Property);@(AppendGroupWebAssemblyProperty)"
>
<Output TaskParameter="UpdatedEndpoints" ItemName="_UpdatedWasmPreloadPublishAssetEndpoint" />
</UpdateStaticWebAssetEndpoints>
<ItemGroup>
<StaticWebAssetEndpoint Remove="@(_WasmPreloadPublishScriptAssetEndpoint)" />
<StaticWebAssetEndpoint Include="@(_UpdatedWasmPreloadPublishAssetEndpoint)" />
</ItemGroup>
</Target>

<Target Name="ProvideDefaultUserRuntimeConfig" BeforeTargets="_GenerateRuntimeConfigurationFilesInputCache" Condition="'$(GenerateRuntimeConfigurationFiles)' == 'true'">
Expand Down
67 changes: 67 additions & 0 deletions src/mono/wasm/Wasm.Build.Tests/PreloadingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

#nullable enable

namespace Wasm.Build.Tests;

public class PreloadingTests : WasmTemplateTestsBase
{
public PreloadingTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext)
: base(output, buildContext)
{
}

[Theory]
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public void PreloadAssets(bool isPublish, bool fingerprintAssets)
{
Configuration config = Configuration.Debug;
ProjectInfo info = CopyTestAsset(config, aot: false, TestAsset.WasmBasicTestApp, "PreloadAssets");

// TODO: AssertAppBundle doesn't with fingerprinted dotnet.js at the moment
string extraMSBuildArgs = $"-p:OverrideHtmlAssetPlaceholders=true -p:WasmFingerprintAssets={fingerprintAssets}";
if (isPublish)
PublishProject(info, config, new PublishOptions(ExtraMSBuildArgs: extraMSBuildArgs, AssertAppBundle: false));
else
BuildProject(info, config, new BuildOptions(ExtraMSBuildArgs: extraMSBuildArgs, AssertAppBundle: false));

string? indexHtmlPath = null;
if (isPublish)
{
indexHtmlPath = Path.Combine(
GetBinFrameworkDir(config, forPublish: isPublish),
"..",
"index.html"
);
}
else
{
string objDir = Path.Combine(GetObjDir(config), "staticwebassets", "htmlassetplaceholders", "build");
indexHtmlPath = Directory.EnumerateFiles(objDir, "*.html").SingleOrDefault();
}

Assert.True(File.Exists(indexHtmlPath));
string indexHtmlContent = File.ReadAllText(indexHtmlPath);

if (fingerprintAssets)
{
// Expect to find fingerprinted preload
Assert.Contains("<link href=\"_framework/dotnet", indexHtmlContent);
Assert.DoesNotContain("<link href=\"_framework/dotnet.js\"", indexHtmlContent);
}
else
{
// Expect to find non-fingerprinted preload
Assert.Contains("<link href=\"_framework/dotnet.js\"", indexHtmlContent);
}
}
}
6 changes: 6 additions & 0 deletions src/mono/wasm/Wasm.Build.Tests/ProjectProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,12 @@ public virtual string GetBinFrameworkDir(Configuration config, bool forPublish,
throw new NotImplementedException();
}

public string GetObjDir(Configuration config, string framework, string? projectDir = null)
{
EnsureProjectDirIsSet();
return Path.Combine(projectDir ?? ProjectDir!, "obj", config.ToString(), framework);
}

[MemberNotNull(nameof(ProjectDir))]
protected void EnsureProjectDirIsSet()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ void OnErrorMessage(string msg)
public string GetBinFrameworkDir(Configuration config, bool forPublish, string? framework = null, string? projectDir = null) =>
_provider.GetBinFrameworkDir(config, forPublish, framework ?? DefaultTargetFramework, projectDir);

public string GetObjDir(Configuration config, string? framework = null, string? projectDir = null) =>
_provider.GetObjDir(config, framework ?? DefaultTargetFramework, projectDir);

public BuildPaths GetBuildPaths(Configuration config, bool forPublish) =>
_provider.GetBuildPaths(config, forPublish);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<title>WasmLazyLoading</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preload" id="webassembly" />
<script type='module' src="./main.js"></script>
<script type='module' src="./profiler.js"></script>
</head>
Expand Down
Loading