Skip to content

Commit 8c0b0e0

Browse files
author
Nate Nasteff
committed
code reduction and expandability (minify func), removed needles in favor of more regex
1 parent 52fb642 commit 8c0b0e0

File tree

2 files changed

+34
-35
lines changed

2 files changed

+34
-35
lines changed

minify.php

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/php
22
<?php # minify.php - CSS minifier for CLI - Nate Nasteff 2020
33

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..
55

66
if ($argc == 1 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
77

@@ -19,7 +19,7 @@
1919
}
2020

2121
// 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
2323

2424
elseif ($argc > 2) {
2525
?>
@@ -30,66 +30,61 @@
3030
minify <foo.css>
3131

3232
<?php
33-
3433
}
3534

3635
else {
37-
// Define needles for stripping newlines and spaces
38-
39-
$needles = ["\n", " "];
4036

4137
// Define regex patterns to strip comments
4238

43-
$regex = array(
39+
$regex = [
4440
"`^([\t\s]+)`ism"=>'',
4541
"`^\/\*(.+?)\*\/`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+
];
5048

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
5251

5352
if (file_exists($argv[1])){
5453

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
6355

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+
};
6560

6661
// Update filename
6762

6863
$minified_filename = str_replace(".css", ".min.css", $argv[1]);
6964

7065
// Attempt to save the new minified CSS
7166
try {
72-
file_put_contents($minified_filename, $css_str);
67+
file_put_contents($minified_filename, $minify());
7368
}
7469

7570
catch (Exception $e) {
7671
echo $e->getMessage();
7772
}
7873

7974
echo "Minified CSS file written to " . $minified_filename ."\n";
80-
}
75+
}
8176

82-
// Make sure file is actually a valid CSS file..
77+
// Make sure file is actually a valid CSS file..
8378

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+
}
8782

88-
// If no file is found / incorrect filename..
83+
// If no file is found / incorrect filename..
8984

90-
else {
91-
echo "File not found or incorrect filename!";
85+
else {
86+
echo "File not found or incorrect filename!";
87+
}
9288
}
9389

94-
}
95-
?>
90+
?>

test.css

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
html {
22
padding: 0;
33
margin: 0;
4+
5+
46
width: 100%;
7+
58
height: 100%;
69
}
710

811
/* Adding some comments */
912

1013
html {
11-
padding: 0;
14+
padding: 0;
1215
margin: 0;
13-
width: 100%;
14-
height: 100%;
16+
width: 100%;
17+
height: 100%;
1518
}
1619

1720
a {
1821
padding: 0;
22+
1923
content: '/* Test */';
2024
}

0 commit comments

Comments
 (0)