|
26 | 26 |
|
27 | 27 | <h1 style="margin-top: 48px;">How to detect file compression</h1> |
28 | 28 |
|
| 29 | +<p> |
| 30 | + To demonstrate the compressed files execute the 'LocalBuild.cmd' at the root of this repository and follow |
| 31 | + the instructions to browse to the demonstration. |
| 32 | +</p> |
29 | 33 | <p> |
30 | 34 | Press F12 to enter your browser's development tools and select the network tab (we did this on Edge here). |
31 | 35 | Click any css or js file (except for blazor.server.js, which as an ASP.NET framework file isn't compressed) |
|
43 | 47 | </p> |
44 | 48 |
|
45 | 49 | <pre><code> |
46 | | - <PropertyGroup> |
47 | | - <!--Paths to tools, tasks, and extensions are calculated relative to the BlazorWebAssemblySdkDirectoryRoot. |
48 | | - But there does not seem to be a property for that, so after much trial and error, using the property |
49 | | - ILLinkTasksAssembly seems most reliable as it contains the same value whether the build is run from VS |
50 | | - or the command line, unlike many others such as MSBuildSDKsPath. |
51 | | - If a better property is identified, this can be changed. |
52 | | - --> |
53 | | - |
54 | | - <!-- First, we strip the property value back to the root SDKs level and then down into the WebAssembly SDK --> |
55 | | - <_SDKRoot>$(ILLinkTasksAssembly.Substring(0,$(ILLinkTasksAssembly.LastIndexOf('Microsoft.NET.ILLink.Tasks'))))</_SDKRoot> |
56 | | - <BlazorWebAssemblySdkDirectoryRoot>$(_SDKRoot)\Microsoft.NET.Sdk.BlazorWebAssembly\</BlazorWebAssemblySdkDirectoryRoot> |
57 | | - |
58 | | - <!-- The next four lines are Copyright (c) .NET Foundation. All rights reserved. --> |
59 | | - <_BlazorWebAssemblySdkTasksTFM Condition=" '$(MSBuildRuntimeType)' == 'Core'">net6.0</_BlazorWebAssemblySdkTasksTFM> |
60 | | - <_BlazorWebAssemblySdkTasksTFM Condition=" '$(MSBuildRuntimeType)' != 'Core'">net472</_BlazorWebAssemblySdkTasksTFM> |
61 | | - <_BlazorWebAssemblySdkTasksAssembly>$(BlazorWebAssemblySdkDirectoryRoot)tools\$(_BlazorWebAssemblySdkTasksTFM)\Microsoft.NET.Sdk.BlazorWebAssembly.Tasks.dll</_BlazorWebAssemblySdkTasksAssembly> |
62 | | - <_BlazorWebAssemblySdkToolAssembly>$(BlazorWebAssemblySdkDirectoryRoot)tools\net6.0\Microsoft.NET.Sdk.BlazorWebAssembly.Tool.dll</_BlazorWebAssemblySdkToolAssembly> |
63 | | - </PropertyGroup> |
64 | | - |
65 | | - <!-- The next two lines are Copyright (c) .NET Foundation. All rights reserved. --> |
66 | | - <UsingTask TaskName="Microsoft.NET.Sdk.BlazorWebAssembly.BrotliCompress" AssemblyFile="$(_BlazorWebAssemblySdkTasksAssembly)" /> |
67 | | - <UsingTask TaskName="Microsoft.NET.Sdk.BlazorWebAssembly.GzipCompress" AssemblyFile="$(_BlazorWebAssemblySdkTasksAssembly)" /> |
68 | | - |
69 | | - |
70 | | - |
71 | | - |
72 | | - <!-- |
73 | | - This target runs after build. If you want to run after Publish, change: |
74 | | - - AfterTargets should be set to "Build" |
75 | | - - "ProjectDir" in MyStaticFiles should be changed to "PublishDir" |
76 | | - --> |
77 | | - <Target Name="CustomGZipAndBrotliCompression" AfterTargets="Build"> |
78 | | - |
79 | | - <!--************* THIS IS WHERE YOU CHOOSE WHAT TO COMPRESS ***************** |
80 | | - you probably don't want to change anything else - just this ItemGroup--> |
81 | | - |
82 | | - <ItemGroup> |
83 | | - <MyStaticFiles Include="$(ProjectDir)/wwwroot/**/*.css" /> |
84 | | - <MyStaticFiles Include="$(ProjectDir)/wwwroot/**/*.js" /> |
85 | | - </ItemGroup> |
86 | | - |
87 | | - <!--Call the BlazorWebAssembly Task GZipCompress and store the results in MyCompressedFiles--> |
88 | | - <GZipCompress FilesToCompress="@@(MyStaticFiles)" OutputDirectory="$(IntermediateOutputPath)compress\"> |
89 | | - <Output TaskParameter="CompressedFiles" ItemName="MyCompressedFiles" /> |
90 | | - </GZipCompress> |
91 | | - |
92 | | - <!--Call the BlazorWebAssembly Task BrotliCompress and store the results in MyCompressedFiles--> |
93 | | - <BrotliCompress OutputDirectory="$(IntermediateOutputPath)compress\" FilesToCompress="@@(MyStaticFiles)" ToolAssembly="$(_BlazorWebAssemblySdkToolAssembly)" ToolExe="$(_RazorSdkDotNetHostFileName)" ToolPath="$(_RazorSdkDotNetHostDirectory)"> |
94 | | - <Output TaskParameter="CompressedFiles" ItemName="MyCompressedFiles" /> |
95 | | - </BrotliCompress> |
96 | | - |
97 | | - <!--Log what we have done--> |
98 | | - <Message Importance="High" Text="======= Brotli/GZip Compression For @@(MyCompressedFiles->Count()) Static Files =======" /> |
99 | | - <Message Importance="High" Text=" Compressed: %(MyCompressedFiles.OriginalItemSpec)%(MyCompressedFiles.Extension)" /> |
100 | | - |
101 | | - <!--Rename/move the compressed files back to to the correct place/names as they have "hashed" names at this stage.--> |
102 | | - <Move SourceFiles="%(MyCompressedFiles.FullPath)" DestinationFiles="%(MyCompressedFiles.OriginalItemSpec)%(MyCompressedFiles.Extension)" /> |
103 | | - |
104 | | - </Target> |
105 | | - <ItemGroup> |
106 | | - <ProjectReference Include="..\CompressedStaticFiles.AspNetCore\CompressedStaticFiles.AspNetCore.csproj" /> |
107 | | - </ItemGroup> |
108 | | - <ItemGroup> |
109 | | - <Folder Include="wwwroot\images\" /> |
110 | | - </ItemGroup> |
| 50 | + <!--Compression directives (Note that the patterns are relative to wwwroot)--> |
| 51 | + <PropertyGroup> |
| 52 | + <CompressionIncludePatterns>$(CompressionIncludePatterns);**/*.css;**/*.js</CompressionIncludePatterns> |
| 53 | + <BuildCompressionFormats>gzip;brotli</BuildCompressionFormats> |
| 54 | + <PublishCompressionFormats>gzip;brotli</<PublishCompressionFormats> |
| 55 | + </PropertyGroup> |
111 | 56 | </code></pre> |
112 | 57 |
|
113 | 58 | <h1 style="margin-top: 48px;">Attribution</h1> |
114 | 59 |
|
115 | 60 | <p>Thanks to the following:</p> |
116 | 61 | <ul> |
117 | 62 | <li>This project is a fork of Peter Andersson's from whose original <a href="https://github.com/AnderssonPeter/CompressedStaticFiles" target="_blank">CompressedStaticFiles repo</a>; and</li> |
118 | | - <li><a href="https://github.com/SQL-MisterMagoo" target="_blank">SQL-MisterMagoo</a> for providing the MSBUILD code that performs Brotli and GZIP static file compression.</li> |
119 | 63 | </ul> |
0 commit comments