Ric*_*der 14 javascript php minify
我做了一些环顾四周,但我仍然感到困惑.
我尝试过Crockford的JSMin,但Win XP因某些原因无法解压缩可执行文件.
我真正想要的是一个简单易用的JS minifier,它使用PHP来缩小JS代码 - 并返回结果.
原因是因为:我有2个文件(例如)我正在处理它们:scripts.js和scripts_template.js
scripts_template是我写的普通代码 - 然后我必须缩小它并将缩小的脚本粘贴到scripts.js中 - 我实际在我的网站上使用的脚本.
我想通过在我的页面上做这样的事情来消除中间人:
<script type="text/javascript" src="scripts.php"></script>
Run Code Online (Sandbox Code Playgroud)
然后是scripts.php的内容:
<?php include("include.inc"); header("Content-type:text/javascript"); echo(minify_js(file_get_contents("scripts_template.js")));
Run Code Online (Sandbox Code Playgroud)
这样,每当我更新我的JS时,我都不必经常访问网站来缩小它并将其重新粘贴到scripts.js中 - 所有内容都会自动更新.
是的,我也尝试过Crockford的PHP Minifier,我看了一下PHP Speedy,但我还不了解PHP类......那里有什么猴子可以理解的,也许是RegExp的东西?
我们如何让这更简单?
我只想删除制表符空格 - 我仍然希望我的代码可读.
这不像剧本使我的网站显得滞后,它只是一切都比没有好.
删除标签,有人吗?如果可能的话,如何完全删除BLANK线?
Rob*_*t K 23
道格拉斯·克罗克福德(Douglas Crockford)使用了JSMin 的PHP实现已有一段时间了.连接文件时可能会有一点风险,因为在闭包结束时可能会丢失分号.
缓存缩小的输出并回显缓存的内容是明智的,只要它比源文件更新.
require 'jsmin.php';
if(filemtime('scripts_template.js') < filemtime('scripts_template.min.js')) {
read_file('scripts_template.min.js');
} else {
$output = JSMin::minify(file_get_contents('scripts_template.js'));
file_put_contents('scripts_template.min.js', $output);
echo $output;
}
Run Code Online (Sandbox Code Playgroud)
你也可以试试JShrink.我之前从未使用过它,因为之前我没有遇到过JSMin的困难,但下面的代码应该可以解决问题.我没有意识到这一点,但JShrink需要PHP 5.3和命名空间.
require 'JShrink/Minifier.php';
if(filemtime('scripts_template.js') < filemtime('scripts_template.min.js')) {
read_file('scripts_template.min.js');
} else {
$output = \JShrink\Minifier::minify(file_get_contents('scripts_template.js'));
file_put_contents('scripts_template.min.js', $output);
echo $output;
}
Run Code Online (Sandbox Code Playgroud)