Last-modified: 2014-10-09 (木) 20:49:56
node.js/Javascriptのminifyがしたい

概要

javascriptのminifyをコマンドで行います。

手順

  1. uglify-jsをインストール
    Everything is expanded.Everything is shortened.
      1
    
     
    
    npm install -g uglify-js

サンプル

サンプルスクリプト全体はスクリプト/deploy.shを参照

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 
 
 
-
|
|
|
|
|
!
 
function JSOptimize() {
    find "$workPath/code/skin" -type f ! -name '*.min.js' -and -name '*.js' -print0 |
        while read -r -d '' orgFilePath
        do
            local cnvFilePath=${orgFilePath%%.js}_$DATETIME.min.js
            uglifyjs "$orgFilePath" > "$cnvFilePath" || { echo "[!!ERROR!!] failed to JS optimize(minify)"; exit 1; }
            find "$workPath/code" -type f -print0 |
                xargs -0 sed -i "s/`basename $orgFilePath .js`\.js/`basename $cnvFilePath .min.js`\.min\.js/g" ||
                    { echo "[!!ERROR!!] failed to JS optimize"; exit 1; }
        done
}

検証時の環境