So now we are going to talk about how to do this yourself.
SlimIt is available from PyPi at http://pypi.python.org/pypi/slimit
You will probably need to use ‘sudo’ to run many of these commands with appropriate permissions unless you like to hang around logged in as root which is not usually a great idea.
If you don’t have pip, and you are on CentOS or RHEL or Fedora, you can install pip first with this command.
[sudo] yum install python-pip
If or when you have pip ready you can install SlimIt using this command.
[sudo] pip install slimit
On my CentOS systems, pip is named ‘pip-python’ instead.
To install rCSSmin, download the bzipped tar file (or gzipped or the .zip) from it’s page and decompress the archive, then run ‘python setup.py install’ in the directory created. Assuming you have wget installed, you can do this like so:
wget http://storage.perlig.de/rcssmin/rcssmin-1.0.1.tar.bz2 tar -xjf rcssmin-1.0.1.tar.bz2 cd rcssmin-1.0.1 [sudo] python setup.py install
The rCSSMin module supports preserving CSS comments which begin with ! character (bang) so that licenses and other important comments can be preserved.
Unfortunately, SlimIt and also rJSmin both seem to lack an equivalent capability, so I have setup the code to prepend license text to the minified version of the file.
In order to use this script, just replace the values in the jsSources and cssCources arrays with the paths to your source files, and change the values of jsDestPath, jsMinPath, cssDestPath, and cssMinPath to the paths where you would like to output your unminified and minified output files.
#!/usr/bin/python2 from slimit import minify from rcssmin import cssmin def minifyCSSProc(srcText): return cssmin(srcText, keep_bang_comments=True) def minifyJSProc(srcText): return minify(srcText, mangle=True, mangle_toplevel=True) def doProcessFiles(minifyProc, sourcePaths, header, destPath, minPath): print "Combining to %s and %s" % (destPath,minPath) f = open(destPath, 'w') mf = None try: mf = open(minPath, 'w') mf.write(header) for srcFile in sourcePaths: print(srcFile) with open(srcFile) as inputFile: srcText = inputFile.read() minText = minifyProc(srcText) f.write(srcText) mf.write(minText) finally: f.close() if mf and not mf.closed: mf.close() def doJSMin(sourcePaths, header, destPath, minPath): return doProcessFiles(minifyJSProc, sourcePaths, header, destPath, minPath) def doCSSMin(sourcePaths, destPath, minPath): return doProcessFiles(minifyCSSProc, sourcePaths, '', destPath, minPath) jsDestPath = "static/js/allmy.js" jsMinPath = "static/js/allmy.min.js" jsHeaderPath = "jslicenses.js" jsSources = [ "static/js/first-js.js", "static/js/second-one.js" ] cssDestPath = "static/css/allmy.css" cssMinPath = "static/css/allmy.min.css" cssSources = [ "static/css/top.css", "static/css/main.css" ] if __name__ == '__main__': jsHeader = '' with open(jsHeaderPath) as f: jsHeader = f.read() doJSMin(jsSources, jsHeader, jsDestPath, jsMinPath) doCSSMin(cssSources, cssDestPath, cssMinPath)
This script or something like it can form part of your web deployment process which I recommend automating to prevent mistakes. You can combine this logic with other preprocessing steps and then automate something like rsync to upload your files. Just be careful the first few times when your deployment scripts are new!