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

概要

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

手順

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

サンプル

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

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
-
|
|
!
 
 
-
|
|
|
|
|
!
 
# CSSのminify,リネーム,リプレイス
# minify,リネーム対象:code/skin以下
# リプレイス対象:code以下
function CSSOptimize() {
    find "$workPath/code/skin" -type f ! -name '*.min.css' -and -name '*.css' -print0 |
        while read -r -d '' orgFilePath
        do
            local cnvFilePath=${orgFilePath%%.css}_$DATETIME.min.css
            uglifycss "$orgFilePath" > "$cnvFilePath" &&
            find "$workPath/code" -type f -print0 |
                xargs -0 sed -i "s/`basename $orgFilePath .css`\.css/`basename $cnvFilePath .min.css`\.min\.css/g" &&
            $RM "$orgFilePath" || { echo "[!!ERROR!!] failed to CSS optimize"; exit 1; }
        done
}

検証時の環境