加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
process_file_js.php 2.42 KB
一键复制 编辑 原始数据 按行查看 历史
oldhuhu 提交于 2019-12-12 17:06 . change all chinese to english
<?php
require_once "process_file.php";
require_once "vendor/steelywing/Chinese.php";
require_once "vendor/codesniffer/Tokens.php";
require_once "vendor/codesniffer/Tokenizer.php";
require_once "vendor/codesniffer/Comment.php";
require_once "vendor/codesniffer/JS.php";
require_once "vendor/codesniffer/TokenizerException.php";
define('PHP_CODESNIFFER_VERBOSITY', 1);
use PHP_CodeSniffer\Tokenizers\JS;
function strip_one_js_file($from_file, $to_file)
{
$buffer = file_get_contents($from_file);
// already minified files, skip them
$to_skip = array(
'.min.js', 'highcharts.js', 'qqapi.custom.js',
'/qb.js', 'jpeg.encoder.basic.js', 'userapp_swfobject.js'
);
$should_minify = true;
foreach ($to_skip as $skip) {
if (ends_with($from_file, $skip)) {
logging\debug("skip stripping js %s", $from_file);
$should_minify = false;
break;
}
}
if ($should_minify) {
logging\debug("processing js %s", $from_file);
$eol = detect_eol($buffer);
$js = new JS($buffer, null, $eol);
$tokens = $js->getTokens();
$newstr = "";
$block_comment = "";
$in_block_comment = false;
foreach ($tokens as $token) {
$content = $token["content"];
if ($token["type"] !== "T_COMMENT") {
$newstr .= $content;
} else {
if (starts_with($content, "//")) {
$newstr .= $eol;
}
if (starts_with($content, "/*")) {
$block_comment = "";
$in_block_comment = true;
}
if ($in_block_comment) {
$block_comment .= $content;
}
if (ends_with($content, "*/")) {
$in_block_comment = false;
if (str_contains($block_comment, "[Discuz!]")) {
$newstr .= $block_comment;
}
}
}
}
$buffer = ltrim($newstr);
}
mkdir_of_file($to_file);
file_put_contents($to_file, $buffer);
}
function js_file_condition($fi)
{
return strtolower($fi->getExtension()) == 'js';
}
function strip_all_js_files($from_path, $to_path)
{
logging\info("processing JavaScript files");
process_all_files_with_condition($from_path, $to_path, 'js_file_condition', 'strip_one_js_file');
}
?>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化