-
Notifications
You must be signed in to change notification settings - Fork 11
/
bundle.php
55 lines (43 loc) · 1.38 KB
/
bundle.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
<?php
require 'vendor/autoload.php';
// generate posts
passthru(sprintf("php %s/genposts.php", __DIR__), $out);
// compile less => css
$lessInput = __DIR__.'/public/css/main.less';
$cssOutput = __DIR__.'/public/css/compiled.css';
$lessCacheFile = __DIR__.'/less.cache';
if (!file_exists($cssOutput)) {
@unlink($lessCacheFile);
}
if (file_exists($lessCacheFile)) {
$cache = unserialize(file_get_contents($lessCacheFile));
} else {
$cache = __DIR__.'/public/css/main.less';
}
$less = new lessc;
$less->setFormatter("compressed");
$newCache = $less->cachedCompile($cache);
if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) {
if (file_put_contents($cssOutput, $newCache['compiled']) !== false) {
gzip($cssOutput);
file_put_contents($lessCacheFile, serialize($newCache));
}
}
// minify js
$jsDir = __DIR__.'/public/js/';
$jsOutput = 'compiled.js';
$js = '';
// for this site, the order of including doesn't matter
if (file_exists($jsDir)) {
foreach (scandir($jsDir) as $file) {
if (substr($file, -3) === '.js' && $file !== $jsOutput) {
$js .= \JShrink\Minifier::minify(file_get_contents($jsDir.$file));
}
}
}
file_put_contents($jsDir.$jsOutput, $js);
gzip($jsDir.$jsOutput);
function gzip($inFile, $outFile = null)
{
file_put_contents(($outFile === null) ? $inFile.'.gz' : $outFile, gzencode(file_get_contents($inFile), 9));
}