1
1
#!/usr/bin/php
2
2
<?php # minify.php - CSS minifier for CLI - Nate Nasteff 2020
3
3
4
- # If no args are received from CLI or help is requested, print instructions..
4
+ // If no args are received from CLI or help is requested, print instructions..
5
5
6
6
if ($ argc == 1 || in_array ($ argv [1 ], array ('--help ' , '-help ' , '-h ' , '-? ' ))) {
7
7
19
19
}
20
20
21
21
// Make sure no second argument gets passed at CLI
22
- // TODO: allow multiple css files at once
22
+ // TODO: Allow multiple css files at once
23
23
24
24
elseif ($ argc > 2 ) {
25
25
?>
30
30
minify <foo.css>
31
31
32
32
<?php
33
-
34
33
}
35
34
36
35
else {
37
- // Define needles for stripping newlines and spaces
38
-
39
- $ needles = ["\n" , " " ];
40
36
41
37
// Define regex patterns to strip comments
42
38
43
- $ regex = array (
39
+ $ regex = [
44
40
"`^([ \t\s]+)`ism " =>'' ,
45
41
"`^\/\*(.+?)\*\/`ism " =>"" ,
46
- "`([ \n\A;]+)\/\*(.+?)\*\/`ism " =>"$1 " ,
47
- "`([ \n\A;\s]+)//(.+?)[ \n\r]`ism " =>"$1 \n" ,
48
- "`(^[ \r\n]*|[ \r\n]+)[\s \t]*[ \r\n]+`ism " =>"\n"
49
- );
42
+ "`([ \n\A;]+)\/\*(.+?)\*\/`ism " =>"" ,
43
+ "`([ \n\A;\s]+)//(.+?)[ \n\r]`ism " =>"" ,
44
+ "`(^[ \r\n]*|[ \r\n]+)[\s \t]*[ \r\n]+`ism " =>"" ,
45
+ "/\s+/ " =>'' ,
46
+ "/;}/ " =>'} '
47
+ ];
50
48
51
- // Check that the file name is valid, and assign contents to css_str
49
+ // Check that the file name is valid
50
+ // TODO: Add logic for multiple files
52
51
53
52
if (file_exists ($ argv [1 ])){
54
53
55
- $ css_str = file_get_contents ($ argv [1 ]);
56
-
57
- // Remove comments, strip whitespaces / newlines
58
-
59
- $ css_str = preg_replace (array_keys ($ regex ), $ regex , $ css_str );
60
- $ css_str = str_replace ($ needles , "" , $ css_str );
61
-
62
- // Strip trailing semicolons at the end of css definitions
54
+ // Define anonymous func to return a minified string
63
55
64
- $ css_str = str_replace (";} " , "} " , $ css_str );
56
+ $ minify = function () use (&$ argv , &$ regex ){
57
+ $ css_str = file_get_contents ($ argv [1 ]);
58
+ return preg_replace (array_keys ($ regex ), $ regex , $ css_str );
59
+ };
65
60
66
61
// Update filename
67
62
68
63
$ minified_filename = str_replace (".css " , ".min.css " , $ argv [1 ]);
69
64
70
65
// Attempt to save the new minified CSS
71
66
try {
72
- file_put_contents ($ minified_filename , $ css_str );
67
+ file_put_contents ($ minified_filename , $ minify () );
73
68
}
74
69
75
70
catch (Exception $ e ) {
76
71
echo $ e ->getMessage ();
77
72
}
78
73
79
74
echo "Minified CSS file written to " . $ minified_filename ."\n" ;
80
- }
75
+ }
81
76
82
- // Make sure file is actually a valid CSS file..
77
+ // Make sure file is actually a valid CSS file..
83
78
84
- else if (!strpos ($ argv [1 ], '.css ' )) {
85
- echo "Not a valid CSS file! " ;
86
- }
79
+ else if (!strpos ($ argv [1 ], '.css ' )) {
80
+ echo "Not a valid CSS file! " ;
81
+ }
87
82
88
- // If no file is found / incorrect filename..
83
+ // If no file is found / incorrect filename..
89
84
90
- else {
91
- echo "File not found or incorrect filename! " ;
85
+ else {
86
+ echo "File not found or incorrect filename! " ;
87
+ }
92
88
}
93
89
94
- }
95
- ?>
90
+ ?>
0 commit comments