forked from wo/tpg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_js.php
65 lines (57 loc) · 1.7 KB
/
_js.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* Preprocesses JS files.
* If name.js is requested, strip all comments and debugging code and output the rest.
* if name.debug.js is requested, comments and debugging code are preserved;
* Several script files can be packaged into one by requesting name1-name2-etc.js.
**/
header("Content-type: text/javascript");
// parse request:
if (!preg_match("/^([^\.]+)(\.debug)?\.js/", $_GET['file'], $matches)) {
print "alert('JS loading error: invalid request {$_GET['file']}');\n";
exit;
}
$files = explode("-", $matches[1]);
$debug = isset($matches[2]) ? $matches[2] : '';
// put together output:
$result = "";
if (count($files) > 1) {
$result .= "/* check the source on github.com/wo/tpg */\n\n";
}
foreach ($files as $file) {
if (!file_exists("$file.js")) {
print "alert('JS loading error: file {$_GET['file']} not found');\n";
exit;
}
$js = file("$file.js");
if (!$debug) $js = compress(strip_debugging($js));
$result .= implode("", $js)."\n";
}
// send output (gzipped if browser supports that):
ob_start("ob_gzhandler");
print $result;
ob_flush();
exit;
function strip_debugging($js) {
// strip all debugging commands, i.e. all lines beginning with "log(":
$newjs = array();
foreach ($js as $line) {
if (preg_match("/^\s*log\(/", $line)) continue;
$newjs[] = $line;
}
return $newjs;
}
function compress($js) {
// strip comments and superfluous whitespace:
$newjs = array();
foreach ($js as $line) {
if (strpos($line, "// ") !== false) $line = substr($line, 0, strpos($line, "// "));
$line = trim($line);
if ($line != "") $newjs[] = $line;
}
$jsstr = implode("\t\n", $newjs);
$jsstr = preg_replace("!\s*/\*.*?\*/\s*!s", "\n", $jsstr);
$js = explode("\t", $jsstr);
return $js;
}
?>