Git 使用钩子提交项目压缩静态文件
摘要:Git 使用钩子提交项目压缩静态文件,git本地提交到服务器上利用uglifyjs压缩静态资源。
线上的文件一般都是 css、js 都是混淆和压缩后的,这样一是减少文件的大小,二是减少网络的开销,还有就是混淆后的代码不易于读取算是提高下安全性吧。压缩 css 一般就是删除下空格、注释什么的,混淆下 js 一般是使用的 uglifyjs 做的压缩,这个 uglifyjs 是基于nodejs 的运行的,如果是 css 文件压缩我们一般手动写下代码处理下就可以了,但是 js 混淆呢?压缩变量尺寸,去除空格这个写起来很难,也不用去重复造轮子。github 大佬帮你解决一切,在 github 上有这样一个类库https://github.com/drify/UglifyJS.php,其实有很多种写法大家可以去看看,也有使用 demo。
1、配置服务器钩子
#!/bin/sh WWWROOT=/home/wwwroot/yanghaihua cd $WWWROOT unset GIT_DIR git config --list sudo -u www git fetch --all sudo -u www git reset --hard origin/master git show --pretty=oneline --name-only | awk "NR!=1{print $2}" > /home/wwwroot/yanghaihua/push_file_list.txt sudo -u www /usr/local/php/bin/php /home/wwwroot/yanghaihua/compression/compression.php rm -f /home/wwwroot/yanghaihua/push_file_list.txt
这里我没有用 git pull origin master 命令,因为没有必要这样,服务器只提供更新操作,而不提供任何的更改动作,所以直接使用的 git fetch 更新所有代码不同步 和 git reset 同步 master 分支最新代码。
然后 git 获取上传文件清单写入到项目根目录下,再执行压缩文件 compression.php 来读取文件清单,进行过滤 js、css 文件进行压缩操作。
压缩完成后直接删除文件清单就可以了。
2、执行压缩的 php 代码
<?php // 引入外部文件 include_once __DIR__ . '/../config/const.php'; include_once __DIR__ . '/uglifyjs.php-master/src/parse-js.php'; include_once __DIR__ . '/uglifyjs.php-master/src/process.php'; // git push 文件清单 $compressionFilePath = ROOT . '/push_file_list.txt'; // 禁止压缩文件清单 $ignoreFilePathArray = [ WWWROOT . '/xxxxxxx/xx/xxxxx.js', WWWROOT . '/xxxxxxx/xx/xxxxx.css', ]; /** * 动态压缩js、css文件. * @param string $filePath * @return bool|int|void */ $jsCssCompression = function ($filePath) use ($ignoreFilePathArray, $parse, $ast_mangle, $ast_squeeze, $strip_lines, $gen_code) { foreach ($ignoreFilePathArray as $ignoreFilePath) { if (strpos($filePath, $ignoreFilePath) !== false) { return false; } } $pathInfo = pathinfo($filePath); if (!isset($pathInfo['extension'])) return false; if ($pathInfo['extension'] == 'css') { $cssContent = file_get_contents($filePath); $cssContent = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $cssContent)); return file_put_contents($filePath, $cssContent); } elseif ($pathInfo['extension'] == 'js') { $jsContent = file_get_contents($filePath); $jsContent = $parse($jsContent); // parse code and get the initial AST $jsContent = $ast_mangle($jsContent); // get a new AST with mangled names $jsContent = $ast_squeeze($jsContent); // get an AST with compression optimizations $jsContent = $strip_lines($gen_code($jsContent)); // compressed code here return file_put_contents($filePath, $jsContent); } return false; }; /** * 递归获取文件列表. * @param string $filePath * @return bool */ $readGitPushFile = function ($filePath) use ($jsCssCompression, &$readGitPushFile) { if (is_dir($filePath) && $openDir = opendir($filePath)) { while (($file = readdir($openDir)) !== false) { if ($file == '.' || $file == '..') continue; if (is_dir($filePath . '/' . $file)) { $readGitPushFile($filePath . '/' . $file); } elseif (is_file($filePath . '/' . $file)) { $jsCssCompression($filePath . '/' . $file); } } closedir($openDir); } elseif (is_file($filePath)) { $jsCssCompression($filePath); } return true; }; // 压缩文件 if (file_exists($compressionFilePath) && filesize($compressionFilePath) > 0) { $pushFileArray = array(); $foPen = fopen($compressionFilePath, 'r'); while (!feof($foPen)) { $line = fgets($foPen); if ($line) { $readGitPushFile(ROOT . '/' . rtrim($line)); } } fclose($foPen); echo '压缩完成'; } else { echo '未发现压缩文件'; }
基本大致流程就是这样,然后每次调用钩子的时候就会自动压缩静态文件了。
结束语:感谢您对本网站文章的浏览,欢迎您的分享和转载,但转载请说明文章出处。