@@ -46,17 +46,18 @@ public void Build_FingerprintsContent_WhenEnabled()
46
46
AssertBuildAssets ( manifest1 , outputPath , intermediateOutputPath ) ;
47
47
}
48
48
49
- public static TheoryData < string , bool > WriteImportMapToHtmlData => new TheoryData < string , bool >
49
+ public static TheoryData < string , string , string > WriteImportMapToHtmlData => new TheoryData < string , string , string >
50
50
{
51
- { "VanillaWasm" , true } ,
52
- { "BlazorWasmMinimal" , false }
51
+ { "VanillaWasm" , "main.js" , null } ,
52
+ { "BlazorWasmMinimal" , "_framework/blazor.webassembly.js" , "_framework/blazor.webassembly#[.{fingerprint}].js" }
53
53
} ;
54
54
55
55
[ Theory ]
56
56
[ MemberData ( nameof ( WriteImportMapToHtmlData ) ) ]
57
- public void Build_WriteImportMapToHtml ( string testAsset , bool assetMainJs )
57
+ public void Build_WriteImportMapToHtml ( string testAsset , string scriptPath , string scriptPathWithFingerprintPattern )
58
58
{
59
59
ProjectDirectory = CreateAspNetSdkTestAsset ( testAsset ) ;
60
+ ReplaceStringInIndexHtml ( ProjectDirectory , scriptPath , scriptPathWithFingerprintPattern ) ;
60
61
61
62
var build = CreateBuildCommand ( ProjectDirectory ) ;
62
63
ExecuteCommand ( build , "-p:WriteImportMapToHtml=true" ) . Should ( ) . Pass ( ) ;
@@ -65,44 +66,56 @@ public void Build_WriteImportMapToHtml(string testAsset, bool assetMainJs)
65
66
var indexHtmlPath = Directory . EnumerateFiles ( Path . Combine ( intermediateOutputPath , "staticwebassets" , "importmaphtml" , "build" ) , "*.html" ) . Single ( ) ;
66
67
var endpointsManifestPath = Path . Combine ( intermediateOutputPath , $ "staticwebassets.build.endpoints.json") ;
67
68
68
- AssertImportMapInHtml ( indexHtmlPath , endpointsManifestPath , assetMainJs ) ;
69
+ AssertImportMapInHtml ( indexHtmlPath , endpointsManifestPath , scriptPath ) ;
69
70
}
70
71
71
72
[ Theory ]
72
73
[ MemberData ( nameof ( WriteImportMapToHtmlData ) ) ]
73
- public void Publish_WriteImportMapToHtml ( string testAsset , bool assetMainJs )
74
+ public void Publish_WriteImportMapToHtml ( string testAsset , string scriptPath , string scriptPathWithFingerprintPattern )
74
75
{
75
76
ProjectDirectory = CreateAspNetSdkTestAsset ( testAsset ) ;
77
+ ReplaceStringInIndexHtml ( ProjectDirectory , scriptPath , scriptPathWithFingerprintPattern ) ;
76
78
77
79
var projectName = Path . GetFileNameWithoutExtension ( Directory . EnumerateFiles ( ProjectDirectory . TestRoot , "*.csproj" ) . Single ( ) ) ;
78
80
79
81
var publish = CreatePublishCommand ( ProjectDirectory ) ;
80
82
ExecuteCommand ( publish , "-p:WriteImportMapToHtml=true" ) . Should ( ) . Pass ( ) ;
81
83
82
84
var outputPath = publish . GetOutputDirectory ( DefaultTfm , "Debug" ) . ToString ( ) ;
83
- var indexHtmlPath = Path . Combine ( outputPath , "wwwroot" , "index.html" ) ;
85
+ var indexHtmlOutputPath = Path . Combine ( outputPath , "wwwroot" , "index.html" ) ;
84
86
var endpointsManifestPath = Path . Combine ( outputPath , $ "{ projectName } .staticwebassets.endpoints.json") ;
85
87
86
- AssertImportMapInHtml ( indexHtmlPath , endpointsManifestPath , assetMainJs ) ;
88
+ AssertImportMapInHtml ( indexHtmlOutputPath , endpointsManifestPath , scriptPath ) ;
87
89
}
88
90
89
- private void AssertImportMapInHtml ( string indexHtmlPath , string endpointsManifestPath , bool assetMainJs )
91
+ private void ReplaceStringInIndexHtml ( TestAsset testAsset , string scriptPath , string scriptPathWithFingerprintPattern )
92
+ {
93
+ if ( scriptPathWithFingerprintPattern != null )
94
+ {
95
+ var indexHtmlPath = Path . Combine ( testAsset . TestRoot , "wwwroot" , "index.html" ) ;
96
+ var indexHtmlContent = File . ReadAllText ( indexHtmlPath ) ;
97
+ var newIndexHtmlContent = indexHtmlContent . Replace ( scriptPath , scriptPathWithFingerprintPattern ) ;
98
+ if ( indexHtmlContent == newIndexHtmlContent )
99
+ throw new Exception ( $ "Script replacement '{ scriptPath } ' for '{ scriptPathWithFingerprintPattern } ' didn't produce any change in '{ indexHtmlPath } '") ;
100
+
101
+ File . WriteAllText ( indexHtmlPath , newIndexHtmlContent ) ;
102
+ }
103
+ }
104
+
105
+ private void AssertImportMapInHtml ( string indexHtmlPath , string endpointsManifestPath , string scriptPath )
90
106
{
91
107
var indexHtmlContent = File . ReadAllText ( indexHtmlPath ) ;
92
108
var endpoints = JsonSerializer . Deserialize < StaticWebAssetEndpointsManifest > ( File . ReadAllText ( endpointsManifestPath ) ) ;
93
109
94
- if ( assetMainJs )
95
- {
96
- var mainJs = GetFingerprintedPath ( "main.js" ) ;
97
- Assert . DoesNotContain ( "src=\" main.js\" " , indexHtmlContent ) ;
98
- Assert . Contains ( $ "src=\" { mainJs } \" ", indexHtmlContent ) ;
99
- }
110
+ var fingerprintedScriptPath = GetFingerprintedPath ( scriptPath ) ;
111
+ Assert . DoesNotContain ( $ "src=\" { scriptPath } \" ", indexHtmlContent ) ;
112
+ Assert . Contains ( $ "src=\" { fingerprintedScriptPath } \" ", indexHtmlContent ) ;
100
113
101
114
Assert . Contains ( GetFingerprintedPath ( "_framework/dotnet.js" ) , indexHtmlContent ) ;
102
115
Assert . Contains ( GetFingerprintedPath ( "_framework/dotnet.native.js" ) , indexHtmlContent ) ;
103
116
Assert . Contains ( GetFingerprintedPath ( "_framework/dotnet.runtime.js" ) , indexHtmlContent ) ;
104
117
105
118
string GetFingerprintedPath ( string route )
106
- => endpoints . Endpoints . FirstOrDefault ( e => e . Route == route && e . Selectors . Length == 0 ) ? . AssetFile ?? throw new Exception ( $ "Missing endpoint for file '{ route } '") ;
119
+ => endpoints . Endpoints . FirstOrDefault ( e => e . Route == route && e . Selectors . Length == 0 ) ? . AssetFile ?? throw new Exception ( $ "Missing endpoint for file '{ route } ' in ' { endpointsManifestPath } ' ") ;
107
120
}
108
121
}
0 commit comments