Minify multiple Javascript files in a folder with UglifyJS

by Viet Anh Nguyen
  
 
  
 ~ less than 1 minute read

To minify multiple Javascript files, you can use Grunt. However, there is a much easier way to archive it using UglifyJS and Bashscript.

Step 1: Install UglifyJS

npm install -g uglify-js

Step 2: Write some Bash script code to find and minify all js files in a folder

for file in path/to/js/folder/*.js; do
    uglifyjs "$file" --stats -c -m  -o "$file" 
    echo minified: "$file" 
done 

Step 3: Run script

Save script in Step 2 to a file called ‘minify-js.sh’ and run it using Terminal:

sudo chmod +x minify-js.sh
./minify-js.sh

Modify this code a litte and you can use it with cleancss or other minifier.

This code help me much in deploying my webiste (a static site built with Jekyll). Hope it help someone else.

Disqus comment

Lưu ý: Hệ thống comment Disqus có thể không được load thành công do bị chặn bởi nhà mạng. Để khắc phục, đổi DNS về 8.8.8.8 hoặc 8.8.4.4 .

Designed by @vietanhdev.
Copyrighted