source: Dev/branches/jQueryUI/client/RGraph/scripts/minify @ 249

Last change on this file since 249 was 249, checked in by hendrikvanantwerpen, 13 years ago

This one's for Subversion, because it's so close...

First widget (stripped down sequencer).
Seperated client and server code in two direcotry trees.

File size: 3.6 KB
Line 
1#!/usr/bin/php -q
2<?php
3    /**
4    * This is a minification script. It minifies the desired script "in-place"
5    *
6    *  Usage (Unix): ./minify file1 file2 ...
7    *  Usage (Windows): C:\php\php.exe minify file1 file2 ...
8    *
9    * The minification is medium, and it can over halve a scripts size.
10    * Combined with subsequent gzip compression the results can be excellent. For
11    * example a 70k script of mine was reduced to just 7k. (!) Substantial
12    * bandwidth savings for you, and much improved performance for your users.
13    *
14    * NOTE: Usage on Windows works, but is less tested
15    */
16
17    /**
18    * Check for arg
19    */
20    if (empty($_SERVER['argv'][1])) {
21        echo "RGraph minification script\n";
22        echo "==========================\n";
23        echo "Usage (Unix):     " . $_SERVER['argv'][0] . " file1 file2 ...\n";
24        echo "Usage (Windows): C:\php\php.exe " . $_SERVER['argv'][0] . " file1 file2 ...\n\n";
25        exit;
26
27    } else {
28
29        for ($i=1; $i<count($_SERVER['argv']); ++$i) {
30            Minify($_SERVER['argv'][$i]);
31        }
32    }
33
34
35    /**
36    * This is the function that does the work of minifying the file
37    *
38    * @param $filename string The filename to be minified
39    */
40    function Minify($filename)
41    {
42        /**
43        * Begin
44        */
45        $original = file_get_contents($filename);
46        $new = preg_replace('/^ +/m', '', $original);    // Lose spaces at the start of lines
47        $new = preg_replace('/ *\/\/.*$/m', '', $new);   // Lose comments of the style: "// ..."
48        $new = preg_replace("/;\r?\n/m", ";\r\n", $new); // Make all lines end with \r\n
49        //$new = preg_replace("|\r\n\*|", "", $new);
50       
51        /**
52        * Get rid of block comments
53        */
54        $out = '';
55        $inBlock = false; // Are we in a block comment
56        for ($i=0; $i<strlen($new); $i++) {
57            if (substr($new, $i, 1) == '/') {
58                // Read the next char
59                if (!$inBlock AND substr($new, $i, 2) == '/*') {
60                    $inBlock = true;
61                }
62            } elseif (substr($new, $i, 2) == '*/') {
63                $inBlock = false;
64                $i++;
65                continue;
66            }
67           
68            if (!$inBlock) {
69                $out .= substr($new, $i, 1);
70            }
71        }
72       
73        /**
74        * Get rid of double line breaks
75        *
76        * NOTE: Is this necessary?
77        */
78        $out = preg_replace('|\n+|', "\n", $out);
79       
80        /**
81        * Further tweaks
82        * UPDATED: 28th March 2009 - Line endings have been changed - they should all be \r\n
83        */
84        $out = str_replace(";\r\n}", ';}', $out);
85        $out = str_replace(";\r\nRGraph", ';RGraph', $out);
86        $out = preg_replace('/;\r\n([a-z])/i', ';$1', $out);
87        $out = str_replace('if (', 'if(', $out);
88        $out = str_replace(') {', '){', $out);
89        $out = str_replace("}\r\n", '}', $out);
90        $out = str_replace("{\r\n", '{', $out); // UPDATED
91        $out = str_replace("{\r\n", '{', $out); // UPDATED
92        $out = str_replace("}\r\n}", '}}', $out); // UPDATED
93        $out = str_replace("}\r\n}", '}}', $out); // UPDATED
94        $out = str_replace("}\r\n}", '}}', $out); // UPDATED
95        $out = preg_replace('/ {2,}= /', ' = ', $out);
96       
97        // Mark the file as minified
98        $out = "// THIS FILE HAS BEEN MINIFIED\r\n" . $out;
99   
100        file_put_contents($filename, $out);
101       
102        printf(" Minifying {$filename} Before: %s After: %s Saving: %d%%\n", number_format(strlen($original)), number_format(strlen($out)), number_format((((strlen($original) - strlen($out)) / strlen($original)) * 100), 1));
103    }
104?>
Note: See TracBrowser for help on using the repository browser.