@@ -46,70 +46,105 @@ public void Build_FingerprintsContent_WhenEnabled()
46
46
AssertBuildAssets ( manifest1 , outputPath , intermediateOutputPath ) ;
47
47
}
48
48
49
- public static TheoryData < string , string , string > WriteImportMapToHtmlData => new TheoryData < string , string , string >
49
+ public static TheoryData < string , string , string , bool , bool > WriteImportMapToHtmlData => new TheoryData < string , string , string , bool , bool >
50
50
{
51
- { "VanillaWasm" , "main.js" , null } ,
52
- { "BlazorWasmMinimal" , "_framework/blazor.webassembly.js" , "_framework/blazor.webassembly#[.{fingerprint}].js" }
51
+ { "VanillaWasm" , "main.js" , "main#[.{fingerprint}].js" , true , true } ,
52
+ { "VanillaWasm" , "main.js" , null , false , false } ,
53
+ { "BlazorWasmMinimal" , "_framework/blazor.webassembly.js" , "_framework/blazor.webassembly#[.{fingerprint}].js" , false , true }
53
54
} ;
54
55
55
56
[ Theory ]
56
57
[ MemberData ( nameof ( WriteImportMapToHtmlData ) ) ]
57
- public void Build_WriteImportMapToHtml ( string testAsset , string scriptPath , string scriptPathWithFingerprintPattern )
58
+ public void Build_WriteImportMapToHtml ( string testAsset , string scriptPath , string scriptPathWithFingerprintPattern , bool fingerprintUserJavascriptAssets , bool expectFingerprintOnScript )
58
59
{
59
60
ProjectDirectory = CreateAspNetSdkTestAsset ( testAsset ) ;
60
61
ReplaceStringInIndexHtml ( ProjectDirectory , scriptPath , scriptPathWithFingerprintPattern ) ;
62
+ FingerprintUserJavascriptAssets ( fingerprintUserJavascriptAssets ) ;
61
63
62
64
var build = CreateBuildCommand ( ProjectDirectory ) ;
63
- ExecuteCommand ( build , "-p:WriteImportMapToHtml=true" ) . Should ( ) . Pass ( ) ;
65
+ ExecuteCommand ( build , "-p:WriteImportMapToHtml=true" , $ "-p:FingerprintUserJavascriptAssets= { fingerprintUserJavascriptAssets } " ) . Should ( ) . Pass ( ) ;
64
66
65
67
var intermediateOutputPath = build . GetIntermediateDirectory ( DefaultTfm , "Debug" ) . ToString ( ) ;
66
68
var indexHtmlPath = Directory . EnumerateFiles ( Path . Combine ( intermediateOutputPath , "staticwebassets" , "importmaphtml" , "build" ) , "*.html" ) . Single ( ) ;
67
69
var endpointsManifestPath = Path . Combine ( intermediateOutputPath , $ "staticwebassets.build.endpoints.json") ;
68
70
69
- AssertImportMapInHtml ( indexHtmlPath , endpointsManifestPath , scriptPath ) ;
71
+ AssertImportMapInHtml ( indexHtmlPath , endpointsManifestPath , scriptPath , expectFingerprintOnScript : expectFingerprintOnScript ) ;
70
72
}
71
73
72
74
[ Theory ]
73
75
[ MemberData ( nameof ( WriteImportMapToHtmlData ) ) ]
74
- public void Publish_WriteImportMapToHtml ( string testAsset , string scriptPath , string scriptPathWithFingerprintPattern )
76
+ public void Publish_WriteImportMapToHtml ( string testAsset , string scriptPath , string scriptPathWithFingerprintPattern , bool fingerprintUserJavascriptAssets , bool expectFingerprintOnScript )
75
77
{
76
78
ProjectDirectory = CreateAspNetSdkTestAsset ( testAsset ) ;
77
79
ReplaceStringInIndexHtml ( ProjectDirectory , scriptPath , scriptPathWithFingerprintPattern ) ;
80
+ FingerprintUserJavascriptAssets ( fingerprintUserJavascriptAssets ) ;
78
81
79
82
var projectName = Path . GetFileNameWithoutExtension ( Directory . EnumerateFiles ( ProjectDirectory . TestRoot , "*.csproj" ) . Single ( ) ) ;
80
83
81
84
var publish = CreatePublishCommand ( ProjectDirectory ) ;
82
- ExecuteCommand ( publish , "-p:WriteImportMapToHtml=true" ) . Should ( ) . Pass ( ) ;
85
+ ExecuteCommand ( publish , "-p:WriteImportMapToHtml=true" , $ "-p:FingerprintUserJavascriptAssets= { fingerprintUserJavascriptAssets } " ) . Should ( ) . Pass ( ) ;
83
86
84
87
var outputPath = publish . GetOutputDirectory ( DefaultTfm , "Debug" ) . ToString ( ) ;
85
88
var indexHtmlOutputPath = Path . Combine ( outputPath , "wwwroot" , "index.html" ) ;
86
89
var endpointsManifestPath = Path . Combine ( outputPath , $ "{ projectName } .staticwebassets.endpoints.json") ;
87
90
88
- AssertImportMapInHtml ( indexHtmlOutputPath , endpointsManifestPath , scriptPath ) ;
91
+ AssertImportMapInHtml ( indexHtmlOutputPath , endpointsManifestPath , scriptPath , expectFingerprintOnScript : expectFingerprintOnScript ) ;
89
92
}
90
93
91
- private void ReplaceStringInIndexHtml ( TestAsset testAsset , string scriptPath , string scriptPathWithFingerprintPattern )
94
+ private void FingerprintUserJavascriptAssets ( bool fingerprintUserJavascriptAssets )
92
95
{
93
- if ( scriptPathWithFingerprintPattern != null )
96
+ if ( fingerprintUserJavascriptAssets )
97
+ {
98
+ ProjectDirectory . WithProjectChanges ( p =>
99
+ {
100
+ if ( p . Root != null )
101
+ {
102
+ var itemGroup = new XElement ( "ItemGroup" ) ;
103
+ var pattern = new XElement ( "StaticWebAssetFingerprintPattern" ) ;
104
+ pattern . SetAttributeValue ( "Include" , "Js" ) ;
105
+ pattern . SetAttributeValue ( "Pattern" , "*.js" ) ;
106
+ pattern . SetAttributeValue ( "Expression" , "#[.{fingerprint}]!" ) ;
107
+ itemGroup . Add ( pattern ) ;
108
+ p . Root . Add ( itemGroup ) ;
109
+ }
110
+ } ) ;
111
+ }
112
+ }
113
+
114
+ private void ReplaceStringInIndexHtml ( TestAsset testAsset , string sourceValue , string targetValue )
115
+ {
116
+ if ( targetValue != null )
94
117
{
95
118
var indexHtmlPath = Path . Combine ( testAsset . TestRoot , "wwwroot" , "index.html" ) ;
96
119
var indexHtmlContent = File . ReadAllText ( indexHtmlPath ) ;
97
- var newIndexHtmlContent = indexHtmlContent . Replace ( scriptPath , scriptPathWithFingerprintPattern ) ;
120
+ var newIndexHtmlContent = indexHtmlContent . Replace ( sourceValue , targetValue ) ;
98
121
if ( indexHtmlContent == newIndexHtmlContent )
99
- throw new Exception ( $ "Script replacement '{ scriptPath } ' for '{ scriptPathWithFingerprintPattern } ' didn't produce any change in '{ indexHtmlPath } '") ;
122
+ throw new Exception ( $ "String replacement '{ sourceValue } ' for '{ targetValue } ' didn't produce any change in '{ indexHtmlPath } '") ;
100
123
101
124
File . WriteAllText ( indexHtmlPath , newIndexHtmlContent ) ;
102
125
}
103
126
}
104
127
105
- private void AssertImportMapInHtml ( string indexHtmlPath , string endpointsManifestPath , string scriptPath )
128
+ private void AssertImportMapInHtml ( string indexHtmlPath , string endpointsManifestPath , string scriptPath , bool expectFingerprintOnScript = true )
106
129
{
107
130
var indexHtmlContent = File . ReadAllText ( indexHtmlPath ) ;
108
131
var endpoints = JsonSerializer . Deserialize < StaticWebAssetEndpointsManifest > ( File . ReadAllText ( endpointsManifestPath ) ) ;
109
132
110
133
var fingerprintedScriptPath = GetFingerprintedPath ( scriptPath ) ;
111
- Assert . DoesNotContain ( $ "src=\" { scriptPath } \" ", indexHtmlContent ) ;
112
- Assert . Contains ( $ "src=\" { fingerprintedScriptPath } \" ", indexHtmlContent ) ;
134
+ if ( expectFingerprintOnScript )
135
+ {
136
+ Assert . DoesNotContain ( $ "src=\" { scriptPath } \" ", indexHtmlContent ) ;
137
+ Assert . Contains ( $ "src=\" { fingerprintedScriptPath } \" ", indexHtmlContent ) ;
138
+ }
139
+ else
140
+ {
141
+ Assert . Contains ( scriptPath , indexHtmlContent ) ;
142
+
143
+ if ( scriptPath != fingerprintedScriptPath )
144
+ {
145
+ Assert . DoesNotContain ( fingerprintedScriptPath , indexHtmlContent ) ;
146
+ }
147
+ }
113
148
114
149
Assert . Contains ( GetFingerprintedPath ( "_framework/dotnet.js" ) , indexHtmlContent ) ;
115
150
Assert . Contains ( GetFingerprintedPath ( "_framework/dotnet.native.js" ) , indexHtmlContent ) ;
0 commit comments