cssminify update

2007-12-05 @ 11:18#

i updated my cssminify tool this week. it now handles whitespace and newlines a bit better. no other big changes as this script is all about making the file smaller (not making it *better* like CSSTidy).

below is the entire script. i have this in my environment path and use it from the command line.

' ***************************************************
' cssminify.vbs   
' 2007-10-03 (mca)
' 2007-12-05 (mca) : improved regexp & space-handling
' remove comments and whitespace from CSS files
' no tidying up here, just minifying
' usage:
'    cssminify infile.css [outfile.css] ["comment"]
' ***************************************************

' vars
Dim fso,f,cmt,rex,data,re_cmt,re_ws,re_sp,ifile,ofile
re_cmt = "/\*[\s\S]*?\*/"
re_ws = "[\t\r\n]]*"
re_sp = "[\s]{2,}"
re_nl = "\} "

' get input file
If WScript.Arguments.Count=0 Then
  WScript.Echo "Missing Input File"
  WScript.Quit
Else
  ifile = WScript.Arguments.Item(0)
End If

' get output file (or set default)
If WScript.Arguments.Count>1 Then
  ofile = WScript.Arguments.Item(1)
Else
  ofile = "min-" & WScript.Arguments.Item(0)
End If

' get comment line
If WScript.Arguments.Count=3 Then
  cmt = "/* " & WScript.Arguments.Item(2) & " */"
Else
  cmt = ""
End If

' set file object
Set fso = CreateObject("Scripting.FileSystemObject")

' read data in
Set f = fso.OpenTextFile(ifile,1,False) 
  data = f.ReadAll()
  f.Close
Set f = Nothing

' do regexp work
Set rex = new RegExp
  rex.MultiLine = True
  rex.Global = True
  rex.Pattern = re_cmt
  data = rex.Replace(data,"")
  rex.Pattern = re_ws
  data = rex.Replace(data," ")
  rex.Pattern = re_sp
  data = rex.Replace(data," ")
  rex.Pattern = re_nl
  data = rex.Replace(data,"}" & Chr(13) & Chr(10))
Set rex = Nothing

' write data out
Set f = fso.CreateTextFile(ofile,1,False)
  f.WriteLine("/* cssminify @ " & Now() & " */")
  If cmt<>"" Then f.WriteLine(cmt)
  f.Write(Trim(data))
  f.Close
Set f = Nothing

' all done
WScript.Quit

code