Recently faced a requirement to search all the JS files from a visual studio solution, minify each & overwrite at the same location.
After some research I found a node component named as Uglify-JS to a achieve the functionality.
So, first I downloaded the Uglify-JS node component & which is placed to my local path: C:\Program Files\nodejs\node_modules\grunt-contrib-uglify\node_modules\uglify-js\bin\uglifyjs
Then, prepared a powershell script below to iterate each directory & its sub-directories specified in the input array, do the minification for each & overwrite to the same file.
$arrayInput = ("C:ProjectsMinificationDemoMinificationDemoScripts", "C:UsersTarunDesktoptest1", "C:UsersTarunDesktoptest2")
foreach ($input in $arrayInput)
{
$folders = Get-ChildItem -path $input -Recurse -include *.js
Foreach ($fldr in $folders)
{
if($fldr.Attributes -ne 'Directory')
{
node " C:Program Filesnodejsnode_modulesgrunt-contrib-uglifynode_modulesuglify-jsbinuglifyjs " --output $fldr.FullName $fldr.FullName
Write-Host $fldr.FullName "has been minified."
}
}
}
Hope it will give the basic idea about how simply we can minify the JS files.
Happy Coding
Tarun Kumar Chatterjee
Leave a comment