Minify CSS and JavaScript files with PowerShell script

JavaScript and CSS file optimization with PowerShell script

Minification of Cascade Style Sheet (CSS) and Java Script (JS) should be on a TODO list for every web application. 

The minification of CSS and JS can be easily one on the runtime with ASP.NET bundling which is build in in ASP.NET framework, but in case of distributing your resource files over CDN you cannot use the runtime minification. You physical files need to be minified. However, the other side which is source control needs to keep the original files, so you cannot minify and check-in to your source control.

This is the case where you need automation to perform minification on the production file system which can be then taken to the CDN. I had a similar case and that is when I used PowerShell script to minify CSS and JS files on the filesystem where web application was deployed. 

Minify CSS files

I used YUICompressor.NET library for minifying CSS and JS file on the runtime and did not find any issues with it. It handles well both JS and CSS comments and removes all the whitespace without any issues so I decided to use it for PowerShell script as well. It can be downloaded from the NuGet package page https://www.nuget.org/packages/YUICompressor.NET or download it from this article download section in the top right corner along with PoweShell scripts.
The nice past with PowerShell is that you can easily load any .NET library and use it as if you are writing C# code. So first thing is to load the assembly Yahoo.Yui.Compressor.dll in PowerShell with [Reflection.Assembly]::LoadFile.

Note

If you prefer not to use paths in PowerShell you can load the assemblies to GAC with gacutil https://docs.microsoft.com/en-us/dotnet/framework/app-domains/how-to-install-an-assembly-into-the-gac and reference it with Add-Type cmdlet https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type?view=powershell-6

The rest is just looping through all CSS fies in the folder and applying Compress method from loaded assembly.

Param(  
[Parameter(Mandatory=$true)]  
[string]$folder
 )  
$libPath = $PSScriptRoot + "\lib\YuiCompressor\Yahoo.Yui.Compressor.dll"
[Reflection.Assembly]::LoadFile($libPath)

$files = get-childitem $folder -recurse -force -include *.css  
foreach($file in $files){ 
   $content = [IO.File]::ReadAllText($file.FullName)
   $cssCompressor = New-Object -TypeName Yahoo.Yui.Compressor.CssCompressor
   $compressedContent = $cssCompressor.Compress($content)
   Set-ItemProperty $file.FullName -name IsReadOnly -value $false
   [IO.File]::WriteAllText($file.FullName,$compressedContent)
}
    

Minify JS files

For JavaScript files, the proses is pretty much the same except we need to create an instance of Yahoo.Yui.Compressor.JavaScriptCompressor class and invoke Compress method of it.

Additionally we also need to load EcmaScript.NET.dll assembly which is required for JavaScriptCompressor.Compress method. You ca download it with NuGet package or you can download it from download section on this article page.

Param(  
[Parameter(Mandatory=$true)]  
[string]$folder
 )  

$libPathEcma = $PSScriptRoot + "\lib\YuiCompressor\EcmaScript.NET.dll"
$libPathCompressor = $PSScriptRoot + "\lib\YuiCompressor\Yahoo.Yui.Compressor.dll"

[Reflection.Assembly]::LoadFile($libPathEcma)
[Reflection.Assembly]::LoadFile($libPathCompressor)


$files = get-childitem $folder -recurse -force -include *.js  
foreach($file in $files){ 
   $content = [IO.File]::ReadAllText($file.FullName)
   $cssCompressor = New-Object -TypeName Yahoo.Yui.Compressor.JavaScriptCompressor
   $compressedContent = $cssCompressor.Compress($content)
   Set-ItemProperty $file.FullName -name IsReadOnly -value $false
   [IO.File]::WriteAllText($file.FullName,$compressedContent)
}
    

These PowerShell scripts can be invoked as a part of build in your CI and do the CSS and JS minification. 

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform includion ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

.NET

read more

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

Comments for this article